# Need to test the angle of the cursor (or an object) in relation to player for a 2D platformer

``````What you want to do ?
``````

As per title:

Need to test the angle (range) of the cursor (or an object) in relation to player for a 2D platformer. (Trying to program the player to be able to shoot stuff towards where the mouse is. both with realistic hitscan-like weapons and projectile based stuff.)

``````What is the issue ?
``````

Couldn’t find a good way to do it (Or think of one using the options I saw)

``````Give a screenshot of the events
``````

Don’t think it’s necessary in this case, but just to be safe:

condition:
player x < cursor x
→ player y > cursor y
→ (cursor x - player x) > (player y - cursor y)

action:
tan( (player y - cursor y) / (cursor x- player x) )

this is one example. you have to check x and y positions of cursor and player, figure out what is the shorter distance and divide the short distance with the long one and use that value for tan().

Jesus christ… this is… humbling haha

I have no idea what any of that means…

Could you maybe explain it in a more… “for dummies” version? I totally suck at math and this looks like Klingon to me

(Yes, I know I’ll need a lot of math for Game-Dev’ing… I’m doomed lol)

What is the… everything in that picture? lol

I assume “a” and “u” is the “angle range” I’ll be scanning with this?

this is one example. you have to check x and y positions of cursor and player, figure out what is the shorter distance and divide the short distance with the long one and use that value for tan().

Okay, check the coordinates of player and mouse I can do. But… figure out what is the shorter distance…? Between them? How, and why?

Sorry if this is a lot, I could just paste it all in their specific lines and not care, but I want to be able to learn and understand what does that math do, and hopefully find other uses for it if I ever see the need again…? I hope I’m not asking too much.

i am making an example, so its easier to understand.
But i am wondering, if you really need it. what do you want to do with that angle exactly?
are you sure you cannot just use a distance check or compare x/y positions?

the formula you can use:
ToDeg(atan2((Player.PointY(“Center”)-MouseY()),(MouseX()-Player.PointX(“Center”))))
this returns the “correct” angle up to 90degree when in front and continues to grow over 90deg. behind the player.
if you multiply this formula with -1 you get angle in orientation used in gdevelop aka
0 deg = right
90 deg = down
180 (-180) deg = left
-90 deg = up

what do you want to do with that angle exactly?

Okay, so…

I have up to 16 different sprites for the character aiming. each for an angle.

But to know which of those sprite the game has to draw… I need to know where the mouse is. (AKA between 45-90 degrees in relation to the player. or 180-225 degrees. or anything like that.

Knowing that, I can tell the program which sprite do I want it to draw on the screen. so the player character is always looking in the general direction of the mouse. get it?

(PS: I know I can just use a single sprite and rotate|flip it, but it looks super bad regardless of anti-aliasing or not. and I prefer this method using premade sprites so I can make sure it always look good regardless of where the player is facing.)

Here’s a drawing I made to find the angle ranges I needed. hopefully it helps you get a better picture of what I’m trying to do:

the formula you can use:
ToDeg(atan2((Player.PointY(“Center”)-MouseY()),(MouseX()-Player.PointX(“Center”))))

“Cannot find an expression with this name: ToDeg”

i dont understand where you tried the formula, cause when you are using it for a number it works, both in action and condition.
I recommend you use a variable that holds the angle and check against that variable to set your animations.

Hi Slash! Thanks a ton for the help so far.

Sorry for delayed reply. I was fast asleep by the time you replied.

i dont understand where you tried the formula, cause when you are using it for a number it works, both in action and condition.
I recommend you use a variable that holds the angle and check against that variable to set your animations.

I was first trying to make the game return it ingame to see if it was the result I needed. Unfortunately it seems it doesn’t understand your formula, despite recognizing perfectly others like atan and ToValue… I don’t know why.

Edit: I can’t seem to be able to dl your file… The download button just returns me this:

( ^ Note this is the screenshot only. not actual text)

because you try to use a number as text.
it should look like this (taken from my example:)

I did click that link, and I also use Firefox. not sure what went wrong before but I managed to download it now.

because you try to use a number as text.
it should look like this (taken from my example:)

Thank you for this. I learned that Player.X() and Player.PointX(“Center”) are different things and know better now.

However, one issue still plagues me.
I copied the code exactly as you showed me, also taking your example as reference, and yet for some reason it only returns 0 to me. I don’t know why.

I decided it might be easier to upload the file for you now… maybe you could tell me what am I doing wrong…?

(Controls are “w”, “a”, “s”, “d”, Left Mouse shoots, “p” Pauses and “Esc” quits the game.)

because your text is refering to to variable MouseAngleToPlayer, while you use the function to modify the Global variable MouseAngleToPlayer!
those are 2 different and indipendend variables. change it to scene variable and ure good.

Jesus christ I’m an idiot… Bless you! I’ll fix it now!

btw, i noticed, that you might want to change the angles of your lasers too
looks weird otherwise

Okay, so…

The code almost works now… but it’s still not quite like Angles yet?

It’s currently kinda like this:

“o” = Player

``````      90°
|
``````

180° - o - 0°
|
-90°

``````     270°
|
``````

180° - o - 0°
|
90°

…How could I fix that to work like so?

I first tried making it multiply by -1 if the number was less than 0, but it didn’t seem to work at all…

PS: I updated the game file, so feel free to redownload using the link from earlier if you’d like to check it again.

Also, about the controls, I forgot to say before: Left mouse button shoots.

since you can now see the numbers, it should not have been to difficoult to figure out how to get all positive numbers to 360 degree …

this is how i would change the animation:

I’m still taking a look at your version. Love what you did btw, it’s brilliantly simple and I feel dumb af to not have tought of it myself…

But meanwhile, I thought you mighta wanted to see this:

Something seems horribly broken there, and I’m not sure why or how.

Okay, so, first of all, I found out what causes the angles to bork is moving the player to the right enough. moving it back to the left|center fixes it again… why is this happening…?

Second…

This is absolutely brilliant, and I’m ashamed to say I would never have thought of it myself…

fixed version
the angles got messed up because the centerpoint was jumping to much. resizing the sprites to the 8x8 pixels fixed it (i also rounded the angle result to make sure no odd numbers are calculated).
also the set animation number via math is something i learned from the topdown example, i did not came up with that myself

1 Like