Page 1 of 1

Block collision - problems in detection

Posted: Tue Jul 24, 2012 12:08 pm
by Schbaltz
Helly guys,

I need help again, but now, i got a different problem.

I built a matrix 5x5 (just to test) to store my blocks (as u can see in the image). So, when i going to "walk" over this blocks, i got some undesirable collisions, it happens in each division of 2 or 3 blocks (left division and right division).
Anyone knows why it happens and how can i fix it?

PS: I thought about giving up the idea to use physics in my game, it's giving me much headache (whenerver i got an unexpected problem).

Re: Block collision - problems in detection

Posted: Tue Jul 24, 2012 4:04 pm
by ivan
Hi. First off let me just say that Box2D is probably not the best tool for side-scrollers especially if physics is not the main emphasis on your game. Box2D is great for physics gameplay were controls are generally loose. If you want super-tight controls Box2D will drive you insane.
Schbaltz wrote:I built a matrix 5x5 (just to test) to store my blocks (as u can see in the image). So,
I believe you meant to say 'array' instead of matrix. :)
when i going to "walk" over this blocks, i got some undesirable collisions, it happens in each division of 2 or 3 blocks (left division and right division).
Anyone knows why it happens and how can i fix it?
This is because your character's shape collides with the ledge of the box that's right next to it.

Code: Select all

        __
   <---/  \
__ ____\__/___
  |  B |  A |  
  |    |    |
Your character is standing on block A but it's also colliding with block B at the same time.
Box2D can't figure out the order in which these two collisions should be resolved.
What happens is, collision with B is resolved first, so your character is pushed to the RIGHT and then the collision with box A is resolved second so your character is pushed UP.
This is caused by the Box2D collision solver so you have to work around it.

The simplest solution is to create your platforms as a single shape rather than an array of boxes.

Another (uglier) approach is to create overlapping trapezoids instead of boxes for inner blocks:

Code: Select all

___ ___ ___
   /   \
  /_____\
Obviously corner blocks can't be trapezoids.
PS: I thought about giving up the idea to use physics in my game, it's giving me much headache (whenerver i got an unexpected problem).
Don't get discouraged. Physics and collisions is tightly related to game controls which is usually the most important thing in action games.
This is kinda tacky but I'll use the chance to plug my FizzX lib based on earlier project by Taehl. :)

Re: Block collision - problems in detection

Posted: Tue Jul 24, 2012 6:07 pm
by juno
Hey Schbaltz.
One solution is to use a circle instead of a rectangle. I change around your code a bit to do this.
I don't know how 'strict' you want the collision detection to be so maybe you would prefer the rectangle shape.

anyway...
The circle has setFixedRotation set to true so it rolls instead of slides over the blocks.
and i gave the body setAngularDamping( 1000 ) to make it slow down quicker.

hope this helps :)

Re: Block collision - problems in detection

Posted: Tue Jul 24, 2012 10:36 pm
by Schbaltz
Ivan,

Thx very much for your explanation, my game needs sigle blocks because it's "breakable" (like terraria)...

Sorry my bad english, brazillian here :3

Juno,

This is a good solution man! i change my code to:

Code: Select all

instance.shape = love.physics.newCircleShape(instance.bounds.width / 2,instance.bounds.height / 2,instance.bounds.height / 2)
	
instance.body:setAngularDamping(1000)
It works fine!!!

I'm so excited with this game, i have many ideas to implement... the next step is create "breakable" blocks, but this is easy to do. ASAP i'll post the results in the "projects" topic...

Thx very much guys!!!

Re: Block collision - problems in detection

Posted: Wed Jul 25, 2012 12:26 am
by juno
Glad to help :)

Re: Block collision - problems in detection

Posted: Wed Jul 25, 2012 4:37 am
by ivan
One of the problem cases with circle collision shapes is when your character reaches the end of a platform and you change direction back towards the middle of the platform. Since you're using a circle with fixed rotation the shape will get stuck on the corner of the platform with his/her legs dangling in the air. :)

Code: Select all

      ___  circle
 <-- /   \
-----|   |
     \___/
 box | 
     |