Jump to content
  • Advertisement
Sign in to follow this  
Locutusborg

using mouse to steer a spacecraft

This topic is 3770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!