Launching Ball with Havok Physics

I’m trying to create a ball launching mechanic based on the camera forward position. I’ve done this before using CannonJS but I can’t seem to figure out why I’m running into issues with this. Here is the playground I’m using to test this with:

The first issue I had was that if I cloned the sphere and reset its position to the camera position that it would then move the camera with it using gravity. I assume the camera is being affected by the sphere, possibly getting stuck inside it. Do I need to modify the camera to ignore these kinds of collisions? or is there a better way to resolve this?

The second issue I have is that the applyForce doesn’t seem to do anything. Even with a value of 1000 along the Z axis it just falls. The mass of the sphere is set to 1 so I don’t understand why it’s not affecting it.

cc @carolhmj and @Cedric our Physix wizards.

Hello!

About the first issue, I’m not seeing the camera move?

As for the second one, there seems to be an issue with the physics body when cloning it, I’m investigating.

PR to fix the cloning: [Physics] Avoid cloning physics body twice and set body properties when cloning by carolhmj · Pull Request #14019 · BabylonJS/Babylon.js (github.com)

2 Likes

I adjusted the position of the cloned object to avoid the camera issue in the previous playground but here it is with the issue present:

When I’ve done this in the past I would create the initial object, then use “setEnabled(false)” that way the original is not actually active. When I’m ready to clone, I clone the original, reset the position to the camera position then enable it. This worked fine before with CannonJS but seems to interact with the camera for some reason when I do it with the Havok physics plugin.

You set reference of camera’s position-Vector3 to clone’s position, instead you can clone() or use copyFrom() to avoid camera moving with the clone:

clone.position = camera.position.clone();
1 Like

Thanks, that actually solved my issue with the camera positioning. Here was another example I was working on where the camera positioning was getting “caught” on the physics ball:

It’s supposed to launch a ball into the cups, but seems to pull the camera with it. Nonetheless after changing the camera position to camera.position.clone() it worked as expected. Just wanted to make note of the camera issue in case its a bug of some sort.

That’s because when you assign the camera position to the physics object position, they’re referencing the same object, so when physics update the object, the camera will “see” the same updates. :slight_smile:

1 Like

So they’re basically updating each other in this case? The physics object uses the camera’s position for every update that makes sense, but then the camera updates its position on the physics object’s position as well? If the camera was static why would it start changing its position in this case? Just trying to understand that part. The solution to that was to clone the position as mentioned, just want clarification. Thanks!

Position is a Vector3, which is a reference type ( (7) JavaScript Value vs Reference Types - YouTube). So when you do:

camera.position = physicsObject.position

they’re referencing the same space in memory. If you change camera.position then the physicsObject will see the same changes, and vice versa.

1 Like

Right, I understand that, but in my example, I never update the camera’s position to the physics object. I only reference the camera’s position for the physics object. That’s why I’m confused. Is something in the background updating the camera position to the physics objects’ location?

I modified the playground to just have a plane and the ball with a basic implementation of the camera with no controls: https://playground.babylonjs.com/#Z8HTUN#484

Based on this code, what part would be modifying the camera’s position? If I’m constantly updating the position of an object to the camera’s position then I would expect it to always move with the camera but not the other way around.

Nothing. They are just the same Vector3-Object. If you change camera.position then the object you are changing is the same as if you would change mesh.position.

The following line links/connects position of both (ball and camera), make them reference the exact same object:

ball.position = camera.position;

If you assign to position-property you either have to set x,y,z or create a new object (new Vector3()) when you want the position-property be its own Vector.

The following shows ways to keep the old Vector3 of ball only and set x,y,z by that of camera:

ball.position.copyFrom(camera.position)  // option 1
ball.position.set(camera.position.x, camera.position.y, camera.position.z)  // option 2

The following creates a new Vector3 object and assign as ball position:

ball.position = new Vector3(camera.position.x, camera.position.y, camera.position.z)

Hope this will clarify things for you :slight_smile:

2 Likes

Thanks, honestly this is just for my own personal understanding. No need to discuss this further. I was just hung up on why the camera position was getting updated. I wasn’t aware that would link them both ways.