How to handle movement - new to löve

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
much.love
Prole
Posts: 4
Joined: Tue Apr 10, 2012 12:13 pm

How to handle movement - new to löve

Post by much.love »

Hello there.

I just started using löve a few days ago and I just love it. I'm in the process of writing a pong clone and am almost done. Actually, it's finished, but I was wondering, if there was any better way to handle movement. Right now, all movements are handled this way:

Code: Select all

if ball.move == 0 then
	ball.x = ball.x + ball.speed*dt*ball.dirx
	ball.y = ball.y + ball.speed*dt*ball.diry
end
The problem is, the ball moves multiple pixles at once, which leads to problems when it comes to collision. I can't really explain it any better, so I tried to draw a picture. :)
s6WHH.png
s6WHH.png (5.09 KiB) Viewed 476 times
lovely regards
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to handle movement - new to löve

Post by Jasoco »

You need to use Delta Time. That's what the "dt" in the love.update(dt) function is for. It tells you the time since the last frame. So to move something at 100 pixels per second, you'd add 100 * dt to the position.

In order to handle collision, you need to first check the X and Y you want to move to for a collision instead of moving it and then checking. So you'd check first, find out whether it's collided or not, then either A) move the object to the new position if there's nothing there, or B) figure out where it can be moved to.

It's also best to check the X and Y separately I guess.

Are you using Box2D or coding it yourself? Can we have more code to see overall?

Greetings and welcome!
User avatar
much.love
Prole
Posts: 4
Joined: Tue Apr 10, 2012 12:13 pm

Re: How to handle movement - new to löve

Post by much.love »

Hello again and thanks a lot for your fast reply. :)

I already use "dt", it seems like you did not see it, which might be my fault for not practising good coding style...should I put the dt always on the end? Like

Code: Select all

   ball.x = ball.x + ball.speed*ball.dirx*dt
instead of

Code: Select all

   ball.x = ball.x + ball.speed*dt*ball.dirx
I coded collision myself and it works just fine for the most part. Especially collision with the pong-paddles works just fine, but it doesn't really look nice if part of the ball is in the paddle before flying back and using my methode, the ball gets reflected, even if it aleady passed the paddle, but didn't quite reach the end of the scrren just jet. Again, I can't explain it any better (sadly english is not my native language), so I made a picture. :)
Collision with the top and the bottom of the screen sometimes leads to the ball bein "caught" and sort of being stuck.

I also attached the whole game, but my code is probably a mess. I never programmed andything in lua before and only know some Pascal back from high school.

Edit: I accidently the picture: download/file.php?mode=view&id=22584
Attachments
lSkiC.png
lSkiC.png (2.21 KiB) Viewed 477 times
pong-clone.love
(2.88 KiB) Downloaded 161 times
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: How to handle movement - new to löve

Post by Santos »

much.love wrote:I coded collision myself and it works just fine for the most part. Especially collision with the pong-paddles works just fine, but it doesn't really look nice if part of the ball is in the paddle before flying back and using my methode, the ball gets reflected, even if it aleady passed the paddle, but didn't quite reach the end of the scrren just jet.
Your collision checks whether the two rectangles are overlapping just fine, but this probably isn't what you want. :D One way to achieve what you want might be to give the ball a boolean variable which determines if it should check for collisions or not. For example, if the left side of the ball moves past the right side of the left paddle and they haven't collided, the paddle has already been beaten and the variable can be set to false to let the ball move off the screen.
much.love wrote:Collision with the top and the bottom of the screen sometimes leads to the ball bein "caught" and sort of being stuck.
This is caused by the ball switching y directions when it's off the screen, moving for the next frame and still being off the screen, therefore switching directions again! :D

One possible way to remedy this is by changing this:

Code: Select all

if ball.y <= 0 or ball.y >= (600-ball.height) then
		ball.diry = ball.diry*(-1)
	end
To this:

Code: Select all

if ball.y <= 0 then
		ball.diry = math.abs(ball.diry)
	end

	if ball.y >= (600-ball.height) then
		ball.diry = -math.abs(ball.diry)
	end
math.abs() will return the absolute value of the number (for example, math.abs(123) and math.abs(-123) will both return 123). If the ball moves off the up of the screen, math.abs(ball.diry) will definitely return a positive number, and if it moves off the bottom, -math.abs(ball.diry) will definitely return a negative number.

I hope this helps! ^^
User avatar
Codex
Party member
Posts: 106
Joined: Tue Mar 06, 2012 6:49 am

Re: How to handle movement - new to löve

Post by Codex »

I wrote a quick pong collision program a few weeks ago.

http://codepad.org/qs13jZmH

Maybe looking at the code will give you some ideas? I had two different kinds of collision, one that teleported the pong ball to the other side of the screen and another that bounced within the screen... :awesome:
User avatar
much.love
Prole
Posts: 4
Joined: Tue Apr 10, 2012 12:13 pm

Re: How to handle movement - new to löve

Post by much.love »

Hello again.

@Santos: Your solution for the collision with the top and bottom of the screen works perfectly. Thanks a lot.

@Codex: Thanks for your help, but to me it seems like your solution is basically the same as mine. Maybe I miss something?

As to the collision with the paddles, my ideal solution would not be to check if the rectangles overlap but instead check if ball and paddle touch each other.

More like

Code: Select all

	if ball.x == player1.x+player1.width then
instead of

Code: Select all

	if ball.x <= player1.x+player1.width then
But this is not possible due to the nature of the ball-movement, which has a velocity greater than 1 pixel per frame, which leads to the ball "skipping" the border of the paddle.

best regards
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: How to handle movement - new to löve

Post by Santos »

I'm glad I could help!

Here is what I came up with...

Code: Select all

if ball.direction == 'left' and ball.x <= (player1.x+player1.width) and not ball.out then
	if (ball.y+ball.height) >= player1.y and ball.y <= (player1.y+player1.height) then
		ball.direction = 'right'
		ball.speed = ball.speed + 15
	else
		ball.out = true
	end
end
Which is basically...

Code: Select all

If the ball has gone past the player:
	And if the ball hits the player:
		Bounce the ball back!
	Otherwise:
		Let the ball go out by not checking any of this again! :D
caldur
Prole
Posts: 20
Joined: Tue Mar 13, 2012 3:30 pm

Re: How to handle movement - new to löve

Post by caldur »

Well I guess what you mean is that the collision check is not 100% accurate, right?
The thing is with a normal computer and normal human being, seriously, it doesn't make difference, because players can't tell, really.
Of course you can use some projection type of collision detection mechanism to "predict" the collision and thus get more accurate results, but then there's that trade off between accuracy and performance/complexity.
Hope that helps, and pls correct me if I'm wrong : P
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests