Failed to achieve uniform circle movement with rigidbody2D.
Hello,
I try to make rigidbody2Ds to do uniform movement based on the function F=mv^2/r, but the movement always become not uniform or totally irregular.
There are at least 3 modes are observed. The common thing is that the radius (distance between the rigidbody2D) changes, in different pattern. And their codes for the physics2D are run in different parts.
First one (physics run in coroutine), the rigidbody2Ds rotate around the center with the radius cycling between 0.5 and 1, I have to admit that this mode is interesting and lovely, it looks sort of like some dancers dance around bonfire (center), and some time dance in a closer distance, some time in a larger circle. Hope you can imagine that. Though it's beautiful, it is not the movement I want.
Second (physics run in update() ), the rigidbody2Ds rotate around for a short while (may be 0.5s?), and then they just fly away...
Third (physics run in FixedUpdate() ), I create a test project, use the red dots. The radius become smaller, though it looks fine in naked eye. The radius decrease from 1 to about 0.7, and it seems to increase very very slowly after that (this may be a little like the first mode? just far slower?). And after a long time (about 3min), the dots don't spread equally around the circle.(Fig2.)
debug.Log the distance of dots and center fig1. initial value and several seconds later
fig2. the distribution of dots around the centers.
Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player_Controller : MonoBehaviour
{
public GameObject Test; //the GameObject for test, the red dot.
public List<GameObject> TestObject; // to hold the red dots to control their movement.
public List<Vector3> Center; // to hold the centers surrounded by the dots.
private Vector3 center; // will be randomized in each cycle.
private Vector3 Posi; //hold the initial position of the dots.
private static float radian=Mathf.PI/3; // radian angle to spread the dots
void Start()
{
Quaternion Rotation = Quaternion.identity;
for (int i = 0; i < 4; i++)
{
center = new Vector3(Random.Range(-12f, 12f), Random.Range(-4.4f, 4.4f), 0); //get the center
for (int j = 0; j < 6; j++)
{
// Decide the position of the dot on the circle
float Angle = radian * j ;
Posi=new Vector3(center.x+Mathf.Cos(Angle),center.y+Mathf.Sin(Angle),0);
// Instantiate the dot and give the initial velocity in tangential direction.
GameObject tmpGameObject = Instantiate(Test, Posi, Rotation) as GameObject;
TestObject.Add(tmpGameObject); // store the dot into list for further control.
Center.Add(center); //store the coordinate of this dot's center.
tmpGameObject.GetComponent<Rigidbody2D>().velocity =new Vector2(center.y - tmpGameObject.transform.position.y, tmpGameObject.transform.position.x - center.x).normalized *2.0f;
}
}
}
void FixedUpdate()
{
foreach (var dot in TestObject)
{
if (dot != null)
{
Rigidbody2D missileRigidbody2D = dot.GetComponent<Rigidbody2D>();
//Get this dot's center
int i = TestObject.IndexOf(dot);
Vector3 centerVector3 = Center[i];
//Base on F=mv^2/r, here, F is the magnitude of the force.
float F = missileRigidbody2D.mass * missileRigidbody2D.velocity.sqrMagnitude / Vector3.Distance(dot.transform.position, centerVector3);
missileRigidbody2D.AddForce(new Vector2(centerVector3.x - dot.transform.position.x, centerVector3.y - dot.transform.position.y).normalized * F, ForceMode2D.Force);
Debug.Log(Vector3.Distance(dot.transform.position, centerVector3));
}
}
}
}
Your answer
Follow this Question
Related Questions
Physics of boxes and blocks in 2d 0 Answers
Is there joint physics example asset? 0 Answers
How to make flips, backflips like in game Backflip madness 2 Answers
stick-figure piercing 0 Answers