[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 856 KB, 275x372, rotate.gif [View same] [iqdb] [saucenao] [google]
6164488 No.6164488 [Reply] [Original]

Hey /sci/, sorry to bother you guys. I'm doing some programming, and I'm having trouble solving a problem. I posted all around the internet, but so far no one has answered me.

Here's the problem:

I need to rotate a model on the screen with my mouse, but this rotation also needs to be based on the current roll and pitch of the camera. I've got the first part down, but I don't know how to go on with transforming my roll and pitch to be relative to the camera. Right now, the rotation only works if the camera has not been rotated yet (which is to be expected considering that's what the current code was made to do.)

Also, do note that I do mean roll instead of yaw. I want the roll to be controlled with the left and right swipes of the mouse.

I've read that quaternions can solve the problem, but being a learn-by-example learner, I haven't been able to figure out how to do this. I can't for the life of me figure out how those things work - a lot of websites seem to overcomplicate what they are. Or I'm probably just an idiot. I'm assuming the latter.

Here is my current code (Note that PI2 is simply Pi * 2):
http://pastebin.com/B3CkdcPL

Pic related is a GIF of how it looks right now when the camera hasn't be rotated. It goes out of whack when I rotate it though.

I'd go by /g/, but you guys are a lot smarter and will probably get down to the point really fast.

I'll delete the thread when I get an answer.

>> No.6164492

>>6164488
My bad, current YAW and pitch of the camera*

>> No.6164536

>>6164488
Well, I would suggest using rotation matrices rather than quaternions. Suppose you have a functions RPY_To_Rot(roll, pitch,yaw) which converts roll pitch and yaw to a rotation matrix, and Rot_To_RPY(rot) which does the opposite. If Rcam is the camera rotation, and (roll_m,pitch_m,yaw_m) are for the model, then you want to update these values as

(roll_m_new,pitch_m_new,yaw_m_new) = Rot_To_RPY(RPY_To_Rot(roll_m,pitch_m,yaw_m)*Rcam)

>> No.6164578

>>6164536
How large would the matrices be? Just clarification purposes.

>> No.6164581
File: 79 KB, 800x900, 1384298856572.jpg [View same] [iqdb] [saucenao] [google]
6164581

>>6164578
>>6164536
Oh, shit, my bad. Reread that and now I see. Haha

Pic related, I've been at this for an hour.

>> No.6164621

>>6164536
How would you transform a vector with just the camera data into a rotation matrix?

>> No.6164889

>>6164621
You mean convert pitch, roll and yaw to a rotation matrix?

>> No.6164954

First you rotate your object within your universe. Then offset your universe by the opposite of the position of the camera. Then you rotate all the points in your universe to apply the orientation of the camera.

>> No.6165049

>>6164889
Yeah.

>> No.6165077
File: 472 KB, 508x270, 1384714577415.gif [View same] [iqdb] [saucenao] [google]
6165077

>>6164954
I need something as specific as >>6164536 was.

I'm having trouble doing the rotation matrix conversions.

Sorry I seem so ignorant on the subject, but I'm not used to working with this stuff. I was a 2D programmer before starting this project. It's been a while since my trigonometry days.

>> No.6165083

>>6165077
It depends on if you do the pitch first, or the roll or the yaw, etc.

http://planning.cs.uiuc.edu/node102.html

>> No.6165084

>>6165083
see also

http://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix

>> No.6165089

>>6165049
It's in here:
http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
they call it a directional cosine matrix

>> No.6165092

>>6165083
How dramatic are the differences between the different matrices you produce?

I don't really care which is done first - but theyr'e both equally important. The rotations are being used for flips he'll be doing in the air - I want the player to be able to control them.

>> No.6165135

>>6165084
>>6165083
This is kicking my ass.

Could someone put it in lamens terms? I'm not sure which equations I need. The way it's written doesn't help either.

>> No.6165150

>>6165092
If you mix up the angles, you will receive incorrect answers and your stuff won't make sense.
I prefer using quaternions, but when I started I kept getting errors from different sources doing the rotation differently.

>> No.6165161

>>6165150
So, should I go with quaternions or rotations?

Shit guys, I'm even more confused than I was when I got here. Haha.

Could someone just give me a run through of how to achieve the desired effect?

>> No.6165173

>>6165135
Okay 1 rotation.
You have two unit bases A=ijk, and B=i'j'k'.
You rotate around k to get from A to B.
so k=k'
Simple rotation ends up like this:
Make a table
<span class="math">^AR_B [/spoiler]
R| i' | j' | k'
i|cx|+sx|0
j|-sx|cx|0
k|0|0|1
where cx = cos x, sx = sin x, x being the angle of rotation. The signs on the sine, depend on the direction of the rotation. The one I have here is for positive right handed rotation.
<span class="math">\mathbf{v}_B=^AR_B \mathbf{v}_A [/spoiler]
you can also view it as a matrix of dot products (or as a multiplication table for dot products)
R|i'|j'|k'
i|i*i'|i*j'|i*k'
j|j*i'|j*j'|j*k'
k|k*i'|k*j'|k*k'

And now the quaternion representation
<span class="math">q= \exp(x \mathbf{ \hat{u}}) = \cos x+\mathbf{ \hat{u}} \sin x[/spoiler]
Since <span class="math"> \mathbf{\hat{u}}[/spoiler] = k=k'

<span class="math"> q=\cos x +\sin x k [/spoiler]
This time the conversion is
<span class="math"> \mathbf{v}_B= q\otimes \mathbf{v}_A \otimes q^{-1} [/spoiler]

>> No.6166559

>>6164536
OP here.

I implemented this last, and it worked for the most part.

The problem now is that the model actually faces the camera as a byproduct of the multiplications. I need him to use his default orientations instead - but how would I do that?

>> No.6166581
File: 1.54 MB, 275x372, rotate.gif [View same] [iqdb] [saucenao] [google]
6166581

>>6166559
Example pic

>> No.6167088

>>6166581
Funny... the math part is easy for me, but the graphics part is way beyond me. Two sides of a coin.