I need help with a angle problems

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
TheFoxyHu3
Prole
Posts: 5
Joined: Sun Mar 17, 2024 10:05 pm

I need help with a angle problems

Post by TheFoxyHu3 »

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...
Attachments
game.love
game.love
(11.96 KiB) Downloaded 163 times
debug.lua
debug.lua
simplescreenrecorder-2024-10-19_16.26.55.gif (10.25 MiB) Viewed 2493 times
User avatar
marclurr
Party member
Posts: 158
Joined: Fri Apr 22, 2022 9:25 am

Re: I need help with a angle problems

Post by marclurr »

What's the problem?
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I need help with a angle problems

Post by dusoft »

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...
Provide the code and elaborate on what your exact problem is.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: I need help with a angle problems

Post by pgimeno »

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:

Code: Select all

local function angleInRange(a, A1, A2)
  A2 = (A2 - A1) % 6.283185307179586
  a = (a - A1) % 6.283185307179586
  return a <= A2
end
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.
Attachments
angleDiff.love
(569 Bytes) Downloaded 116 times
TheFoxyHu3
Prole
Posts: 5
Joined: Sun Mar 17, 2024 10:05 pm

Re: I need help with a angle problems

Post by TheFoxyHu3 »

dusoft wrote: Sun Oct 20, 2024 10:23 am
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...
Provide the code and elaborate on what your exact problem is.
Well, I'm trying to make enemies facing* the player.
enemies.lua
(4.52 KiB) Downloaded 108 times

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
The problem is, for example, when the player.x is > enemy.x and the player.y is between enemy.y positive and negative, the enemy stops to reach playrer
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I need help with a angle problems

Post by dusoft »

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.
TheFoxyHu3
Prole
Posts: 5
Joined: Sun Mar 17, 2024 10:05 pm

Re: I need help with a angle problems

Post by TheFoxyHu3 »

What do you mean, redefine the angle?
I don't know what to do...
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: I need help with a angle problems

Post by RNavega »

TheFoxyHu3 wrote: Sun Oct 20, 2024 8:18 pm What do you mean, redefine the angle?
I don't know what to do...
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.

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. Note that a ".love" file is just a renamed ZIP file, so to get to the code you need to download it and then either open with an unzipping tool like 7-Zip, or renaming it to .zip and extracting to some folder. The code is in "main.lua". (edit: You already know how to create a .love, disregard this.)
TheFoxyHu3
Prole
Posts: 5
Joined: Sun Mar 17, 2024 10:05 pm

Re: I need help with a angle problems

Post by TheFoxyHu3 »

Hello! Finally works. Thank you for everyone her for help me.
(I'm need to delete post? I don't use a forum ever...)
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: I need help with a angle problems

Post by RNavega »

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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 14 guests