Change object animation (top-down style) with mouse position and possible "bug"

So i wrote “bug” because i believe it’s more of a calculation logic problem (where logic is correct just what you would expect is not) than it is actual bug
I have 3 questions here which you will understand reading what is below them
1 - Is there a better way to do it without additional sprite object (arm object in my case)?
2 - Where did 360 degree go? Why it is 0 to 180 and -180 to 0?
3 - Is it something that we can call a bug and report or (which i am more willing to believe) it is intended and should stay that way?

So in short of it guy came to discord asking for this kinda way to do it where top down object changes animation depending on where mouse is (top down so up down left right)
Problem is if we go by X and Y position from center of player we are doing this
We limit our fields by vertical and horizontal lines which leads into problem of what if mouse is in diagonal position to the player?
Well last animation to which object changed will still be present
And so we want a way to split that fields diagonally

My best idea was to slap additional object (horizontal line which i called arm) and depending on it’s rotation split animations between this

And now we are in business
Problem is that everything works expect left side
And that is because for whatever reason
My initial question #2
It goes from 0 to 180 and then -180 to 0 instead of 360
And so link to preview with this

Which is how it should be (bullet there is just for me to indicate when mouse enters that range)
And it does not work for left

Now i noticed that whole problems comes from the fact that IF center of that circle is center of player
Then anything below red line is 180 then 179 178 and so go on
But above is -180 then -179 -178 and so go on

It does make sense to reset half way because it’s going from 180 and then changes to -180 but that is not what user would expect
And it does make sense why it bugs out

I fixed it by going with 2 separate conditions

And believe or not but it works

So again my questions
1 - Is there a better way to do it without additional sprite object (arm object in my case)?
2 - Where did 360 degree go? Why it is 0 to 180 and -180 to 0?
3 - Is it something that we can call a bug and report or (which i am more willing to believe) it is intended and should stay that way?

Also i attach here zip file of this project if someone care to test/check it

You can get the angle between the player and the mouse using Angle between positions and set it to a variable. You then use the variable in the conditions.

For your left issue. You can use separate events or combine them with an Or and 2 And conditions.

1 Like

Your method of dealing with left is way more elegant than mine
Also Angle between mouse and player now looks so obvious yet i didn’t come up with that idea
So thank you
It works perfectly

1 Like

You’re welcome. I believe in simplicity. The fewer events you use, the easier it is to debug when something invariably goes wrong.

Me also i just forget about other ways to do stuff that would actually work so i just go with 1st thing that comes to my mind that would work

Also do you have any idea why it is 0 to 180 then -180 to 0 and not 360 degree?

1 Like

I don’t know why they use 0 to 180 and -180 to 0. There must be a reason. IDK if it works better with other functions or formulas? Even if it went from 0 to 360, there would still be a crossover when 0 and 360 meet. There’s probably a way to use sin() or some other function to get the direction easier. Time to Google.

I think you didn’t get my question
I was asking why it is NOW 180 -180 and not 360 like everyone are repeating

I never did stuff with angles this was my 1st time
But from day one i sit on gdev discord i remember for angles 270 0 90 180 top right down left
Just like everyone says

And everybody repeated it like mantra to any1 who did need to determine angle of something
I even seen it many times on forums

And when i wanted to do something with angles only thing that saved me from hours of confusion was i wanted to print angle with text object because i was not solving here my issue but another user from discord
So i wanted to show him in which position to player what angle mouse will have

And if not for that fact i would not print angle and simply try to do that 4 animations with 360 degree and wonder for hours why it does not work

So again i don’t care why they switched i bet there is a reason and that is fine with me
I wonder why everyone repeat 360 and it is 180 -180
Did i miss that transition?
Or maybe 360 degree is for something else?
I am simply confused here

It’s like in few years kids will ask why ppl call meta facebook?
I doubt any1 will care why they changed name
But question is why everyone around call it one thing when it is called another

And i’m kinda in same situation i don’t care for reason why it changed
I care did i miss that change and was it not long ago?
Why ppl still refer to it as 360?
Are they same things or angle to object is 180 -180 while 360 is reserved for something else?

I wish I had the answer. I don’t do many calculations with angles. I just know when it’s something like the angle of a sprite it’s 0 to 360 but things like AngleBetweenPostions it’s -180 to 180. I don’t know if it’s because it’s a reference. I really dont know. It confuses me everytime. I usually need to write or draw it out or check the value to figuire out the angles.

1 Like

Hi, I’m trying to program the same behaviour, to change the animation based on the position of the mouse.

I set it up as you described, but the change is not happening.
Any clues?

I believe the issue was the way angle between position works. It’s probably easier to set it up if you set a text object to your MouseAngle variable.

Instead of 0 to 360, it returns - 180 thru 180

You would need to check for those values.
Right up would be -45, left up - 135

See the image on this post

Angles for 6 directions

Move mouse around player look at head

Angle position from center

Thank you, @Keith_1357 and @ZeroX4!

I got it working.

Any idea how to play the animation in reverse when the angle of movement is opposite to mouse angle? :sweat_smile:

1 Like

I’m not sure what you mean.

I mean, now I have a character that is animated according to the mouse angle from the character, so if I point the mouse top-right, the character will look top-right, but if I walk bottom-left at the same time the animation is still for going in the top-right direction. So I’m moving backwards, but the animation shows the character going forwards.

I’m still confused. Are you moving with the keyboard or joystick (it doesn’t matter which one) then what is the mouse for? Is it for aiming? How do you decide which takes control? You need to add some form of logic to switch between the 2 methods or maybe only aim the weapon or use an arrow or crosshairs.

What exactly is happening in your project? What is it about?

It’s an isometric view. I still haven’t decided on how I want the controls to work exactly, I’m still prototyping, but what I’ve got so far is this:

  • I can move the character with keyboard (WSAD) or left gamepad stick and the character is animated depending on the direction of the movement.

  • On right mouse click, for gun control, I’m switching the animations to be based on the mouse angle. So, I’m still walking with keyboard or left stick, but now aiming and rotating the character with mouse or right stick.

I can post a clip tomorrow, if it’s still not clear.

ok, you could have 2 groups, 1 for changing the animation based on keypad and one for mouse aiming.

If mouse is down then do aiming, if [inverted] mouse is down then use the keys. Just put your events as sub-events so the mouse is only checked once and it ignores everything if the state doesn’t match,

This isn’t proper code. This was just a quick way for me to test it.

That’s more or less what I already have, so that’s not the issue :sweat_smile:

I’ll post a clip tomorrow, it will be easier to understand.

But thanks for the input so far!

1 Like

There you go, here’s the clip of my current controls:

I hope this helps.

Nice looking. Is there still a problem? Unless the player twists at the waist or the hands move independent of the body then I’m not sure what you want. You could aim with a crosshair cursor without changing the cat. The cat would follow the keys until the fire button was clicked. That might be nice. The cat would stop, turn and shoot and then continue to move.

If you have a plan, let us know. We’ll try to help.