Hello. I'm trying to do a top-down shooter in lua,
but when i test the enemy, i see a problem in the angle.
I already had this problem (it's 'in the pi 'n -pi), but i don't remember how i fix...
I need help with a angle problems
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 5
- Joined: Sun Mar 17, 2024 10:05 pm
I need help with a angle problems
- Attachments
-
- game.love
- game.love
- (11.96 KiB) Downloaded 163 times
Re: I need help with a angle problems
What's the problem?
Re: I need help with a angle problems
Provide the code and elaborate on what your exact problem is.TheFoxyHu3 wrote: ↑Sat Oct 19, 2024 7:49 pm Hello. I'm trying to do a top-down shooter in lua,
but when i test the enemy, i see a problem in the angle.
I already had this problem (it's 'in the pi 'n -pi), but i don't remember how i fix...
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: I need help with a angle problems
So the question is, when going from angle `A1` to angle `A2` clockwise, will we find angle `a` in the way?
I think this function answers it:
Proof of concept attached. Move the ball with the mouse and change the detection angle with the mouse buttons.
For future reference, it would help us help you if you give some tips about how to reproduce the problem, what to do to test and what should happen. In this case it could have been something like this:
"Try game.love and right click somewhere on the screen to add enemies, then move with WASD. The player should turn red when it is within range and between the two visibility lines of an enemy, but it's not turning red sometimes."
@dusoft The code is already provided.
I think this function answers it:
Code: Select all
local function angleInRange(a, A1, A2)
A2 = (A2 - A1) % 6.283185307179586
a = (a - A1) % 6.283185307179586
return a <= A2
end
For future reference, it would help us help you if you give some tips about how to reproduce the problem, what to do to test and what should happen. In this case it could have been something like this:
"Try game.love and right click somewhere on the screen to add enemies, then move with WASD. The player should turn red when it is within range and between the two visibility lines of an enemy, but it's not turning red sometimes."
@dusoft The code is already provided.
- Attachments
-
- angleDiff.love
- (569 Bytes) Downloaded 117 times
-
- Prole
- Posts: 5
- Joined: Sun Mar 17, 2024 10:05 pm
Re: I need help with a angle problems
Well, I'm trying to make enemies facing* the player.dusoft wrote: ↑Sun Oct 20, 2024 10:23 amProvide the code and elaborate on what your exact problem is.TheFoxyHu3 wrote: ↑Sat Oct 19, 2024 7:49 pm Hello. I'm trying to do a top-down shooter in lua,
but when i test the enemy, i see a problem in the angle.
I already had this problem (it's 'in the pi 'n -pi), but i don't remember how i fix...
Code: Select all
--if the angle of the player is more or less than enemy then will rotate
if math.atan2(plr.y-enemy.y, plr.x-enemy.x) >= enemy.a then
enemy.a = enemy.a + .0349
else
enemy.a = enemy.a - .0349
end
Re: I need help with a angle problems
Maybe related, maybe not:
Are you resetting angle over 360 degrees to zero and under 0 to 360?
Also, just to make things easier, I would recommend switching to degrees instead of radians. But maybe that's just my preference.
Are you resetting angle over 360 degrees to zero and under 0 to 360?
Also, just to make things easier, I would recommend switching to degrees instead of radians. But maybe that's just my preference.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
-
- Prole
- Posts: 5
- Joined: Sun Mar 17, 2024 10:05 pm
Re: I need help with a angle problems
What do you mean, redefine the angle?
I don't know what to do...
I don't know what to do...
Re: I need help with a angle problems
You can describe the same direction using different angle values. Say, 360 and 0 point in the same direction, and 330 and -390 also point in the same direction.TheFoxyHu3 wrote: ↑Sun Oct 20, 2024 8:18 pm What do you mean, redefine the angle?
I don't know what to do...
math.atan2() can return negative angles for some inputs -- the direction of that angle is fine and is what you expect, but the angle is not in that standard range (0, 2pi), and so your IF...THEN will fail because of that.
The code in the demo that pgimeno shared above ("angleDiff.love") shows how you can test if the output of math.atan2() is within the shortest path between two angles.
-
- Prole
- Posts: 5
- Joined: Sun Mar 17, 2024 10:05 pm
Re: I need help with a angle problems
Hello! Finally works. Thank you for everyone her for help me.
(I'm need to delete post? I don't use a forum ever...)
(I'm need to delete post? I don't use a forum ever...)
Re: I need help with a angle problems
You should leave this as is, that's the great thing about forums: others with the same problem as yours can drop in from a web search and find the solution. I've solved a few things based on years-old threads, people that don't even post here regularly anymore.
Who is online
Users browsing this forum: Google [Bot] and 13 guests