Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Tiles and collision

Author
Message
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 11th Jul 2012 15:46
I have problems with the physics collision in my tile based platformer. Each tile has it's own collision box and the player has one. I use set velocity to move the player. Sometimes the player gets stuck moving over a row of tiles. Anyone got some tips on this?

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 11th Jul 2012 17:44
Often, all you can really do is make a loop, moving the sprite out a little at a time until it's free.

Last time I did this, I set a radius of 1 pixel, and starting at angle 0, I stepped the angle by 45, then when the angle wraps around to 0 again, I increase the radius.

Say....px,py is the player position, and collision(x,y) returns the collision state

col=1
a=0
r=1
while col=1
x=px+sin(a)*r
y=py+cos(a)*r
if collision(x,y)=0 then col=0
a=a+45
if a>359 then a=0 : r=r+1
wend
px=x
py=y

That will kinda spin the sprite from it's start position, 1 pixel up and no pixels along - and it'll wind round and round until it finds some free space. It will favor the top of the platforms, as the angle is set to 0 as the range increases.

It might also be worth storing the last free position of the object - and if all else fails, set the player back to that. For example, you might want to limit the range of pixels, or you might want to kill the player if the range is more than 3 or 4 pixels - so they still die if they get properly stuck, but little glitches shouldn't bother it.

Health, Ammo, and bacon and eggs!
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 11th Jul 2012 18:19
The player sprite doesn't rotate and I'm not moving or checking the collision myself, just letting AppGameKit handle it all. Considering reporting this as a bug.

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 11th Jul 2012 22:08 Edited at: 11th Jul 2012 22:11
I had a similar problem with a platformer I was working on. If you use the setphysicsdebugmodeon command (or something similar, that's off the top of my head), you will see your collision boxes. You will probably find there are small gaps that the player can get stuck on the tile. I only had this problem with diagonal tiles though not flat ones. But you should see exactly why it is getting stuck.

Edit - Thinking back, I ended up getting the diagonal floors to ignore the main sprite, and used a small sphere collision zone at his feet instead. That sorted it out nicely, as the sphere rolls over the overlap areas without getting stuck at all. You may want to try a similar thing.

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 11th Jul 2012 22:45
Hmm, maybe I should try making a polygon shape with angled corners. Thing is I will use the same for enemies as well and I hope it's not too slow. Maybe I can come up with a shape that will solve all my problems. Thanks for the tip.

Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 13th Jul 2012 22:18
Quote: "maybe I should try making a polygon shape with angled corners. Thing is I will use the same for enemies as well and I hope it's not too slow."


In Box2D there are only two shapes, polygon and sphere, so AppGameKit uses a 4 sided polygon to implement the box shape. Therefore you shouldn't see too much of an issue going to a 5 or 6 sided shape to round off the corners.
apocolyp4
13
Years of Service
User Offline
Joined: 10th Jun 2010
Location:
Posted: 14th Jul 2012 00:40 Edited at: 14th Jul 2012 00:46
Having each tile to have it's own collision box isnt a good solution when combining it with box2D. The reason is due to accuracy problems with floating point numbers. Even though the appear to be the same (in this case the y-axis position of the tile) they may actually be different which is why your character is getting stuck as 1 tile is slightly higher up than the other even though you cant see it, and the returned y position values match.

This isnt a bug due to the AppGameKit or box2D it is just an issue when dealing with floating point numbers in general.

One solution is for your game to detect groups of tiles and create the collision box for the group. For example drawing one collision box for an entire platform.
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 14th Jul 2012 19:14
Paul:
Thanks for the info.

apocolyp4:
I would also suspect the floating point values. However, I don't think that should be a problem in a physics system. It makes no sense why it isn't made to deal with it if you ask me.

I like your other idea. It wouldn't be too hard to loop through the level array row by row creating collision boxes. We'll see what method I will end up using.

Login to post a reply

Server time is: 2024-05-04 11:52:34
Your offset time is: 2024-05-04 11:52:34