• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Rotating a point around a central vertex

## Recommended Posts

Hi, I have a space sim shooter, and previously I just had the gun thing shoot from the origin of the ship out in the direction of the ship''s view, but now I want to mount the gun on the sides of the ship. I quickly ran across the problem of calculating the position of the guns firing position in relation to the ships rotation. I am using quaternions, but I don''t know how to use them well enough to figure this out. What is the simplist way of finding this position based on the quaternions? Also, how would I then find their directional vector? Thanks!

##### Share on other sites
Hi there-
This problem should be able to be solved with simple vectors, no quaternions if I understand you correctly. Say each bullet has some position and some initial velocity. The initial position of the bullet is located where the gun''s muzzle is, and the velocity is some initial velocity in the direction of the look vector. So for the bullets, the velocity vector is the normalized look vector times the speed of the bullet. All that can be handled with simple vector stuff. Now for the position of the bullets. Suppose that in the reference frame of the spaceship, there is a vector P that represents the location of the gun relative to the center of the ship. Say the ship isn''t rotated. Then the position of the muzzle is the vector from the center of your gamespace to the center of the ship plus the vector from the center of your ship to the gun. Thats easy enough to program. Now say that the ship has rotated through an angle theta. This is like rotating P through an angle theta, and through trig, you get that P''.x = P.x * cos theta and P''.y = P.y * sin theta where P'' is the new gun position vector. Just add the ship position vector to P'' and you have the location of your gun, set that to your bullets, and add the velocity as stated above. Hope this all helps!

##### Share on other sites
What about the z-dimention? Also, when you say rotated through an angle theta, do you mean the angle like in glRotatef that you put before the axis coords? Thanks a lot, I''ve almost got it.

##### Share on other sites
Here is the line of code that I have, but I dont' think it works:
t1.vec.pos = ship.vec.pos + CVector3(cos(DEG2RAD(ship.vec.euler.x)) * sin(DEG2RAD(ship.vec.euler.z)) * 20, sin(DEG2RAD(ship.vec.euler.y)) * -10, sin(DEG2RAD(ship.vec.euler.z)) * -20);

Am I on the right track or way off?
Thanks!

[edited by - crossbow on November 20, 2002 11:06:58 PM]

##### Share on other sites
CVector3(sin(DEG2RAD(ship.vec.euler.y)) * sin(DEG2RAD(ship.vec.euler.x+DEG2RAD(90)),cos(DEG2RAD(ship.vec.euler.y)),cos(DEG2RAD(ship.vec.euler.y)) * -sin(DEG2RAD(ship.vec.euler.x))+DEG2RAD(90));

How do I fix this and incorperate roll? I though this would be simple, but nobody seems to know the basic math.

[edited by - crossbow on November 20, 2002 11:07:26 PM]

##### Share on other sites
I would think this''d work:

    // Calculate turret metrics.  void Ship::calculateTurretMetrics (Turret turret, Vec3 &pos, Vec3 &firevec)  {    // Transform the turret position.    pos = this->orient * turret.pos + this->pos;    // Transform the turret orientation.    firevec = this->orient * turret.firevec;  }

This produces pos , which is where the turret''s shot comes from, and firevec , which is the direction the shot is going in. this->orient is a quaternion.

Hail Eris! All fnord hail Discordia!