# How to make a dynamic body revolve around a center object which is independent of rotation of center object?

Are there any default functions to apply force which make the dynamic object revolve around a pivot point and when we stop using force that object moves freely?

1 Like

Hi @Vikas,

It sounds like you would like to add some constraints to objects while applying forces to them. There is no direct way of doing this in the editor, but you can use the AmmoJS API to tell the physics engine to add these constraints. Here is a section of the user manual that has some example code:

https://developer.playcanvas.com/en/user-manual/physics/calling-ammo/

That page also links to an example project to show the different types of constraints here:

https://playcanvas.com/project/618829/overview/physics-constraints

2 Likes

To make a dynamic body revolve around a center object in PlayCanvas, you can use the applyForce function to apply a rotational force to the dynamic object. However, you need to calculate the appropriate force vector based on the desired rotation and the object’s position relative to the center object.

Here’s an example script that demonstrates how to achieve this effect:

``````var RevolvingObject = pc.createScript('revolvingObject');

// Properties
RevolvingObject.attributes.add('rotationSpeed', { type: 'number', default: 1 });

// Initialize function
RevolvingObject.prototype.initialize = function () {
// Get the rigidbody component of the dynamic object
this.rigidbody = this.entity.rigidbody;
};

// Update function
RevolvingObject.prototype.update = function (dt) {
// Calculate the direction from the dynamic object to the center object
var direction = this.centerObject.getPosition().sub(this.entity.getPosition()).normalize();

// Calculate the force vector for rotation
var force = direction.cross(pc.Vec3.UP).scale(this.rotationSpeed);

// Apply the rotational force to the dynamic object
this.rigidbody.applyForce(force);

// Stop applying force when the object is not rotating
if (force.length() < 0.1) {
this.rigidbody.applyForce(force.scale(0));
}
};
``````

In this script, we define a `RevolutionObject` script that needs to be attached to the dynamic object that you want to revolve around the center object. The script has two attributes: `rotationSpeed`, which controls the speed of rotation, and `centerObject`, which is the entity representing the center object.

In the `initialize` function, we obtain a reference to the rigidbody component of the dynamic object.

In the `update` function, we calculate the direction from the dynamic object to the center object and normalize it. We then calculate the force vector for rotation by taking the cross product of the direction vector and the UP vector (representing the axis of rotation), and scale it by the rotation speed.

We apply the rotational force to the dynamic object using the `applyForce` function. If the force vector’s length drops below a certain threshold (0.1 in this example), we stop applying the force by applying a force of zero.

Make sure to attach the `RevolutionObject` script to the dynamic object and specify the center object entity in the script attributes. Adjust the rotation speed and threshold values to achieve the desired effect.

Please be careful using ChatGPT answers @Jacob_McBride2. Information is most likely not completely correct and therefore not suitable for the forum.

3 Likes

I was just suggesting and the information does seem to be valid. I’m not trying to provide incorrect information. If anything I said was incorrect please let me know so I can fix it. I’m just here to help.

1 Like