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().
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
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:
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.
Sorry for delayed reply. I was fast asleep by the time you replied.
To answer your question:
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.
if i click on the link i get to the download page. i use firefox.
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.
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…?
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