x


Help with bounce angle and velocity change

Hi, I am trying to write code to cause a ball to bounce off of the walls of an oval shaped arena. I dont want to use the built in physics engine because that dose a bunch of other things that I do not need. I am using the variables of xDirection and zDirection as the values of its x and z speed as well as using the value velocity for it's total speed. I figure I need the program to calculate the normal of the surface I hit and then somehow "flip" my xDirection and zDirection over that. I am not really sure how to proceed though. If anyone can help me to get this work it would be greatly appreciated, my math skills seem to be failing me and I can't seem to find all the proper info online (I must not be looking up the right things).

EDIT: OK so I got something working now. I am at least able to draw the correct lines of where it should go, except that after the first bounce it starts behaving weird, and going bounceing nearly back on itself, can anyone help me figure out why?

var xDirection:float;
var zDirection:float;
var velocity:float;
function Start()
{
print ("start");
    zDirection = velocity;
}
function Update () 
{
    transform.position.x += xDirection * Time.deltaTime;
    transform.position.z += zDirection* Time.deltaTime;    
}
function OnCollisionEnter(collision : Collision) 
{
    if(collision.collider.tag == "wall")
    {
       // Debug-draw all contact points and normals
       for (var contact : ContactPoint in collision.contacts) 
       {
         var reflection = Vector3.Reflect (transform.position, contact.normal);
         var reflectionNorm = reflection.normalized;
         Debug.DrawRay(contact.point, contact.normal, Color.green, 100);
         Debug.DrawRay(contact.point, -transform.position, Color.red, 100);
         Debug.DrawRay(contact.point, reflection,Color.blue,100);
         print("Contact point: "+contact.point);
         print("Cantact normal: "+contact.normal);
         print("Reflect: "+reflection);
         print("Position: "+transform.position);
         print("Normalized Reflection: "+reflectionNorm);
         print("Angle: "+ Vector3.Angle((contact.point - transform.position),transform.forward));
         xDirection = velocity * reflectionNorm.x;
         zDirection = velocity * reflectionNorm.z;
       }
    }  
}
more ▼

asked Dec 19 '11 at 07:54 PM

skail gravatar image

skail
61 8 9 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The Vector3.Reflect example in the docs is terrible, the worst possible - this function is intended to reflect vectors, not positions. I would use a Vector3 as the velocity, and always zero its y component to avoid small errors that could accumulate and make the object "escape" from the horizontal plane. I would also use FixedUpdate, because collisions are detected in the physics cycle. My script would be something like this:


var startVel: float = 5;
private var velocity: Vector3;

function Start(){ // start going in the z direction
  velocity = Vector3(0, 0, startVel);
}

function FixedUpdate(){
  velocity.y = 0; // make sure velocity is strictly horizontal
  // move the object (velocity is in world space):
  transform.Translate(velocity * Time.deltaTime, Space.World);
}

function OnCollisionEnter(coll: Collision){
  if (coll.collider.tag == "wall"){
    var hit = coll.contacts[0]; // the first contact is enough
    hit.normal.y = 0; // keep normal in the horizontal plane
    Debug.DrawRay(hit.point, hit.normal, Color.green, 0.3); // draw green normal
    Debug.DrawRay(hit.point, -velocity, Color.red, 0.3); // draw red "in" velocity
    velocity = Vector3.Reflect(velocity, hit.normal); // reflect the velocity
    Debug.DrawRay(hit.point, velocity, Color.blue, 0.3); // draw blue "out" velocity
  }
}

Anyway, you may have weird direction problems because Translate doesn't process collisions correctly - the rigidbody just "appears" inside a collider, and receive the "penalty force", which may not be in the correct direction.
Only physical methods (applying forces or changing rigidbody.velocity) do it the right way. A simple version using physics could be something like this:


var startVel: float = 5;

function Start(){ // start going in the z direction
  rigidbody.velocity = Vector3(0, 0, startVel);
  rigidbody.constraints = RigidbodyConstraints.FreezePositionY; // avoid changes in Y
}
more ▼

answered Dec 20 '11 at 04:41 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Thank you sooo much. it works like a charm.

Dec 20 '11 at 05:19 AM skail

This is old as hell, but just saved my sanity trying to solve a problem similar to this.

Jan 27 at 01:06 AM cj_coimbra
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x1948
x300

asked: Dec 19 '11 at 07:54 PM

Seen: 1912 times

Last Updated: Jan 27 at 02:20 AM