How can I use the mouse to rotate an object?
Hey, folks!
I'm working on an Atari Asteroids style game where there is a spaceship which rotates and shoots bullets in straight direction from its action point. Presently I have the controls set up to only use left and right keyboard keys to rotate the spaceship. Is there a way I can use the mouse to rotate the ship's direction?
Thank you for your help!
Most appreciatively...
RGBreality
Re: How can I use the mouse to rotate an object?
Advanced Direction Object has "Rotate towards an object" and might also have "Rotate towards a point". Custom coding wouldn't be too hard either if you know how to work the angles.
Re: How can I use the mouse to rotate an object?
Create two Active Objects, MouseCurrent and MousePrevious. In their properties, make both of them invisible so they are not show at the start of a level.
In the Event Editor set up the following code...
{Always position MouseCurrent where the mouse pointer is on the X axis.}
Always
> Set X of MouseCurrent to X of Mouse.
{Calculate where MouseCurrent is in regards to MousePrevious on the X axis. Subtract the X of MouseCurrent from the X of MousePrevious which will return a number which is either positive (MouseCurrent is to the left of MousePrevious) or a negative (MouseCurrent is to the right of MousePrevious). If it returns 0, they are both in the same place, the mouse has not been moved. Store this value in Alterable Value A of MouseCurrent.}
Always
> Set Alterable Value A of MouseCurrent to X of MouseCurrent - X of Mouse Previous.
{If MouseCurrent is to the left of MousePrevious, then every 0.05 seconds, rotate the SpaceShip 1 notice anticlockwise.}
If Alterable Value A of MouseCurrent > 0
+ Every 0.05 seconds
> Set direction of SpaceShip to direction of SpaceShip + 1
{If MouseCurrent is to the right of MousePrevious, then every 0.05 seconds, rotate the SpaceShip 1 notice clockwise.}
If Alterable Value A of MouseCurrent < 0
+ Every 0.05 seconds
> Set direction of SpaceShip to direction of SpaceShip - 1
{Always position MousePrevious over the mouse pointer, where MouseCurrent is, so the calculations are ready to be checked again.}
Always
> Set X of MousePrevious to X of Mouse.
Off the top of my head, I think this should work. Might need a bit of tweeking but you should be able to rotate the SpaceShip by moving the mouse left and right.
Re: How can I use the mouse to rotate an object?
Or, if you want the spaceship to point at the mouse, set the angle of the spaceship to
atan2(y("spaceship")-ymouse,xmouse-x("spaceship"))
and then set direction to
Angle("spaceship")/11.25
Re: How can I use the mouse to rotate an object?
Another way is to have an invisible object always at the mouse (just like my example above started) and always set the SpaceShip to look at 0,0 of the invisible object.
WOW! So many ways to turn an object with a mouse ;).
Re: How can I use the mouse to rotate an object?
Hey, Paul_Boland! Thank you for your suggestion! I really like how nicely that turns out! Great idea!
But I need to pick your brain again... I need this spaceship to be able to turn 360 degrees (or all 32 directions). Because MouseCurrent only stays within the X-axis, the spaceship can only rotate 180 degrees (X-coordinates which are less than the spaceship's Y-coordinate).
So, how can I adjust this so that the spaceship can rotate 360 degrees?
Thank you again for your help and great idea!
Most appreciatively...
RGBreality
Re: How can I use the mouse to rotate an object?
Use mine, it solves for this
Re: How can I use the mouse to rotate an object?
Hey, Jacob!
I had tried your idea, but somehow the spaceship "snapped" (rather than rotated) to the mouse point. I wonder if I did it incorrectly? Here is what I did:
1.) If "spaceship" is visible
-->ATan2(YActionPoint( "Spaceship" )-YMouse, XMouse-XActionPoint( "Spaceship" ))
--> Angle( "Spaceship" )/11.25
I must have done something wrong, because the spaceship only moves within a 11.25 degree angle from its initial rotation.
What did I do wrong here?
Thank you again!
Most appreciatively...
RGBreality
Re: How can I use the mouse to rotate an object?
Set Angle to: ATan2(YActionPoint( "Spaceship" )-YMouse, XMouse-XActionPoint( "Spaceship" ))
will set the angle in 360 degrees. The other part was just for setting the direction based on the angle.
Re: How can I use the mouse to rotate an object?
Hey, Jacob!
Ah, that totally makes sense! Great idea! Thank you much!
Let me pick your brain one more time: After implementing this, I see that the ship can rotate very quickly (depending on how fast the mouse is moved). I've used the Kernel object to temporarily set the mouse speed to 2, and yet it moves really fast (sometimes just spinning around like a top)!
Would you have any ideas on how to control the speed in which the ship rotates, regardless of how fast the mouse is moved?
Thank you again!
Most appreciatively...
RGBreality
Re: How can I use the mouse to rotate an object?
Quote:
Originally Posted by RGBreality
Hey, Paul_Boland! Thank you for your suggestion! I really like how nicely that turns out! Great idea!
But I need to pick your brain again... I need this spaceship to be able to turn 360 degrees (or all 32 directions). Because MouseCurrent only stays within the X-axis, the spaceship can only rotate 180 degrees (X-coordinates which are less than the spaceship's Y-coordinate).
So, how can I adjust this so that the spaceship can rotate 360 degrees?
Thank you again for your help and great idea!
Most appreciatively...
RGBreality
That shouldn't happen. The Spaceship is not set to look at the mouse, it's just being rotated based on the mouse being moved left or right. The ship sould be able to do full circles. Now if it was looking at the mouse, then yes, it would be restricted to 180 degrees, but it's not. I've not tested this in MMF but executing the code in my head, I don't see why it shouldn't be able to do a full rotation.