using mouse to steer a spacecraft

Started by
8 comments, last by Locutusborg 15 years, 10 months ago
I am trying to make a spacecraft fly in the direction of the mouse. The camera is always straight behind the spaceship. If the mouse is to the right, it is supposed to turn to the right, and the same goes for the left (this must also be done for up and down, but I am still working on that) My problem now is as follows: I start with setting the mouse in the center of the screen. I use directinput to find the mousemovement and I keep track of it. this is the code I use: mousex += mouseState.lX; if (mousex > 0) {ship.headingg+=0.002f;} else{ if (mousex<0) {ship.heading-=0.002f;} } this turn the ship, but after a while the zero position (at which the ship does not turn) is no longer in the center of the screen. Perhaps I am doing something wrong, or should I use the absolute mouse position using GetCursorPos()? Thanks for any ideas and suggestions!
Advertisement
Firstly, you don't want to be using DirectInput for keyboard or mouse input at all. The problem you're seeing is that Windows is applying pointer ballistics to the cursor, but DirectInput doesn't use them at all. That causes the two systems to get out of sync.

I'd recommend ditching DirectInput completely and just using GetCursorPos() to determine where the cursor is, relative to the center of the screen - or just using WM_MOUSEMOVE messages to do that. DirectInput adds absolutely nothing in this case, and just makes things more troublesome.
I'm not sure what you mean, but if you have the same problem as this guy, then see my reply there for a possible solution.
This was indeed the answer I was looking for! My steering is working now.

Thank you for youre replies
I tried to make the steering work also for the vertical axis. succeeded there, however....
When facing directly down, and than turning to one side, causes the heading to shift by 90 degrees (from forward to side). I haven't been able to fix this using sin, cos, the lot. (Euler methode)

I found something called a quaternion, which is supposed to help in these cases.
Does anyone have an idea on how to implement this (or a site where this is explained?)

Thank you again!
You're probably getting what's called gimbal lock.

As far as a site that explains quaternions - which of the 518000 hits I got on Google do you want a link to? [smile]

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

You might benefit from a discussion I had over here.
I googled around a bit, and indeed, it seemes like a gimbal lock. apparently, the guy from the discussion is having about the same problem, though he is using openGL.

I tried to change the engine from euler to quaternions, I use the following code:

D3DXQUATERNION tmp;
D3DXQuaternionRotationYawPitchRoll(
&tmp,
0.001, //yaw
0, //Pitch,
0 //Roll
);
result *= tmp;

this does rotate around one axis. I put this up for another axis and using the two togehter, it seems I am not rotating around the local (mesh's own) axis, but around a world axis. As far as I know, I am not storing any heading anymore, that suggests that the direction is incrementing. (got this from the other forum)
My question is how I can get the object to turn aroud its own axis?
If you want to apply a rotation about a local axis, you need to prepend the new rotation transform. So let's say your plane is at at some arbitrary orientation, represented by matRotation, and you wanted to rotate a bit about the local y-axis:

D3DXMATRIX matNewRotation;D3DXMatrixRotationY(&matNewRotation, 0.01f);matRotation = matNewRotation * matRotation;


That's what I was looking for!

The order in multipying with quaternations and matrixes is important. By using the operator *= I multipy the wrong way

matRotation *= matNewRotation is the same as:
matRotation = matRotation * matNewRotation

as opposed to:
matRotation = matNewRotation * matRotation;


Thanks for your assistence!

This topic is closed to new replies.

Advertisement