- Home /
Force in Circular Motion
Hi Everyone
So I'm trying to make a ball moving in half circular motion ( that is when it enters a zone, it will be moving in half circular motion to exit that zone, kindda like soft bouncing ). What I do is, let's say my Character is moving straight. Then in order to achieve this circular motion, I apply a constant force that is perpendicular to the Character.rigidboy.velocity ( and I need to update this new constant Force every single frame ). So, in Fixedupdate, I have a function to calculate my new Force vector, and a function to addForce to my Character. However, after doing this, my characeter seems to fly away in insense speed. I wonder what have I done wrong ? Thanks a lot for your time.
your basic idea is correct! well done
very likely you just have some bug.
simply plant a "marker" (an empty game object) at the center of the circle.
(if it is in the same place every time, just do that. if it is in a different place each run, set it dynamically when you enter the "turn zone")
Simply make the force always point towards that marker point.
to be clear unity HAS a thingy called "constant force". if you were referring to that, don't use it. delete that component. simply set the force each time (pointing at the marker as discussed) in each frame.
Hi Fattie, Im not quite sure what do you mean by set the force without using constant force, but yes, I do use rigidbody.addForce which I believe to be constant Force. I don't know that there exists another way around ?
look in the menus. component -> physics -> ConstantForce
it's a component, like adding camera, box collideer or whatever!
anyway, it sounds like you're NOT using ConstantForce. so that's fine.
It's either an issue of force vs. impulse or an over-calculated force. I reckon it's the latter.
"I apply a force that is perpendicular to the Character.rigidboy.velocity" --
that's incorrect, as I mention Ethan you simply apply the force towards a point in the center of the circle you want the object to travel around
it's exactly like swinging something with a string- you're applying the force of the string. it's just a line of code
you surely know the formulas involved from basic high school arithmetic, you can google up hundreds of pages!
http://www.school-for-champions.com/science/force_centripetal.htm
Alternately if you don't want to use physics, Aldo has explained beautifully how to "animate the velocity"
Just a note, all of this is generally useless in real-world video games because it does not actually "steer" the object around - it's why cars are so hard to simulate properly.
you say it's a ball so you might get away with it, but it will always be pointing the wrong way
incidentally you say "kindda like soft bouncing" - I have no idea what that means.
Answer by aldonaletto · Oct 01, 2012 at 07:11 AM
Unfortunately, physics isn't that easy: applying a force produces an acceleration, which gradually increase the velocity in its direction over time. The results are hard to predict, and probably far away from what you expect - unless you have time enough to wait until the ball trajectory stabilizes in an elliptical orbit, at God-knows-what velocity.
If you really want a semi circular trajectory, it's better to twist the rigidbody velocity each FixedUpdate while the ball is inside this zone. You could create an empty object, add a box collider and check Is Trigger, then adjust the collider dimensions and center to define the capture zone, and finally tag it as "CircularZone", for instance. You could do the magic in the character script, with something like this:
private var cZone: Transform;
private var rBody: Rigidbody;
function Start(){
rBody = gameObject.GetComponent.<Rigidbody>();
}
function OnTriggerEnter(other: Collider){ // entering circular zone
if (other.tag == "CircularZone") cZone = other.transform;
}
function OnTriggerExit(other: Collider){ // exiting circular zone:
if (cZone == other.transform) cZone = null; // null
}
function FixedUpdate(){
if (cZone){
// calculate the direction ball -> zone center:
var vCenter = cZone.position - rBody.position;
// find the axis to orbit around:
var vAxis = Vector3.Cross(rBody.velocity, vCenter);
// find the new velocity direction:
var vVel = Vector3.Cross(vCenter, vAxis).normalized;
// twist rigidbody.velocity to the new direction:
rBody.velocity = vVel * rBody.velocity.magnitude;
}
}
Thank you. This may be very useful to me and I don't have the experience to invent code like this.
Your answer
Follow this Question
Related Questions
How To Freeze An Object Motion? 3 Answers
Balls bouncing off each other 0 Answers
how to change physics material of a colider in runtime 5 Answers
Character Controller meets Rigidbody 1 Answer
add force to object that has 2 different rigid bodies 0 Answers