Help with setting angle of weapon!

8 replies [Last post]
VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

I am trying to set the angle of my rifle to my caracter. He stands at the right side of the screen facing left. I have done the caracter as one object without arms.

The Rifle is another object. I have managed to set its position to the body of the Caracter and also done some code for changing the position of the rifle so it looks very good. The problem is that i cant figure out how to set the angle correctly to point at the mouse pointer?

This is how my condition is set up:
*Body* = my caracers body

Allways:
Rifle: Set position at 0,0 of *Body*

Allways:
Rifle: Set angle to YMouse (Quality=1)
Rifle: Set angle to XMouse (Quality=1)

What am i doing wrong?

Also i dont want the arm to be able to rotate around 360 Just 180 at the front.

Jon Lambert's picture
User offline. Last seen 4 days 44 min ago. Offline
Joined: 10/10/2009
GBPs: 51

Your problem is the condition you use to set the angle. Giving a position does not set the angle relative to that position. You are supposed to provide a value between 0 and 360 (in other words, the angle in degrees), where values greater than 360 or lower than -360 loop around to 0. To calculate the angle between two objects, you would use the ATan2 function.

ATan2, an inverse tangent (arctangent) function, returns the angle between two objects when giving the Y distance and X distance between the two objects. It looks like this:

ATan2(YDistance,XDistance)

where YDistance and XDistance are calculations. For you, it would look like this:

ATan2(YMouse-Y("Rifle"),XMouse-X("Rifle"))

That should give you the angle between the mouse and your rifle. To limit the rifle so it doesn't rotate 360 degrees, you can do two things: either use two events, one for left and one for right, or use one, more complicated expression.

Direction of Body = 0:
Rifle: Set angle to Min(Max(ATan2(YMouse-Y("Rifle"),XMouse-X("Rifle")),-90),90)

Direction of Body = 16
Rifle: Set angle to (Min(Max((Abs(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" )))), 90), 180)*(0-(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" ))/Abs(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" )))))*(1-Min(Max(Abs(Dir( "Rifle" )-16), 0), 1)))

or you could use this one, more complicated expression:

Always:
Rifle: Set angle to (Min(Max((0-ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" ))), -90), 90)*(1-Min(Max(Abs(Dir( "Rifle" )-0), 0), 1)))+(Min(Max((Abs(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" )))), 90), 180)*(0-(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" ))/Abs(ATan2(YMouse-Y( "Rifle" ), XMouse-X( "Rifle" )))))*(1-Min(Max(Abs(Dir( "Rifle" )-16), 0), 1)))

There are probably less complicated ways to do it, but this is just my mentality so I won't be able to think of them any time soon.


VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

Thanks for the answer. I put in "ATan2(YMouse-Y("Rifle"),XMouse-X("Rifle"))"

Now it seems to work better but its inversed. So when i pull up on the mouse the rifle goes down and the other way around. (My caracter will allways be facing left by the way)

I also tried both the long and short version to disable the 360 thing.

When i inserted the first nothing changed.

When i inserted the longer version the caracters rifle could not move.

Jon Lambert's picture
User offline. Last seen 4 days 44 min ago. Offline
Joined: 10/10/2009
GBPs: 51

I forgot to say that you have to do 0-ATan2(YMouse-Y("Rifle"),XMouse-X("Rifle")). The longer one works for me, so I'll give you a link to the .mfa source.

http://www.jonhlambert.com/angles.mfa


VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

Thank you very much. I tried to copy your code and replace your active with my rifle. It still inversed though. I think you have missplaced the right and left somewhere. It is impossible to see on the default active object because its the same left and right. But i changed it in your file and wrote "Left" at the left side and "Right" at the right side and made an arrow pointing in the directon the animation was standing in.

WHen i played your mfa file with that active i could see that it was turned to the wrong side, and even upside down i think. I link a file to a picture of the active i made in your file..

http://www.newgrounds.com/dump/item/07246826bcef9be1f31905d0c1499438

Jon Lambert's picture
User offline. Last seen 4 days 44 min ago. Offline
Joined: 10/10/2009
GBPs: 51

If you look at the blue stretchy part, you see that it moves properly. It starts out with the direction set at 16, and if you right-click, it switches between 16 and 0.

All right, I edited my file so that there is a picture of the rifle. I have changed none of the code at all, only the picture of the active.

Download at the same link:
http://www.jonhlambert.com/angles.mfa


VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

AH thanks now it works! I saw that the difference in my rifle and your was that my had one animation for pointing right and one for pointing left. Your rifle had one animation for pointing right and the other for left was upside down, when i changed my "left" animation of the rifle to upside down it worked! That code looked very complicated though, i have no idea how i can learn it myself! :S

VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

I also had a question about how to make a kickback to the rifle when firing, but i pm:ed you my question :)

VaragtP's picture
User offline. Last seen 1 year 12 weeks ago. Offline
Joined: 02/10/2011
GBPs: 21

By the way another problem arised (does it not allways?). I made a bullet and made the condition,
"pressing left mouse buttton"
Rifle: Shoot an object (Bullet) and use directon of Rifle.

The problem is that there is no object shooting when i press the button. I tried to do the same thing with another random object and that worked.. so whats wrong?