[SOLVED] Math + angle + following object on click

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.
User avatar
pgimeno
Party member
Posts: 1115
Joined: Sun Oct 18, 2015 2:58 pm

Re: [in progress] Math + angle + following object on click

Post by pgimeno » Fri May 04, 2018 2:18 pm

Good job getting it to work :)
var77 wrote:
Fri May 04, 2018 8:14 am
Then i just wondering one last question on the result i got a float for user.position
while mouse.position seems more an integer. Guess it's maybe caused by the "dt"
that's not involved in dot product ? What's your point of view ?
That's normal. Mouse position is in pixels, and the mouse cursor can't be between two pixels.

Player position, however, can. It's both because of dt and because of the angle. Imagine you have to move 17 pixels right and 1 pixel up. *Even* if the X coordinate moved in integer steps of say, 1 pixel every frame, then the Y should move 1/17 pixel every frame. If your positions were integers and did not have a fractional part, you wouldn't know when you have exceeded the threshold that makes the vertical position change. (Bresenham's algorithm tracks using integers what the fractional part should be, but the essence is the same.)

Also, the distance is rarely integer. In the above example, 17 is only the horizontal distance, but since the vertical distance is not zero, the actual total distance is a bit over 17: it's sqrt(17²+1²) which is approximately 17.029386. Even if you walked there in perfect integer distances, you would always overshoot, because after walking 17 steps, you would not be there yet and you would need another step.

Finally, you're doing cumulative floating point operations, and each of them is rounded to the closest number representable in floating point, so they are subject to rounding errors, and you can't expect to land exactly in the final position for that reason alone (unless each operation produces an exact floating point number that doesn't need to be rounded, which in this case doesn't seem to be possible).

If you don't like how the texture is drawn at non-integer coordinates, you can round the sprite position in the draw command.

If you want to show the numbers for debugging purposes, you can use string.format:

Code: Select all

-- change this:
love.graphics.print(user.x.."/"..user.y.." ! "..tostring(movev), screen.w /2, screen.h /20)
-- to this:
love.graphics.print(string.format("%.2f/%.2f ! %s", user.x, user.y, tostring(movev)), screen.w /2, screen.h /20)
Also, I suggested that when it overshoots, it assigns the target position to the player's position, like this:

Code: Select all

        if dot <= 0 then
                movev = false
                user.x, user.y = mpX, mpY
        end
That way, the final position is exact. That won't be visible as going back, because you haven't drawn the player at the overshot position yet.
Last edited by pgimeno on Fri May 04, 2018 2:51 pm, edited 1 time in total.

var77
Prole
Posts: 15
Joined: Wed May 02, 2018 1:57 pm

Re: [in progress] Math + angle + following object on click

Post by var77 » Fri May 04, 2018 2:49 pm

Again great explanation, mouse can't be between two pixel also logic. That's also part of what
i thought about the float but wanted to be sure that wasn't a buggy stuff. And the last part
code (user.x, user.y = mpX, mpY) seems so obvious when you take time to think of it.

Thanks for all help, subject solved !
:awesome:

PM question:
I still got one strange point. If i click on the exact position of the player it's seem to desapear i don't know why ?
main.7z
(1.5 MiB) Downloaded 2 times
Last edited by var77 on Thu Jun 14, 2018 6:49 pm, edited 1 time in total.

User avatar
pgimeno
Party member
Posts: 1115
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Math + angle + following object on click

Post by pgimeno » Thu Jun 14, 2018 5:02 pm

Please post your follow-up questions in the forum.

You don't need to open a new topic if it's still related; you can just use the old one and click "Post Reply" or "Full Editor & Preview" and you can attach images again.

Clicking on the exact position of the car causes a division by zero, that's why your car disappears. After this line:

Code: Select all

dist = math.sqrt((mpX - user.x)^2 + (mpY - user.y)^2)
check whether dist is 0 before dividing. That will happen when mpX equals user.x and mpY equals user.y, i.e. when you click in the exact position of the car.

In these conditions, atan2 will return 0 too, making your car point East, so you may want to correct that as well.

var77
Prole
Posts: 15
Joined: Wed May 02, 2018 1:57 pm

Re: [SOLVED] Math + angle + following object on click

Post by var77 » Thu Jun 14, 2018 7:05 pm

Thanks pgimeno that solved my last issue on this topic.

Condition for the distance:

Code: Select all

if dist ~= 0 then ...
and also for the atan2 = 0:

Code: Select all

if love.mouse.isDown(1) then			
	mpX = love.mouse.getX()
	mpY = love.mouse.getY()			
	user.sx = user.x --start user.x position
	user.sy = user.y --start user.y position
	if user.sx ~= mpX and user.sy ~= mpY then
		user.angle = math.pi/2 + math.atan2(mpY - user.y, mpX - user.x)	
	end
end
Thanks again and sorry for the PM :)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests