Block collision - problems in detection

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
Schbaltz
Prole
Posts: 14
Joined: Fri Jul 20, 2012 12:21 am

Block collision - problems in detection

Post 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).
Attachments
MyFarmFactory.love
(148.07 KiB) Downloaded 157 times
MFF.png
MFF.png (199.85 KiB) Viewed 3077 times
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Block collision - problems in detection

Post 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. :)
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: Block collision - problems in detection

Post 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 :)
Attachments
MyFarmFactory.love
(152 KiB) Downloaded 174 times
wat ya mean she's in another castle!?
Schbaltz
Prole
Posts: 14
Joined: Fri Jul 20, 2012 12:21 am

Re: Block collision - problems in detection

Post 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!!!
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: Block collision - problems in detection

Post by juno »

Glad to help :)
wat ya mean she's in another castle!?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Block collision - problems in detection

Post 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 | 
     |
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests