I wanted to have great collision detection so I created this class.
Setup:
First make sure you have a sprite animation and seperate each frame in a seperate file.
Then load all the images into Physics body editor
Draw all required polygons
Save the project into your B4A files folder and make sure to include the images.
You can double check the JSON file to see if the image filepath is not screwed.
Then load the json file with below code.
This is a work in progress and will be made more generic in the future, but it will give you an idea how it works.
Basically it uses the lgBox2DBodyEditorLoader and based on the deltatime it draws the current frame.
It removes all the fixtures from the body and loads the fixture belonging to the current frame to the body.
Feel free to comment and/or provide optimisation tips.
Note:
Main.P2M is my pixel to meter value.
Setup:
First make sure you have a sprite animation and seperate each frame in a seperate file.
Then load all the images into Physics body editor
Draw all required polygons
Save the project into your B4A files folder and make sure to include the images.
You can double check the JSON file to see if the image filepath is not screwed.
Then load the json file with below code.
This is a work in progress and will be made more generic in the future, but it will give you an idea how it works.
Basically it uses the lgBox2DBodyEditorLoader and based on the deltatime it draws the current frame.
It removes all the fixtures from the body and loads the fixture belonging to the current frame to the body.
Feel free to comment and/or provide optimisation tips.
Note:
Main.P2M is my pixel to meter value.
B4X:
'Class module
Sub Class_Globals
Dim Batch As lgSpriteBatch
Dim Bodie As lgBox2DBody
Dim Textures(6) As lgTexture
Dim Sprites(6) As lgSprite
Dim scale As Float = 0.5
Dim Conversion As lgMathUtils
Dim Loader As lgBox2DBodyEditorLoader
Dim fd As lgBox2DFixtureDef
Dim StateTime As Float = 0
Dim TimePerFrame As Float = 0.1
Dim CurrentFrame As Float = 0
Dim TotalFrames As Float = 6
Dim L As List
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(world As lgBox2DWorld)
'add duration of the total sprite duration or time per frame.
Batch.Initialize
Log ("scaling = " & scale)
Loader.InitializeWithFile("actors/plane.json")
Dim L As List = Loader.SortedList
'1. Create a BodyDef, as usual:
Dim bd As lgBox2DBodyDef
bd.Position.Set(5, 5)
bd.Type = world.BODYTYPE_Kinematic
'2. Create a FixtureDef, as usual:
fd.Density = 1
fd.Friction = 0.5
fd.Restitution = 0.2
'3. Create a Body, as usual:
Bodie = world.CreateBody(bd)
'5. Load the associated image:
' First load the images as we need it's width to set the scaling in the bodyfixture.
For i = 0 To L.Size -1
Textures(i).Initialize("actors/" & Loader.GetImagePath(L.Get(i)))
Sprites(i).InitializeWithTexture(Textures(i))
Sprites(i).SetSize(Sprites(i).Width/Main.P2M, (Sprites(i).Width/Main.P2M) * (Sprites(i).Height / Sprites(i).Width) )
Dim Origin As lgMathVector2
Origin = Loader.GetOrigin(L.Get(i), Sprites(i).Width)
Sprites(i).SetOrigin(Origin.X, Origin.Y)
Next
'4. Create the body fixture automatically by using the Loader, only first occurance.
Loader.AttachFixture(Bodie, L.Get(0), fd, Sprites(0).Width)
Log ("List: " & L)
End Sub
Sub draw(camera As lgOrthographicCamera,world As lgBox2DWorld, deltatime As Float)
camera.update
'deltatime is the time spent since last draw.
'first define which frame we should show
StateTime = StateTime + deltatime
If StateTime > TimePerFrame Then
If CurrentFrame < TotalFrames-1 Then
CurrentFrame = CurrentFrame +1
StateTime = 0
Else
CurrentFrame = 0
StateTime = 0
End If
End If
'destroy the fixtures belonging to the previous frame.
Dim arr As lgArray
Bodie.GetFixtureList(arr)
Log("Fixturelist: " & arr.Size)
For Each f As lgBox2DFixture In arr.toList
Bodie.destroyFixture(f)
Next
'attach new fixture based on current frame.
Loader.AttachFixture(Bodie, L.Get(CurrentFrame), fd, Sprites(CurrentFrame).Width)
'update positions.
update
Batch.ProjectionMatrix = camera.Combined
Batch.Begin
Sprites(CurrentFrame).Draw(Batch)
Batch.End
End Sub
Sub update()
'create logic to disable/enable bodies based on the deltatime, statetime and duration of the total sprite.
'world.Step(1/60, 8, 3)
For i = 0 To Sprites.Length - 1
Sprites(i).X = Bodie.Position.X - Sprites(i).OriginX
Sprites(i).Y = Bodie.Position.Y - Sprites(i).OriginY
Sprites(i).Rotation = Bodie.Angle * Conversion.radiansToDegrees
Next
move
End Sub
Sub move()
'create move code
Bodie.setTransform2(Main.camera.Position.x, Main.camera.Position.y, 0)
End Sub
Last edited: