Games Moving Enemy from right to left (Box2d - Tutorial)

ilan

Expert
Licensed User
hi

i had an issue yesterday with my box2d game and i want to share the solution.

the great thing in box2d is that you can create/destroy bodies, fixtures, joints...
and create new one and attach to each body

this will allow you to store 2 different fixtures (body shape) and attach them to a body when ever you want

for example you have a body that you dont want to use a simple rect or circle as shape (because of better collisions detection) so you create a body fixture (for example chain or polygon) and attach it to your enemy body.

but your enemy needs to go right and when it reached the limit turn back and go left and that body wont fit the animation you are drawing so you need a new fixture.

(example image:)

bodyshape.jpg


now what you can simply do is when the body position reached the limit you have choosen you destroy ONLY the fixture of the body and create immediately a new one and attach to your enemy box2d body

example code (first the drawing):

B4X:
            If enemy.getLinearVelocityFromWorldPoint(enemy.WorldCenter).x < 0 Then 'going left
                Batch.DrawRegion2(enemybird_framesFlip(0,enemyobj.frame Mod 6),vec2.x-(vpW*0.08),vec2.y-(vpH*0.01),vpW*0.13,vpH*0.17)
            Else
                Batch.DrawRegion2(enemybird_frames(0,enemyobj.frame Mod 6),vec2.x-(vpW*0.05),vec2.y-(vpH*0.01),vpW*0.13,vpH*0.17)
            End If

now updating the body fixture (if reached limits!)

B4X:
If vec2.x < enemyobj.limitA Then
                enemyobj.speed = enemyobj.speed * -1
                enemy.destroyFixture(enemy.GetFixture(0))
                Dim chainshape As lgBox2DChainShape
                chainshape.createChain(enemyobj.goleftVertices)     
                Dim chainShapeDef As lgBox2DFixtureDef
                chainShapeDef.shape = chainshape
                chainShapeDef.restitution=0
                chainShapeDef.Density = 1
                chainShapeDef.friction=0.8
                chainShapeDef.isSensor = True
                enemy.createFixture(chainShapeDef)
            else if vec2.x > enemyobj.limitB Then
                 enemyobj.speed = enemyobj.speed * -1
                enemy.destroyFixture(enemy.GetFixture(0))
                Dim chainshape As lgBox2DChainShape
                chainshape.createChain(enemyobj.gorightVertices)     
                Dim chainShapeDef As lgBox2DFixtureDef
                chainShapeDef.shape = chainshape
                chainShapeDef.restitution=0
                chainShapeDef.Density = 1
                chainShapeDef.friction=0.8
                chainShapeDef.isSensor = True
                enemy.createFixture(chainShapeDef)             
            End If 
            enemy.setLinearVelocity2(enemyobj.speed,0)

as you can see we store the vertices of the left and right fixture in a custom type (i do it when i load my body from tiled or body editor app)

and i attach the right fixture depending on the velocity of the enemy (-x goes left, +x goes right)

end result:


Have fun coding with box2d and b4a! :)
 
Top