Rigidbody : How to calculate relative point for position based impulse or forces in Cocos Creator

Hello Devs,

I felt like sharing something which I struggled with a lot. I wanted to apply impulse on a rectangular cube at a specific position. Of course, you can use absolute position to make it work like this,

let absoluteRelativePoint = new Vec3(0.5,0.0,0.0); //rightmost side of the cube in absolute terms

this.rigidbody.applyImpulse(impulse,absoluteRelativePoint);

but this is wrong. The object won’t rotate if you have impulse = Vec3(magnitude, 0.0,0.0);

The function applyImpulse expects relativePoint param to be in world space without translation. Something like this,

let absoluteRelativePoint = new Vec3(0.5,0.0,0.0); //rightmost side of the cube in absolute terms

let worldSpaceRP = absoluteRelativePoint.transformMat4(this.node.getWorldRS()); //rotating and scaling relative point to node space(local space), ignoring translation

this.rigidbody.applyImpulse(impulse,worldSpaceRP);

and that’s it. You will get the right behaviour when you will apply impulses or forces on different point of an object.

Thanks.

Is this a documentation question or shall we change the category? Maybe Knowledgebase? Thanks for sharing.

1 Like

Hey,
Not a question. The ‘Rigidbody’ section in the documentation doesn’t mention that the relative point for impulse/forces should be rotated & scaled to world space. I am not sure if its the right thing to do but doing that solved the problem for me.

by the way, I worked with Babylon JS and Cocos 3D Quality is coming close to it. The results of your efforts are visible. Thank you for adding 3D support. :slight_smile: