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;
}
}
}