View Full Version : rotate gun by mouse move
john_flash
January 9th, 2006, 01:05 AM
hey guys , i need some help,
am working on a shoot-out game and i dont know how to make the gun mc rotate to the direction of the mouse
thanks
Smee
January 9th, 2006, 01:29 AM
Just use
Gun._rotation = -Math.atan2(Gun._x-_xmouse, Gun._y-_ymouse)*180/Math.PI;
NiñoScript
January 9th, 2006, 09:35 AM
Smee, you're wrong...
the formula is dy/dx u used dx/dy :puzzle:
here is the real code:
Gun._rotation = -Math.atan2(Gun._y-_ymouse, Gun._x-_xmouse)*180/Math.PI;
nathan99
January 9th, 2006, 06:34 PM
Actually Nino, Smee is right, his points in the right direction, yours doesnt.
NiñoScript
January 9th, 2006, 08:39 PM
Math.atan2(y, x)
to calculate the arc tangent you need to know the "slope" between 2 points, which is calculated by dividng the difference between the y positions of both points by the difference between the x positions of both points
that is:
(delta y) / (delta x)
actually, we're both wrong, this is the real code:
Gun._rotation = Math.atan2(_ymouse-Gun._y, _xmouse-Gun._x)*180/Math.PI;
of course, your object has to have the "top" on its "right"
example of a guy mc : >-|o
blazes
January 9th, 2006, 09:45 PM
we should just make gun rotation a tag.;)
Smee
January 9th, 2006, 11:17 PM
Actually, we're.. oh forget it.
Look again at the function atan2, there is NO "-" sign. It's asking for two arguments, there's a comma.
Math.atan2(x, y) not Math.atan2(x - y).
NiñoScript
January 9th, 2006, 11:48 PM
-?? where did i use "-" ?
please read it again :P
Smee
January 10th, 2006, 01:16 AM
Actually, sorry about that. I meant to say "/". I was replying to your first message:
Smee, you're wrong...
the formula is dy/dx u used dx/dy :puzzle:
ActionScript Code:
Gun._rotation = -Math.atan2(Gun._y-_ymouse, Gun._x-_xmouse)*180/Math.PI;
I didn't receive emails about the rest of the replies, so I didn't know there were any. I was trying to say that yes, the formula is dy/dx, but atan2 doesn't ask for it like that with the division.
Yes, you're right that it asks for it in the other order (y, x), but then, as you said, you have to rotate your graphic. Not only that, but you have to keep the extra 90º in mind at all times, like when firing the bullet. Switching the (x2-x1) to (x1-x2) and the order of the arguments allows you to just leave the graphic pointing upwards, and you don't need to keep adding and subracting the 90º.
Take these two examples for instance:
ActionScript Code:
onEnterFrame = function()
{
Gun._rotation = -Math.atan2(Gun._x-_xmouse, Gun._y-_ymouse)*180/Math.PI;
var Radians = Gun._rotation/180*Math.PI;
Gun._x += Math.sin(Radians);
Gun._y -= Math.cos(Radians);
}
I know you wouldn't usually want to move the gun itself towards the mouse, but this is just for explaination :P
In that example, you need only use the Gun MovieClip's current _rotation property to find the number of radians between it and the mouse.
Now look at this example:
ActionScript Code:
onEnterFrame = function()
{
Gun._rotation = Math.atan2(_ymouse-Gun._y, _xmouse-Gun._x)*180/Math.PI;
var Radians = (Gun._rotation+90)/180*Math.PI;
Gun._x += Math.sin(Radians);
Gun._y -= Math.cos(Radians);
}
In the second example, you need to not only turn the graphic 90º, but also add 90º to the Gun MovieClip's _rotation property when finding the radians. If your movie becomes more complicated, it can become hard to keep in order, and it will become less efficient.
You don't need to conform to standards with math, you can adapt what is already known to your specific needs. That's why it's so fun :drool:
NiñoScript
January 10th, 2006, 11:37 AM
Ahem...
var speed = 10;
onEnterFrame = function () {
Gun._rotation = Math.atan2(_ymouse-Gun._y, _xmouse-Gun._x)*180/Math.PI;
//
var Radians = (Gun._rotation)/180*Math.PI;
Gun._x += Math.cos(Radians)*speed;
Gun._y += Math.sin(Radians)*speed;
}
Smee
January 10th, 2006, 03:37 PM
Um.. Are we fighting or something? What exactly are you trying to do?
My point is that there is no one way to do things. When something is pointing upwards, it's rotation is 0º. When the mouse is directly above the gun, the gun should point upwards, and it's rotation should therefore be 0º. With the code I gave in my first post, the gun's rotation is 0º when the mouse is directly over it. If the code forces you to rotate the graphic inside the MovieClip 90º, and then have the MovieClip itself rotated to -90º to make it look like it's facing 0º, then it's going to become inefficient, or at least unorganized. Even if you manage to keep it organize, it would be needlessly complicated.
NiñoScript
January 10th, 2006, 06:00 PM
Um.. Are we fighting or something? What exactly are you trying to do?
i thought it was something like a pointless debate, i was having fun :lol:
btw, its not complicated, its just.. oh forget it. :P
icio
January 10th, 2006, 06:45 PM
When the mouse is directly above the gun, the gun should point upwards, and it's rotation should therefore be 0º..
The rotation should be -90 if the mouse is directly above it (ie. dy = 0).
See the attached diagram - I've mapped out the locations of the angles. So you would have your gun facing to the right by default and then you would simply be able to use:
gun.onMouseMove = function() {
this._rotation += Math.atan2(this._ymouse, this._xmouse)*180/Math.PI;
}
Smee
January 10th, 2006, 07:32 PM
No one is understanding a thing I'm saying :ogre:
There's that word again, "should". Why? Is there a hidden rule somewhere that stops all other code from working? It's easier to deal with if the _rotation property of the Gun MovieClip is the same as it's direction. In this case it's simpler to have the MovieClip's actual rotation (the number you would get if you traced the Gun MovieClip's _rotation property) be the same as you would normally think of it. When something is facing right, I think "That object is rotated 90º". When it's facing down, I think "Now it's rotated 180º".
Yes, it's documented to be (y, x) and (y2-y1), but that doesn't mean that's the only thing that works.
Smee, you're wrong...
So are you saying my code doesn't work? :deranged: I'm sure it does. It may not be what you would write on a math exam, but in this specific case, it makes things better. Isn't that what math in programming is all about? Using the numbers any way you can to your advantage?
icio
January 11th, 2006, 02:49 AM
I see exactly what you are saying, smee - the thing is that it's not nearly as efficient.
You may aswell have:
Math.atan2(_ymouse, _xmouse)+90-90;
NiñoScript
January 11th, 2006, 09:46 AM
So are you saying my code doesn't work?
not really, as i said before:
i thought it was something like a pointless debate, i was having fun :lol:
btw, thanyou icio for supporting me :P
Smee
January 11th, 2006, 08:33 PM
You may aswell have:
Math.atan2(_ymouse, _xmouse)+90-90;
That's exactly what I'm saying, sort of.
There you have +90 and -90, which together make 0. You're doing the same thing when you rotate the graphic inside the MovieClip +90º, and then the MovieClip itself -90º, see?
I'm saying that it's pointless, and doesn't have to happen. Yes, both ways will point the gun towards the mouse, but you have to think about what comes next. What about the bullets for example? If you attach the bullet and send it in the direction that the Gun MovieClip is pointing, then it will come out the side of the gun. Yes, you can do the same thing to the bullet that you did to the gun (rotate it's graphic +90 and it's MC -90), but why do that? What's the benefit of having things flying around the screen SIDEWAYS?
I just find it kind of stupid that the gun would be pointing say.. down, but it's MovieClip _rotation property says 90... It makes much more sense for it to say 180 when it's pointing down, yeah? I'm not saying either of you are wrong, you're not. It's just that being misunderstood so many times in a row is a real ticker. :fight:
icio
January 12th, 2006, 11:18 AM
My point was that the code was unnecessary, as subtracting 90 each time because you have rotated the movieclip.
I guess you'll just have to make the decision between more efficient code and having to subtract 90 every time you calculate the angle because you rotated the movieclip because that "makes sense".
But I'm actually now unsure of what your point is, after your last post. You contradicted yourself a little:
There you have +90 and -90, which together make 0. You're doing the same thing when you rotate the graphic inside the MovieClip +90º, and then the MovieClip itself -90º, see?
I'm saying that it's pointless, and doesn't have to happen.
Weren't you saying that it was more sensible? Btw, yes I do see - it was the whole point of the code that I posted... pointless. The thing is, it's exactly what you're doing.
I'm going to say no more on this because I don't want to cause trouble. I'm sticking to my guns on this.
Smee
January 12th, 2006, 09:10 PM
Where am I subtracting 90 each time? I never said to DO that, I was saying that it's unnecessary. With what I was saying works, you don't even need the number 90.
I didn't conradict myself at all. I quoted what you said, related it to what was being done with the graphic in the MovieClips, then agreed with you that it was pointless.
My first post has the code that I would use, and the one I'm trying to explain. It does NOT require any rotation of 90º.
I'd like to end this as well, but I don't want it to end with me feeling like I'm speaking another language, because apparently I'm impossible to understand. Everything I've said was taken the opposite way..
icio
January 12th, 2006, 09:29 PM
OMG. I'm so sorry, you're absolutely right. Greatest appologies, Smee.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.