How do I set position of a dynamic object?

Hello!

Is it just parts of the Box2D library that’s implemented? I’ve been trying to use the function SetTransform on the CollisionObject similar to the way you use apply_force but I don’t get any feedback from the Console.

Or is it someway I can use the go.set_position() for the same purpose?

Thanks!

You can’t set the position of a gameobject with a dynamic active collision object. You have to disable the collision object first:

msg.post("#collisionobject", “disable”)

1 Like

Another way is to make the collision object kinematic, but that means it won’t be affected by forces. Dynamic collision objects set the transform from the physics, so if you set your own position it will be overwritten by physics after your script has run. sicher’s tip of disabling it means that physics won’t set the position until you enable it again.

3 Likes

I’m trying to work around my issue here:

By manually creating the objects, so I’d be able to enable/disable as required and can set their group and mask in the editor. However, as was noted here I cannot set the position of a dynamic object, I have to disable to collision object first.

My problem is that when you enable the collision object again it snaps back to where it was. I wrote little function:

local function setPos(id, x, y)
	msg.post(id .. "#collisionobject", "disable")
	go.set_position(vmath.vector3(x, y, 0), id)
	msg.post(id .. "#collisionobject", "enable")
end

Which ends up doing nothing. If I comment out the enable I can see it did move:

local function setPos(id, x, y)
	msg.post(id .. "#collisionobject", "disable")
	go.set_position(vmath.vector3(x, y, 0), id)
	-- msg.post(id .. "#collisionobject", "enable")
end

All I want to do is have a selection of objects of the same base object that can collide with each other. If I use a factory (and hence can set their starting position) there seems to be no group/mask combination that won’t stop the object colliding with itself and I can’t dynamically change the group/mask later. If I add them to the main.collection manually I can set the group/mask so that each object only collides with the other ones (as I effectively have several different base objects, only differing by group/mask) but I cannot set their starting position.

Beyond having to do all the physics manually, does anyone have a solution?

1 Like

I also want to do this to avoid having to calculate the physics manually on a dynamic physics object. Is it impossible?

Edit: I was able to do this by using a factory and creating the dynamic collision game object in a specific location.

You can disable the dynamic collision object, move the game object and then enable the collision object again.

I tried that (many, many times, in many different ways!), but I never got it to work, like @amanset above. Is there a trick we’re both missing? Could you post some sample code or a working project?

I have done that in a project (to teleport dynamic objects). I think you need to enable a kinematic collision object, move the object and then disable the kinematic and enable the dynamic.

No, checked my project and there is no kinematic, only a dynamic collision object that I disable, then move and enable.

I think that you might not be able to perform these three steps during the same frame. @Mathias_Westerdahl knows more but I think that it might be that the physics update (and the new position is stored in the physics engine) happens at the end of each frame so you have to wait until next frame until you enable the physics again.

Ah, I didn’t try to wait a frame. My solution of using a factory works for now, but good to have that possible solution up my sleeve.