UPDATE 5:
I now have a working solution but I will share some of the problems I encountered:
- The collision normals from Collision.contacts.normals aren't suited to the Vector3.Reflect function. By default, they are facing the wrong way to be used in vector reflection. I solved this by flipping them (multiplying the vector by -1).
- The collision normals aren't automatically normalized, code has to be added to do this.
- Your object has to have its forward direction facing -z in order for transform.LookAt to work(At least, I couldn't find a way of changing which vector it took as the forward vector).
Code:
function OnCollisionEnter (collision : Collision){
if (collision.gameObject.tag == "Obstacle"){
var contact : Vector3;
for (i=0;i<collision.contacts.length;i++){
print ("Collision point" + "i" + ": "+collision.contacts*.normal);*
contact += collision.contacts*.normal;*
}
//The above for statement adds all the collision points for the given
//collision together, to be normalized later.
var facingRot : Quaternion = transform.rotation;
var facingVector : Vector3 = facingRot * Vector3(0,0,1);
//The current rotation of the object is stored and changed into the facing vector
// of the object, in this case +z.
var collisionNormal = (Vector3.Normalize(contact))*-1;
//An average of all collision normals is normalised. It is also reversed so it is
//facing the right way for the Vector3.Reflect function.
newVector = Vector3.Reflect (facingVector,collisionNormal);
//An alternative to using the Vector3.Reflect function:
//newVector = facingVector - (2 * Mathf.Abs(Vector3.Dot(facingVector,collisionNormal)) * collisionNormal);
transform.LookAt(transform.position+newVector);
//The transform.Lookat function will rotate the object so its +z direction faces a vector.
//(This gave me trouble as my object faced +x at rotation 0, to begin with)
}
}
My original Question in full:
I would like to know how I can derive a rotation from a collision vector or collision point using js.
I am writing a behaviour script for an object. My object meanders around aimlessly, sometimes it collides with obstacles. At present, when it collides with an obstacle, it smoothly turns 180 degrees, regardless of the angle at which it collides with the other collider or the direction that the other collider’s normals are facing.
Bellow is a sketch of what I want to achieve here. The grey-brown rectilinear biped represents my object. My object has a box collider so the point(s) of collision would be different to the one I have circled in red. (note: I’m only using the y axis for rotation so the two dimensional representation below isn’t a simplification)
My diagram can be found here if it doesn’t load
I think it’s also worth noting what I don’t want to do:
My object is not effected by the physics engine, and I do not want it to be. It has a rigidbody collider with all axes of position and rotation frozen. All movement and rotation are input directly, not via forces. I suppose you could view my object as a robust robot which completely absorbs the impact of the collision and works out which way to turn based on the way the collider they have hit is facing.
–Many thanks.