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.

Newcomers DBPro Corner / 2D platform collisions?

Author
Message
HammerBro
13
Years of Service
User Offline
Joined: 14th Jan 2011
Location: Nowhere
Posted: 15th Jan 2011 03:01
Hello everyone.

I want to know how to get collision to work like in a 2D platformer,
I can make the character stop falling when he lands on top of a platform, but that's it.

And I also like to learn about slopes and rotating platforms.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 15th Jan 2011 15:15
Is this a 2D game using sprites? Or is this a 3D game, but you only move on the x and y axis? Both use very different methods, here's one for 3D:



This will also work with slopes. Moving objects requires more advanced methods. You'd have to get the object the player is currently colliding with, store the offset (object to player), and then position the player at the objects position with that offset. If you move the player, you'll have to apply the movement to the offset instead of the real-world coordinates until the player leaves the object again.

TheComet

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 15th Jan 2011 15:32
Hi Hammer,

Your question is very general. It's difficult to provide guidance without more information. If you can get your character to stop falling when he lands on a platform, what is it that you cannot do? Give us a specific scenario that you want to deal with, and we can help you with the logic.

Slopes and rotating platforms will be the more complicated elements to deal with. Sort out your flat platforms first before you try to tackle that stuff.
HammerBro
13
Years of Service
User Offline
Joined: 14th Jan 2011
Location: Nowhere
Posted: 15th Jan 2011 19:23
I'm working with sprites.

anyways, here is my collision code:



I trying to add something that prevents the player going through the platform horizontally without messing up the vertical collisions.
HammerBro
13
Years of Service
User Offline
Joined: 14th Jan 2011
Location: Nowhere
Posted: 25th Jan 2011 01:41
Bump.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 25th Jan 2011 02:39
If you're going to use actual collision commands in this way, then one way to prevent the character moving through objects is to check for a collision at the time you're checking to see if the movement key is being pressed.

Let's say you're using the arrow keys to move your sprite. When you do a check for rightkey() or whatever other method you're using to detect if the player is pressing the right arrow key, right before the code you use to move the sprite to the right, do a check first on the position he'll be moving to, to see if he'll be colliding with something he's not allowed to move through, like a platform. If the move would cause a collision, don't move the sprite. If he's not, move away!

You can create a dummy sprite that's hidden and doesn't get displayed for this. Move the dummy sprite to the anticipated location, and run a check on that dummy sprite to see if it collides with something. If not, move the actual sprite into position.

Login to post a reply

Server time is: 2024-11-23 03:53:54
Your offset time is: 2024-11-23 03:53:54