Android Question Libgdx and position of dynamic bodies.

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
I'm just starting with libgdx and b4a. I want to change the position of the dynamic bodies during the simulation.

I tried several ways and don't works.

What the right way?

I tried:
B4X:
Sub timer1_Tick
         
    blockbody.GetFixture(0).Body.Position.y=.5
    blockbody.GetFixture(0).Body.Position.x=1.5
     
End sub
Thank you.
 
Last edited:

ilan

Expert
Licensed User
Longtime User
I'm just starting with libgdx and b4a. I want to change the position of the dynamic bodies during the simulation.

I tried several ways and don't works.

What the right way?

I tried:
B4X:
Sub timer1_Tick
     
    blockbody.GetFixture(0).Body.Position.y=.5
    blockbody.GetFixture(0).Body.Position.x=1.5
 
End sub
Thank you.


timer tick???

what are you doing with a timer when you are using libgdx??

have you created a physicworld?
have you converted world to screen coordinates?

moving a physic body is not done by translating the body because it will not apply any physics to the body and then whats the point using a physic body without physics?

you should use body.applyforce() or .applyimpulse()
this is how you move physic bodies around a world.

note that box2d or any physic engine is only a math library that calculates forces. nothing will be drawn by box2d so you have to store your bodies in an array to be able to draw on each frame update your sprite with libgdx at the position of the body.

its a little bit complicated to explain box2d and libgdx in a single answer so you should download some examples done by @Informatix and have a look at them

EDIT: and please remove the timer from a libgdx project. it has no place in it !!
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
I learned a lot in 24 hours.

I now have a box2d world.
I have a square that falls off the top and collides with a
static body on floor.
The square rotates. I have sprites that show the texture appropriately updating square´s rotation and position.

Everything seems to work fine but I still have the problem:

I need to reset the square position some times without restart the simulation.
I just wish the square back to initial position.


timer tick???

EDIT: and please remove the timer from a libgdx project. it has no place in it !!

Yes. Everything has been fixed and I now have the correct events
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Remove the square and create it again in the same starting position if u want to reset the scenario.

Just remove it from the world and create a new square

Yes that works fine.

I'm learning a lot about libgdx and box2d.

B4X:
body.setTransform2(x, y, a)
body.Position.Set(x, y)
body.Position.Set2(v)

work well for BODYTYPE_Kinematic bodies only.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
This is not the right way. You loose all physics like that.

Anyway, there are 3 body types in box2d

Dynamic
Kinematic
Static

A staticbody is a body that cannot move or apply force or velocity. Its ment for walls, ground,spikes,...

It also use much less calculation time so it is recommendet to use static bodies when ever you can to get better performance.

A kinematic body is a body that is not effected by gravity so no impulse or force can be applied BUT you can set a velocity to such a body to move it in your world.

This is ment for moving grounds, elevators,...

A dynamic body is a body that is effected by gravity and therefor you can apply force or impulse to it. You can also set a velocity to it.

This body is ment for characters, obstacles that should be effected by gravity.

It use the most calculation time from all bodies

So a dynamic body should be moved with force or impulse to keep the felling of physics.

A kinematic body should be moved with velocity (if u need it to move)

To get the best performance use static bodies whenever u can and the shape should be circle as much as you can.

The more complex the body fixture is the more calculation you will need.

Good luck
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
This is not the right way. You loose all physics like that.

Anyway, there are 3 body types in box2d

Dynamic
Kinematic
Static

A staticbody is a body that cannot move or apply force or velocity. Its ment for walls, ground,spikes,...

It also use much less calculation time so it is recommendet to use static bodies when ever you can to get better performance.

A kinematic body is a body that is not effected by gravity so no impulse or force can be applied BUT you can set a velocity to such a body to move it in your world.

This is ment for moving grounds, elevators,...

A dynamic body is a body that is effected by gravity and therefor you can apply force or impulse to it. You can also set a velocity to it.

This body is ment for characters, obstacles that should be effected by gravity.

It use the most calculation time from all bodies

So a dynamic body should be moved with force or impulse to keep the felling of physics.

A kinematic body should be moved with velocity (if u need it to move)

To get the best performance use static bodies whenever u can and the shape should be circle as much as you can.

The more complex the body fixture is the more calculation you will need.

Good luck

You're right.

I did not explain it right.

I am destroying the dynamic body and rebuilding as you said above and works well.

When I have learned more about libgx I will write a very basic tutorial for beginners. step by step.

You are one of the most knowledgeable about libgdx e game development here. Thanks for your help.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Upvote 0
Top