Detecting collision on a particular side of a rectangle/display object?

Hi All,

I’m prototyping a platformer style game and am using collision to detect when it’s possible for the PC to jump. Is there a way to specify this for only the “top” side of a rectangle/image display object? Currently my character can not only jump off the floors but also the walls.

Thanks :slight_smile: [import]uid: 105707 topic_id: 32176 reply_id: 332176[/import]

Hi there,

Happy to try to help, but could you clarify a bit more the use case you have in mind? It sounds like you want your player to be able to jump when he is standing on a rectangle, and you want to detect that by tracking that he is currently colliding with the “top” of the rectangle. But then you mentioned your player could also jump off of walls, which sounds to me like you’d want to detect whether he is colliding with the “side” of the rectangle, but you didn’t mention that, so I wasn’t sure.

  • Andrew [import]uid: 109711 topic_id: 32176 reply_id: 128108[/import]

Hi Andrew,

I think you were following me. I’m using rectangular cubes to block out the walls, ground and platforms of my “level”. I’m currently using a collision listener to determine if my player can jump. This as intended when he’s standing on a cube. The problem is that it also lets the player jump if he’s already jumped up and brushed the side of a platform or wall (the side of a cube).

Is there a way to detect which face of a rectangle one has collided with? Or is there a better way to solve the problem of being able to jump off the top of a cube but not off its side?

Thanks! :slight_smile: [import]uid: 105707 topic_id: 32176 reply_id: 128112[/import]

Hi EHO,
It’s ironic… the 2D platformer is ages old in design, but it presents some of the most difficult coding issues! For this, I suggest that your platforms are actually multi-part physics objects. This being done, you can sense which “part” of the platform has been collided with, and react appropriately.

The alternative is detecting, upon collision, the position/size of the platform and the position/size of the player, and calculating if he is “on top” of the platform. If the Y base (feet) of the player is <= the platform’s top surface Y, you can assume he’s standing on it. I can’t vouch for how reliable this method would be, but it’s an alternative you can consider.

Brent [import]uid: 9747 topic_id: 32176 reply_id: 128121[/import]

Hey, Brent is totally right. There would normally be a better any of doing this with the physics engine but there is a bug in the collision code which fails to report the location of a collision:

https://developer.coronalabs.com/forum/2012/10/03/collision-location-appears-incorrect

Your best bet is to either use multiple bodies per platform or even add a sensor object near the platform, assuming that a simple “if p.y > f.y then” is not enough. [import]uid: 8271 topic_id: 32176 reply_id: 128127[/import]

Hey, Brent is totally right. There would normally be a better any of doing this with the physics engine but there is a bug in the collision code which fails to report the location of a collision:

https://developer.coronalabs.com/forum/2012/10/03/collision-location-appears-incorrect

Your best bet is to either use multiple bodies per platform or even add a sensor object near the platform, assuming that a simple “if p.y > f.y then” is not enough. [import]uid: 8271 topic_id: 32176 reply_id: 128128[/import]

Hi there,

Happy to try to help, but could you clarify a bit more the use case you have in mind? It sounds like you want your player to be able to jump when he is standing on a rectangle, and you want to detect that by tracking that he is currently colliding with the “top” of the rectangle. But then you mentioned your player could also jump off of walls, which sounds to me like you’d want to detect whether he is colliding with the “side” of the rectangle, but you didn’t mention that, so I wasn’t sure.

  • Andrew [import]uid: 109711 topic_id: 32176 reply_id: 128108[/import]

Hi Andrew,

I think you were following me. I’m using rectangular cubes to block out the walls, ground and platforms of my “level”. I’m currently using a collision listener to determine if my player can jump. This as intended when he’s standing on a cube. The problem is that it also lets the player jump if he’s already jumped up and brushed the side of a platform or wall (the side of a cube).

Is there a way to detect which face of a rectangle one has collided with? Or is there a better way to solve the problem of being able to jump off the top of a cube but not off its side?

Thanks! :slight_smile: [import]uid: 105707 topic_id: 32176 reply_id: 128112[/import]

Hi EHO,
It’s ironic… the 2D platformer is ages old in design, but it presents some of the most difficult coding issues! For this, I suggest that your platforms are actually multi-part physics objects. This being done, you can sense which “part” of the platform has been collided with, and react appropriately.

The alternative is detecting, upon collision, the position/size of the platform and the position/size of the player, and calculating if he is “on top” of the platform. If the Y base (feet) of the player is <= the platform’s top surface Y, you can assume he’s standing on it. I can’t vouch for how reliable this method would be, but it’s an alternative you can consider.

Brent [import]uid: 9747 topic_id: 32176 reply_id: 128121[/import]

Hey, Brent is totally right. There would normally be a better any of doing this with the physics engine but there is a bug in the collision code which fails to report the location of a collision:

https://developer.coronalabs.com/forum/2012/10/03/collision-location-appears-incorrect

Your best bet is to either use multiple bodies per platform or even add a sensor object near the platform, assuming that a simple “if p.y > f.y then” is not enough. [import]uid: 8271 topic_id: 32176 reply_id: 128127[/import]

Hey, Brent is totally right. There would normally be a better any of doing this with the physics engine but there is a bug in the collision code which fails to report the location of a collision:

https://developer.coronalabs.com/forum/2012/10/03/collision-location-appears-incorrect

Your best bet is to either use multiple bodies per platform or even add a sensor object near the platform, assuming that a simple “if p.y > f.y then” is not enough. [import]uid: 8271 topic_id: 32176 reply_id: 128128[/import]

Hi horacebury - so if I’m following you, I can’t determine the location of the collision it’s self? I was thinking of comparing the y value of the player against the y value of the object (accounting for my cell width/obstacle width constant) so it sounds like that’s still an option. Glad to know about the location of the collision being weird though on the chance I had started going with that.
Brent - I appreciate your recommendations. I had considered comparing the locations of the player and object as you suggest. I hadn’t considered multi-part physics objects. Since I’m using an environment made of many 20px x 20 px cubes layed out to build up walls/platforms, I might consider making the player a multi part physics object instead. Does that sound reasonable?

Thanks everyone for your input :slight_smile: [import]uid: 105707 topic_id: 32176 reply_id: 128290[/import]

Yep, certainly you want to assign mutli-part physics bodies to the player if possible, not the solid objects. You’d need to make at least one “feet” object if the player is something like a jumping Mario, and a “head” object, maybe even “body” and “arms” too. Then, you’ll need to do alot of post-collision processing (conditional checks, positional checks between the player and what it hits, etc.) to determine what action to take.

Again, it’s ironic that 2D platformer movement remains one of most tricky things in modern 2D game coding! One of my eventual goals is to use Corona to resurrect an old 2D platformer game concept that I started making many years ago in a now-ancient language (Basic!), but as I truly consider doing that, I think maybe the core idea could be converted to a 2D top-down view to avoid the complexities of gravity, “only sense from the surface of the platform”, etc.

I’m not trying to dissuade you from your game idea, just pointing out, it’s tricky to fine-tune 2D movement and collision of this sort. :slight_smile:

Brent
[import]uid: 9747 topic_id: 32176 reply_id: 128300[/import]

Another quick follow-up… what Horacebury meant (I think) is that you can’t pinpoint the collision location between two bodies unless your bodies are relatively simple (i.e. two circles, two rectangles) and you use a proven mathematical formula to figure out the point.

Say you have two balls rolling toward each other on a billiard table. If they collide, you can get the X/Y value of each ball, but it’s harder to determine the POINT (on the table, or the screen in this case) where the balls strike each other. You’d need to use math to figure it out, dependent on the location of each ball, their radius, etc. It’s probably not insanely difficult for somebody with a solid foundation in geometry and trig, but it’s not something I’ve attempted to do before.

Brent
[import]uid: 9747 topic_id: 32176 reply_id: 128306[/import]

Yes, Brent is right; Though I was picturing something like multi-bodies or irregular shapes - Box2D has the ability to report the collision point as it obviously needs to calculate this information to do its job. It’s just that Corona doesn’t return it properly. [import]uid: 8271 topic_id: 32176 reply_id: 128333[/import]

Thank you both for the clarification regarding the impact point. That does seem essential to doing collision based physics!

That is interesting how deep the topic of platform collision is for such a staple of so many 2D games.

Without having attempted anything yet, I’m leaning towards an approach that checks whether (player.height + object.height)/2 >= object.y - player.y if player.y is less than (located above) object.y

I have made a nice top down 2D game prototype and may shift the focus of my effort back to that since I’ve also been grappling with how to best handle jumping in a platformer:
http://developer.coronalabs.com/forum/2012/10/22/approach-handling-player-movement-platformer-style-game
The challenge I have with my 2D game is that I’m using individual storyboard scenes for each “room” of my world and I get a small memory increase (~8kb) with each new scene I visit. Not a problem for 10 or 20 screens worth of gameplay but not do able for the 100 to 200 that I’d like to be able to use…
http://developer.coronalabs.com/forum/2012/10/20/storyboard-scenes-and-memory-use
[import]uid: 105707 topic_id: 32176 reply_id: 128337[/import]

Btw, you could also check the velocity of the player’s object - ie: if its moving up, let it pass through the platform, if it’s moving down, don’t. [import]uid: 8271 topic_id: 32176 reply_id: 128386[/import]

Hi horacebury - so if I’m following you, I can’t determine the location of the collision it’s self? I was thinking of comparing the y value of the player against the y value of the object (accounting for my cell width/obstacle width constant) so it sounds like that’s still an option. Glad to know about the location of the collision being weird though on the chance I had started going with that.
Brent - I appreciate your recommendations. I had considered comparing the locations of the player and object as you suggest. I hadn’t considered multi-part physics objects. Since I’m using an environment made of many 20px x 20 px cubes layed out to build up walls/platforms, I might consider making the player a multi part physics object instead. Does that sound reasonable?

Thanks everyone for your input :slight_smile: [import]uid: 105707 topic_id: 32176 reply_id: 128290[/import]

Yep, certainly you want to assign mutli-part physics bodies to the player if possible, not the solid objects. You’d need to make at least one “feet” object if the player is something like a jumping Mario, and a “head” object, maybe even “body” and “arms” too. Then, you’ll need to do alot of post-collision processing (conditional checks, positional checks between the player and what it hits, etc.) to determine what action to take.

Again, it’s ironic that 2D platformer movement remains one of most tricky things in modern 2D game coding! One of my eventual goals is to use Corona to resurrect an old 2D platformer game concept that I started making many years ago in a now-ancient language (Basic!), but as I truly consider doing that, I think maybe the core idea could be converted to a 2D top-down view to avoid the complexities of gravity, “only sense from the surface of the platform”, etc.

I’m not trying to dissuade you from your game idea, just pointing out, it’s tricky to fine-tune 2D movement and collision of this sort. :slight_smile:

Brent
[import]uid: 9747 topic_id: 32176 reply_id: 128300[/import]

Another quick follow-up… what Horacebury meant (I think) is that you can’t pinpoint the collision location between two bodies unless your bodies are relatively simple (i.e. two circles, two rectangles) and you use a proven mathematical formula to figure out the point.

Say you have two balls rolling toward each other on a billiard table. If they collide, you can get the X/Y value of each ball, but it’s harder to determine the POINT (on the table, or the screen in this case) where the balls strike each other. You’d need to use math to figure it out, dependent on the location of each ball, their radius, etc. It’s probably not insanely difficult for somebody with a solid foundation in geometry and trig, but it’s not something I’ve attempted to do before.

Brent
[import]uid: 9747 topic_id: 32176 reply_id: 128306[/import]