[SOLVED] Raycast Normal only works on non-moving objects

Hi there,
I was looking for a way to get an object to rotate towards the normal-vector of succesful raycast.
@yaustar posted this working example in 2018:
https://playcanvas.com/editor/scene/605466

I am not sure if things worked differently then, but I forked the example and added two cubes, the left cube is rotated in the editor and returns correct normals according to the preset rotation. The middle cube is not rotated and comes from the original example. The right cube sets its rotation after a 100ms delay. This cube does not return the correct normals, but the ones according to the rotation set at the init of the project (which are zero). Project below:
https://playcanvas.com/editor/scene/1539751

What’s going on? Am I missing something, how can I raycast a normal which is being rotated in runtime? I can work with an extended function of the ‘setMat4Forward’ function if anyone knows how to include current rotation.

Thanks in advance
j

Solution: Don’t set your object to static, it will only update the visual side of things. When set to kinematic you can rotate in runtime and raycast to return correct normals.

When using physics, it’s important to understand that there are two parts, the entity that effectively represents the node and visual representation of the object and the rigidbody physics body in the physics simulation world.

The relationship between these two is different depending on the type that is set on the rigidbody:

  • static: the physics body doesn’t move or change from the position and rotation it is created at. Setting the position/rotation of the entity will only change the visual representation, not the physics body.
  • kinematic: the physics body position and rotation is matched with position and rotation of the entity
  • dynamic: the entity position and rotation is set to the physics body position and rotation as it is simulated in the physics world.

(More details here: Physics Basics | Learn PlayCanvas)

In your project, all the rigidbodies are set to be static so the physics body will not move at all. It looks like you want to set the one that has a delayed rotation to kinematic if you want to change the position/rotation.

Or you can disable and re-enable the static rigidbody component which will recreate the physics body.

Example of changing it to kinematic: https://playcanvas.com/project/986078/overview/f-normaltomat4rotation

Kapture 2022-09-22 at 9.50.23

1 Like

Thank you! And for the quick reply.