Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Contact class problem — Gideros Forum

Contact class problem

xxRVxxxxRVxx Member
edited June 2014 in Game & application design
Hi, I'm new to Gideros, programming and making apps, and this is my first post on this forum, although I've been reading it for a while. Till now I've found answers to all my questions, but now I'm stuck ;(

I'm using box2d, I created an actor with dynamic body:

actorBody = myWorld:createBody{
type = b2.DYNAMIC_BODY,
position = {x=120, y=10}

}

and a fixture with the following shape:

actorShape = b2.PolygonShape.new()
actorShape:setAsBox(actorWidth/2, actorHeight/2, actorWidth/2, actorHeight/2, 0)

Then I created a ground with static body:

groundBody = myWorld:createBody{
type = b2.STATIC_BODY,
position = {x=0, y=0}

}

and a fixture with the following shape

groundShape = b2.ChainShape.new()
groundShape:createChain(
-44,0,
-44,240,
524,240,
524,0
)

it has a U shape, so that body doesn't fall from screen on the sides. Both actor and ground were created with restitution = 0.0 so that the actor doesn't bounce.

I want my actor to jump only when he's touching ground, so I'm using the Contact class as seen on the Reference and forums:

actorTouchingGroung = false

function beginContact(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
if bodyA == groundBody and bodyB == actorBody then
actorTouchingGroung = true
end
end

myWorld:addEventListener(Event.BEGIN_CONTACT, beginContact)

function endContact(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
if bodyA == groundBody and bodyB == actorBody then
actorTouchingGroung = false
end
end

myWorld:addEventListener(Event.END_CONTACT, endContact)

Then the actor only jumps if actorTouchingGroung = true. By the way, ground's body and fixture are created after actor's body and fixture, and the ground's fixture is always assigned to e.fixtureA, is this the logic behind the Contact class or just coincidence?

The code above works fine, actor only jumps when he's touching ground, until I move the actor to touch any of the sides of my ground and then walk away from it, then he doesn't jump any more!

It seems that if the actor touches one side and walks away, Event.END_CONTACT is triggered, so actorTouchingGroung is set to false, is this expected behavior? If yes, what do I have to change to avoid that behavior?

Probably I'm doing everything bad as this is my first game ever!

Please your comments are welcome! and sorry for the long post.

Comments

  • xxRVxxxxRVxx Member
    Now I've modified the ground, so that there is a square in the center of the screen:

    groundShape = b2.ChainShape.new()
    groundShape:createChain(
    -44,0,
    -44,240,

    230, 240,
    230, 210,
    260, 210,
    260, 240,

    524,240,
    524,0
    )

    but jumping the actor over this square gives all kind of weird behaviors, like the actor bouncing back, or then flying in circles instead of jumping!

    Somewhere I read we should use box2d for a platformer, but for me it's not working at all! Is there any tutorial on using box2d for a platformer?
  • ar2rsawseenar2rsawseen Maintainer
    Hello @xxRVxx
    1) yes that is the logic behind the contact class, the sequence of fixtures as they were created correspondents to the sequence of what is retrieved in contact event

    2) I think what you need is to determine, when collision come from the bottom, and only then mark touching the ground as true
    It can be done through using the normal vector of collision information
    you can check a usage example provided here:
    http://docs.giderosmobile.com/reference/physics/b2WorldManifold#b2.WorldManifold

Sign In or Register to comment.