Update object origin

Hello,

I’m trying to do some physics simulations and one of the tests would be to move an object not with a predefined action but due to the physics itself.

As an example, I’m using an inverted “U” to simulate two legs.
I use shape keys to transform the mesh to simulate the movement of one leg.
I use reinstancePhysicsMesh to update the mesh in every frame and it works fine.

If for example I raise the left leg to the left, the “U” falls to the left.
But if I raise the left leg to the front, it should fall not only to the left but also slightly to the front due to the change of the gravity center. (it is so if i use a model already in that position with the gravity center (objet origin) in the right place)

The problem is that it is not updating the center of mass (object origin) as the mesh changes. I’ve tried to use the (bpy.ops.object.origin_set(Type=‘ORIGIN GEOMETRY’, center=‘MEDIAN’) function (both with Median and with Bounds) and it does not change the origin as the mesh changes.

Is this because it considers the original mesh without the shape key or do I need to do something else? Is there another function to do what I need?

I will probably in a future test try to do a function myself to calculate the mass center, with the vertex positions and probably with a weight function to simulate different densities along the object, but in this initial phase I would use a simpler function (although not so physically correct) if it exists.

Thank you for the help.

I read the problem more carefully, and found that you used the same solution! Sorry!

Original Post:

You have to re-instance the mesh to update it’s bounding box (triangle mesh).

Your python code only runs in Blender, not in the Game Engine, there’s a separate piece of code to do this in the BGE.

I quickly looked it up on tutorialsforblender3d.com, here’s a link to its API:
reinstancePhysicsMesh

Make sure the object you use this code on has triangle mesh bounds.

Good luck!

Linkxgl,

Yes, I use reinstancePhysycsMesh and it works fine with the triangle mesh bounds in order to update the mesh boundaries, the problem is the center of mass of the object that remains in the original position…
For sure someone should already have had this same problem but I can’t find a solution anywhere.

To my knowledge, you can’t move the center of mass in the game engine. The bpy module doesn’t work in the BGE - it’s for Blender operations only. It might be possible to try offsetting the mesh’s position with a shapekey to match the object center’s proposed position.

Thanks SolarLune.
It’s a pity if you can’t change the center of mass…
Offsetting the mesh position could probably work, but if I understood your suggestion, I would have to move the object in the opposite direction in order to stay at the same position, isn’t it?

I believe so, yes. You would use the shapekey to move the mesh over, and then move the object in same amount in the opposite direction, thereby offsetting the center. At least, that’s a theory. I’m surprised that altered shape-keys are picked up by reinstancePhysicsMesh() - I didn’t think it took effect, for some reason.

I suggest to use multiple physics objects connected with rigid body constraints.

BTW. Using BPY is like changing the blueprints of an already build car. The car will not care this changes ;).

@SolarLune, thank you for the suggestion. I’ve been looking into the rigid body constraints and I’ll try if it works in this intitial phase, but for sure I’ll give a try of how to change the center of mass afterwords and see if your method gives good results.

@Monster, thank you for the suggestions, I was in fact looking to those rigid body constraints as you posted, I’ll give it a try. Concerning the bpy, sorry for my short experience in BGE and Python, but from what I understood from what you said, in BGE we can only use the Game Engine modules? (and probably the stand alone modules?) none of the application modules will be accessible in BGE, right?

This is correct.

But: if you run the BGE from within Blender you can indeed call the BPY. But as stated above it is like changing the blueprints of a car you already sit in.
And BPY is not available outside of Blender (standalone).

Ok, thank you for the tips.