Physics & Camera - Infinite floor

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.
Post Reply
Albright
Prole
Posts: 7
Joined: Sat Jun 23, 2012 1:43 pm

Physics & Camera - Infinite floor

Post by Albright »

Hello.

I'm hacking together something where I've got a ball which gets launched as if out of a cannon, and a floor the ball bounces on until it comes to a stop - it's basically an adaptation of the physics tutorial in the wiki. I've added a "camera" to keep the ball in frame based on what's in this tutorial video - basically calling love.graphics.translate() before drawing the ball.

The issue I'm having is that the floor the ball bounces on gets moved out of frame, so after the ball moves out of the first "screen" the ball has nothing to bounce on anymore. I can of course make the ground wider, but in my ultimate game it will be theoretically possible for the ball to keep flying/bouncing forever (eventually there will be obstacles and such to stop them) - recall the old Nanaca Crash Flash game if that helps.

There's three solutions I can think of;

One - I constantly reposition the ball "sprite" so that its X value doesn't actually change, but reposition other elements in the game field so that the appearance of movement is persisted. I don't like this idea since it seems like it'd be a lot of work and not worth using physics or camera concepts in the first place.

Two - I constantly reposition the floor to always be under the ball depending on what the X position of the ball becomes after physics are applied. This seems like a relatively simple approach, but I'm not sure if it's the best, because maybe there's…

Three - Some part of the Physics module I'm not aware of which lets you place an infinite "floor" or other border in a world.

What would be the best approach for this sort of thing? Thanks in advance for your feedback.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Physics & Camera - Infinite floor

Post by pgimeno »

I recommend you to avoid trying to implement approach 1. That clashes with the way the physics world is designed to work.

You could have two floor rectangles. It shouldn't be hard to update one so that when the other goes out of the screen, it is placed in the next spot. It's very similar to the technique applied to tile rendering, where you only draw the tiles that are in the screen.

I don't recommend having one single moving floor, mainly because the interaction of the ball with the floor may not be physically realistic if it moves.
Albright
Prole
Posts: 7
Joined: Sat Jun 23, 2012 1:43 pm

Re: Physics & Camera - Infinite floor

Post by Albright »

pgimeno wrote: Thu Sep 30, 2021 9:54 pm You could have two floor rectangles. It shouldn't be hard to update one so that when the other goes out of the screen, it is placed in the next spot. It's very similar to the technique applied to tile rendering, where you only draw the tiles that are in the screen.
Ah, good thinking. After a bit of experimentation, I got that working fine. Thanks.

There's one tiny issue; once the ball stops bouncing and is just rolling on the floor, when it rolls over a "seam" between the two floor rectangles, it sometimes (maybe once in ten times) does another small bounce as if it were hitting a small ramp there. It should be less noticeable once I implement the friction to slow the forward momentum of the ball as it touches the floor, so I might just choose to ignore it for now.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 43 guests