# "rotate" a vector3 by a quaternion

In my game, i have a ship that flies around on a 2d plane (moves up and down, left and right). I want this plane to be able to rotate in any direction and still be able to bounce off walls accordingly. because i need this plane to move forward at a constant speed, i cant have the ship’s speed simply reflect off the collided normal face.

if i could just “rotate” a vecor3 by a quaternion, i could get this working.

so basically, i will set a vector3 variable to the difference between the ship’s transform and the collision point. then i will “rotate” the vector3 variable so that it snaps to the plane’s transform rotation. then i can extract the x and y accordingly and apply that to the ship’s x and y speed.

a little confusing, i know

I admit, I did not quite understand what you are trying to do, but if you want to rotate a vector by a rotation represented by a quaternion, you just multiply the vector with that quaternion.

well, i tried that, but i think im still doing something wrong. maybe you can take a look at my code and see if it makes any sense at all.

``````function OnCollisionEnter (CollisionInfo : Collision)
{

var contact = CollisionInfo.contacts[0];

var collisionCalcer = Quaternion.LookRotation(transform.position - contact.point, transform.position.up);
var collisionCalcerVec : Vector3 = collisionCalcer * (transform.position - contact.point);
collisionCalcerVec.x = Mathf.Round(Mathf.Clamp(collisionCalcerVec.x, -1, 1));
collisionCalcerVec.y = Mathf.Round(Mathf.Clamp(collisionCalcerVec.y, -1, 1));
speedRaciox += ((wallHitMoveSpeed / speed) * (collisionCalcerVec.x));
speedRacioy += ((wallHitMoveSpeed / speed) * (collisionCalcerVec.y));

}
``````

maybe i can take a look at this from a different angle. do know how to convert a quaternion to a vector3 thats not 360 angles (example, Vector3(45, 90, 180) ). but a magnitude between -1 and 1. (example vector3(0.5, 1, -1)

would you just multiply the quaternion by vector3(1,1,1)?

If you’re looking for a bouncing effect, maybe Vector3.Reflect is what you’re after?

etphonehomegamer, you should try to explain your problem better.
What is it that you are trying to do?
I don’t understand it from your original post.
Since we don’t know what you are trying to accomplish to begin with we cannot tell you how to fix your code.

you can take a look at this thread for bouncing a ball from walls by reflecting the velocity vector around the collision normal vecotor
http://forum.unity3d.com/viewtopic.php?t=37926

ok, sorry, i really am making no sense here. thanks for trying to help me out here though. just one more random question though that may help me out with this problem.

is there a way to change the global coordinates? aka make it so that the ship/player is always at vector3(0,0,0) relative to the world, and so are all other objects?

I guess that would be the same as rotating and translating every object in the scene around the ship/player until the ship/player is at zero(rotation and position), but i’m pretty sure this would slow down the game a bit. please just ignore this paragraph if it makes no sense once again.

you have 2 sets of coordinates:

1. Global: with respect to the world origin
2. Local: with respect to the transform’s parent

if your transform doesn’t have a parent, then the local and the global coordinates are the same.

What you might do is attach your ship to an empty transform and then move this empty transform around. This way the local coordinates of the ship always stay at (0,0,0) the global ones are changing if you are moving the parent of the ship.
On top of that you can have objects that are children of your ship. They will move with the ship. Their global coordinates will be changing but the local ones will be the same.

to change local and global coordinates you would something like:

Global:

``````transform.position = Vector3.zero;
transform.eulerAngles = Vector3.zero;
``````

Local:

``````transform.localPosition = Vector3.zero;
transform.localEulerAngles = Vector3.zero;
``````

You can certainly code your enemy movements so they work this way. The player stays at 0,0,0 and everything else moves based on that, but as you said that would be really inefficient.

What do you need, exactly? If you just need to deal with an object’s coordinates in your player’s coordinate space (ie: get these coordinates as if your player is at 0,0,0 with zero rotation), just apply an inverse of the player’s rotation to the direction vector.

For example:

``````Debug.Log ("Other's position in world coordinates = " + other.position) ;

Debug.Log ("Other's position in local coordinates = " + (Quaternion.Inverse (transform.rotation) * (other.position - transform.position)) ;
``````

Update: Oops, seems Unity has a convenience function to do this for you. So you can also do:

``````Debug.Log ("Other's position in local coordinates = " + transform.InverseTransformPoint(other.position)) ;
``````

yes! it’s finally working. now my ship can fly in any direction, upside down, sideways, what ever, and bounce off walls and take a precise amount of damage. thanks so much for sticking with me, ivkoni and the other guys. ill have to post up a video of what i’ve got so far sometime soon.