Sign in to follow this  

XNA 2d offset projectile origin based on rotation

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

If you intended to correct an error in the post then please contact us.

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.)

[CODE]
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;
}
[/CODE] Edited by Mashuga

Share this post


Link to post
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.

[CODE]
//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();
[/CODE]

Share this post


Link to post
Share on other sites
Went with this

[CODE]
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;
[/CODE]
iX is modified based on player.iFacing (-26 if facing left, +26 if facing right)

Share this post


Link to post
Share on other sites
Guest slikvik55
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]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this