Pendulum with a moving pivot

for a 2D game I want to simulate pendulum with a moving pivot. I don’t want to use rigid body objects, I want to manually calculate this.

the pivot point moves along x axis left or right, I know the rate of movement
the pendulum is at distance l and mass m from the pivot point. I need to calculate the movement of the pendulum.

If anyone has done this before, can you provide some help on this?

Can the pivot slide freely to the left/right (steered by the momentum of the pendulum), or is it animated/controlled by the user? Also, should the pendulum continue swinging with its regular motion even when/while moving the pivot (=non-physics based), or should it adapt its movement if you change the pivot (=physics simulation)?

–

The pivot point is controlled by the user. It is not affected by pendulum movement. I am not clear about your second question. Pendulum is attached to the pivot point (child object) so it should move left or right with the pivot point plus it should swing. But when the user stop the pivot movement the pendulum should swing for some time and then stop as in real life. Let's say the user move the pivot point other direction the pendulum should adopt to the new situation and swing accordingly.

–

So if the pendulum is not moving and the user then moves the pivot say to the right, the pendulum would first slant to the left (relative to the pivot) due to its inertia, and then start swinging when the user stops? So it should behave "realistically"?

–

Yes that is correct

–

2 Answers

2

Start with an empty game object for the pivot. Make the arm/bob a child object of the pivot. Then rotate the empty game back and forth using Lerp(). Use Mathf.Sin() to get the speed of motion right through the swing. Here is an example script for the empty game object:

public class Pend : MonoBehaviour {
	public float angle = 40.0f;
	public float speed = 1.5f;
	
	Quaternion qStart, qEnd;
	
	void Start () {
		qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
		qEnd   = Quaternion.AngleAxis (-angle, Vector3.forward);
	}
	
	void Update () {
	  transform.rotation = Quaternion.Lerp (qStart, qEnd, (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f);
	}
}

Thanks... Is there a way to change the angle base on the movement of pivot?

–

Yes, but it would take significant additional code (probably double the size of the code already written). You would need to use your own timer instead of Time.time. The timer would need to be set so that "(Mathf.Sin(Time.time * speed)*factor + 1.0f) / 2.0f)" would be either 0 or 1 depending on which way the pivot was moved.

–

I have somewhat modified the code. But still not giving the result I wanted. I have to modify the angle by 10000 to get some meaningful swing. The pendulum jumps to the new angle after I stop the movement. There is no smooth transition.

I found the actual equations for this here Here

Can anyone help me improve this code?

public class Pend : MonoBehaviour {
    public float angle = 0.0f;
    public float speed = 1.5f;
    public float frictionFactor = .999f;
    public float factor = 1.0f;
	
	private Vector3 lastPosition;
	private float xDirectionShift;
 
    Quaternion qStart, qEnd;
	
 
    void Start () 
	{
		
		lastPosition = transform.position;
		xDirectionShift = 0.0f;
		
       qStart = Quaternion.AngleAxis ( 0, Vector3.forward);
       qEnd   = Quaternion.AngleAxis (0, Vector3.forward);
    }
 
    void Update () 
	{
		float shift = transform.position.x - lastPosition.x;
		
		if (Mathf.Abs(shift) > 1.0f)
		{	
			xDirectionShift = shift;
			lastPosition = transform.position;
		
			angle = Mathf.Atan2(xDirectionShift,200.0f);
			
			qStart = Quaternion.AngleAxis ( angle*10000, Vector3.forward);
       		qEnd   = Quaternion.AngleAxis (-angle*10000, Vector3.forward);
		}
		else
		{
			transform.rotation = Quaternion.Lerp (qStart, qEnd, (Mathf.Sin(Time.time * speed)*factor + 1.0f) / 2.0f);
       		factor *= frictionFactor;
		}
		
       
    }

Can anyone provide some help on this?

–

bump .....

–