Public Group

# XNA 2d offset projectile origin based on rotation

This topic is 2163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a ship sprite that rotates (this one's simple but I really can't remember how to accomplish it) and has 2 guns, I'm trying to offset the origin of the projectiles so they line up properly.. One on the front-left (45' NE from origin) and the other on the front-right (45' SE from origin) Both need to be pushed about 32 pixels from the center of the ship-sprite (ship is rotated from the center, not top-left)

This is my Fire() function in my Bullet class. (Only working with one gun atm, will duplicate and adjust once I get a reply.)

 public void Fire(int X, int Y, int Facing, float angle) { iFacing = Facing; if (iFacing == 0) iX = X - 10; if (iFacing == 1) iX = X + 10; iY = Y - 10; bActive = true; rotation = angle; }  Edited by Mashuga

##### Share on other sites
After a good bit more searching, I've located the code below and am attempting to adapt it to what I have posted above.

 //Set offset values, assuming that (x,y) are relative to the center offsetX = x; offsetY = y; //Rotate double angle = 45.0f; tempX = offsetX * cos( ( D3DX_PI / 180.0f ) * angle ) - offsetY * sin( ( D3DX_PI / 180.0f ) * angle ); tempY = offsetX * sin( ( D3DX_PI / 180.0f ) * angle ) + offsetY * cos( ( D3DX_PI / 180.0f ) * angle ); xPos = tempX + parent->getX(); yPos = tempY + parent->getY(); 

##### Share on other sites
Went with this

 public static Vector2 RotateAroundVector2(Vector2 point, float radians, Vector2 pivot) { float cosRadians = (float)Math.Cos(radians); float sinRadians = (float)Math.Sin(radians); Vector2 translatedPoint = new Vector2(); translatedPoint.X = point.X - pivot.X; translatedPoint.Y = point.Y - pivot.Y; Vector2 rotatedPoint = new Vector2(); rotatedPoint.X = translatedPoint.X * cosRadians - translatedPoint.Y * sinRadians + pivot.X; rotatedPoint.Y = translatedPoint.X * sinRadians + translatedPoint.Y * cosRadians + pivot.Y; return rotatedPoint; } //---- in Fire(){} from above; Vector2 retval = RotateAroundVector2(new Vector2(iX, Y - 8), rotation, new Vector2(X, Y)); iX = (int)retval.X; iY = (int)retval.Y; 
iX is modified based on player.iFacing (-26 if facing left, +26 if facing right)

##### Share on other sites
Hi,

Not sure if its what you're looking for but I have a rotating ship that fires missiles. The missiles needed to do two things:

Start at the position of the gun.
Be rotated to the correct angle.

This code does it:

[source lang="csharp"] public void AddMissile(Ship ship)
{
Missile missile = GetMissile(ship.Index);

float fCos = (float)Math.Cos(ship.Rotation);
float fSin = (float)Math.Sin(ship.Rotation);
Vector2 missileDirection = new Vector2(fCos, fSin);

missile.Velocity = missileDirection * ship.Power;
missile.Position = ship.Position + (missileDirection * 15); // * 15 so that own ship collision doesnt occur

missile.Rotation = (float)Math.Atan2(missile.Velocity.Y, missile.Velocity.X);
}[/source]

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002140
×