Game Development Community

Rotation based on mouse movement (not location)

by Matthew Hagerty · in Torque Game Builder · 03/03/2005 (8:27 pm) · 9 replies

Greetings,

I'd like to be able to control the rotation of my sprite (a tank) based on left or right movement (difference in x position) of the mouse. I'm not trying to have the tank "look" at the mouse. Basically if you move the mouse left slowly, the tank would rotate CCW, slowly. If you move fast, the tank rotates fast. If you keep moving left the tank would continue to rotate CCW, and so on.

In this case I'd also like to hide the mouse cursor and prevent the T2D window from losing focus, since I need mouse clicks to drive forward and fire the weapons.

I was thinking I could do this by recording the mouse position, then when there is movement, compare the new location to the old, which would give me a difference. x_diff = x_new - x_old. If x_diff is positive, the mouse was moved to the right, and the value of x_diff tells how much. I think this would work by converting x_diff into a rotation velocity... Am I on the right track? The only problem I can think of is what happens when the pointer moves outside the T2D window? Also, seeing the pointer will only be a distraction in this case.

Any insight would be greatly appreciated.

Thanks,
Matthew

#1
03/04/2005 (2:17 am)
Matthew: Reading this post will help you. :)

- Melv.
#2
03/04/2005 (6:42 am)
@Melv

I did read those previous posts and they provide very good information, however they all concentrate on getting some object to "look at" and/or "point to" the mouse pointer. I'm not trying to do that. I simply want my sprite to rotate based on mouse "movement" in the x direction. Also, I don't even want to see the mouse pointer during gameplay as it would simply be in the way.

A better way to think of it might be the typical asteroids controls. You have a key that spins you CW and another that spins you CCW. Instead of spin-CW and spin-CCW keys, I'd like to use the mouse. Maybe I can only do this in fullscreen mode since I can't have the mouse leave the game window. But even then, if I hit, say the left edge of the screen, and I continue to move the mouse left, will the mouse stop reporting movement? IF don't care *where* the mouse is, I just want to know did it move, and if so which way and how much.

Matthew
#3
03/04/2005 (6:48 am)
I'd have to take a good look at T2D to make sure it works, but in TGE I did the same general thing by capturing the coordinates of the mouse each tick, and calculating a delta from the previous tick's position, and then feeding that delta directly to a rotation algorithm of some sort. The effect was that if the player moved the mouse "left", my building rotated clockwise, and if the player moved the mouse "right", the building rotated counter-clockwise.

This would give the player direct control over the rotation, instead of an input to the rotation, so it may not be exactly what you want, but you could modify the idea to detect the mouse moving left (via the same delta calculation, just watch the sign of the change), and then input a "rotate clockwise"/"rotate counterclockwise" input to your controlled object.

I'm not sure the best way to do this however with your added requirement of never seeing the mouse pointer--will have to think about it!
#4
03/04/2005 (6:51 am)
You could use the mouseMove callback to store and check previous mouse position and adjust the rotation of the player accordingly. So on each run through of the callback, you compare the current position of the mouse to a previous mouse position (which is assigned after comparison, to keep it updated), and rotate the player by either the amount of x or y difference (this is where it would be multiplied by a sensativity setting). Of course, you would probably need to continuously reset the position of the mouse to the center of the screen so the cursor doesnt get 'stuck' at the screen edge.

As far as no cursor, simply set your mouse curosr image to an empty .png
#5
03/04/2005 (7:23 am)
Guys,

The reason I posted that link is because it does provide answers to the question being asked here. My post of "Posted: Mar 03, 2005 09:32 GMT" summates by saying that I'll look at adding some specific utility functions to allow targetting/clamping directions.

With regards to the mouse itself, there are a fleet of mouse functions that allow you to do everything that you'd expect from a windowed system. You can lock the mouse to the window so that you continue to get mouse events. The mouse doesn't leave the window and this is used in lots of games. Typically, the mouse is locked/hidden until a menu is selected then the mouse is shown/unlocked.

Trying issuing:-

// Lock the mouse.
lockMouse(true);

Now, assuming I've got a sprite called "testSprite", try adding the following mouse-move callback and try it with the mouse locked and unlocked.

function sceneWindow2D::onMouseMove( %this, %modifier, %worldPosition, %mouseClicks )
{
	testSprite.setPosition( %worldPosition );
}

You can also show/hide the cursor with:-

cursorOn();
cursorOff();

Hope this helps,

- Melv.
#6
03/04/2005 (8:46 am)
@Melv

Thanks for the info on those functions! Are those documented somewhere, because I hate to ask questions about documented functions. I really do try to find answers before posting, honest!

Thanks Stephen and Corey for the information, I think both of you confirmed what I was thinking would work.

Matthew
#7
03/04/2005 (10:42 am)
Yes, absolutely.

You'll find them in the TGE Console Commands Reference (by Ron Yacketta) found in the official documentation page here.

- Melv.
#8
09/27/2005 (10:34 pm)
I know that this is a dead thread, but I could really use this resource.

@Melv:
Unless I'm doing wrong, the link to the TGE Console Commands doesn't actually work, it takes you to a page with another link, which brings you back to the same page. Googling gets me a VERY old version of the doc.

Thanks,
Jake
#9
09/28/2005 (12:08 am)
Jake,

It was working when I posted the link. I'm not in control of that area; maybe it's been altered due to some TDN work, who knows?

I believe that the document is very old (TGE console commands haven't changed that much) so you may have the correct one.

Sorry I couldn't help further.

- Melv.