Best way to rotate a collision object? (SOLVED)

what is the best way to rotate a collision object (and the game object that it’s in). I saw a similar post to this. However, that involved setting the collision object type to kinematic which I don’t want to do (I want to keep mine set to dynamic so collisions are handled automatically)

So you want the point of mass of the body to move dynamically, but you need full control over its rotation? Hmm, I’m afraid that’s quite tricky. At the moment it’s not possible in a straight-forward way afaik. Depending on your needs you could experiment with applying offseted forces for it, but that’s usually a troublesome path to choose. Maybe you could explain what you’re trying to do a bit more, there could be other ways of getting an acceptable result.

1 Like

I was trying to have a vehicle that, when you pressed either left or right on the mouse, the vehicle would rotate in that direction

I’m working on a Gravity Wars/Thrust clone in Defold and I’m using dynamic physics objects. For rotation I apply opposing and offset forces and it works really great.

1 Like

could you send me some sample code?

I’ve added an example of this here:

https://github.com/britzl/publicexamples/blob/master/examples/rotate_collision_object/rotate_collision_object.script

The key here is to apply opposing forces from the same directions all the time, regardless of the current rotation of the game object. See it live here (left and right key to rotate):

http://britzl.github.io/publicexamples/rotate_collision_object/index.html

3 Likes

thanks a lot! Just finished a 5K run so too tired to check it now but will try it when I get a chance

1 Like

Working URL:

1 Like

This works, but I can’t figure out how to rotate the collision object around a point that isn’t its world center? Moving the collision object inside the game object has no effect, strangely.

I would have thought that it had an effect.

We have previously discussed the addition of a center_of_mass property that would be useful in a case like this.

For some reason, both these configurations rotate around the center of the rectangle:

Interestingly, get_world_position on the collision object always return the same coordinates as for the game object itself, regardless of where the collision object is positioned.

Edit: Which makes sense! Because it’s the fixture that is positioned differently, not the collision object itself.

You can’t use go.get_position(), go.get_world_position() or any of the other similar functions to get the component position. They always return the game object transform.

Okay, but then why does the collision object rotate around its own center, even if the word position is in a different place? This may actually not be an issue in my case, I just want to know WHY! :slight_smile:

It is rotating around its center of mass.
Is that not what you’d expect from a physics object?

I suppose I was looking for a way to change the center of mass. To me, moving the collision object away from 0x0 should do this, but obviously it’s not that straightforward.

Yes, currently we don’t support changing the center of mass, we let Box2D/Bullet3D calculate it for us.
We can totally see this as an improvement, but we have nothing planned for this right now.

2 Likes

I was playing around with physics a bit lately and after adding multiple collision objects to one GO (to make its surface bumpy) I noticed the centre of mass moved accordingly, making the rotation a bit wonky. Which looked great, so I kept it.

So I guess one way of changing the centre of mass at runtime would be to add multiple COs and disable the extra ones depending on what behaviour you want. It would be messy, though.

2 Likes

Great, love a messy solution! Nice hack, might try that.

1 Like