Page 1 of 1

Turret Projectile Offset

Posted: Thu Jun 02, 2011 6:11 pm
by THe Floating Brain
Okay so I know how to rotate a sprite for say a turret toward a object. My problem (with rotating objects that fire projectiles) is offsetting the projectile coming out of a turret (for example) to make it appear as though is coming out of the gun(s). Take these images for example:
Img1.png
Img1.png (7.3 KiB) Viewed 1548 times
Img2.png
Img2.png (7.74 KiB) Viewed 1548 times
I haven't been able to find a good tutorial or work out the math, can someone help me?

P.s I know sometimes I have trouble getting my point across if further explanation is required please let me know.
P.s.s Sorry I posted 2 topics in a short time frame I don't intend to create spam.

Re: Turret Projectile Offset

Posted: Thu Jun 02, 2011 6:16 pm
by bnpph

Code: Select all

x = turret.x + cos(turret.direction) * turret.length;
y = turret.y + sin(turret.direction) * turret.length;
Something like that for a single barrel.

For multiple barrels, you can either:
A. use the above code twice - one perpendicular, and one going outwards
B. Use the above code but add a value to the direction to align correctly
C. Use a 2d matrix


Edit:
Since you are likely using a matrix to rotate your sprite, you can use the same one for the offset.

Re: Turret Projectile Offset

Posted: Sun Jun 05, 2011 6:15 pm
by THe Floating Brain
bnpph wrote:

Code: Select all

x = turret.x + cos(turret.direction) * turret.length;
y = turret.y + sin(turret.direction) * turret.length;
Something like that for a single barrel.

For multiple barrels, you can either:
A. use the above code twice - one perpendicular, and one going outwards
B. Use the above code but add a value to the direction to align correctly
C. Use a 2d matrix


Edit:
Since you are likely using a matrix to rotate your sprite, you can use the same one for the offset.
Thank you so much :-D

Re: Turret Projectile Offset

Posted: Sun Jun 05, 2011 7:33 pm
by ibly31
I recently had a similar problem except I am using Cocos2D. If whatever library you are using supports a Object to World conversion function you could do this. I specifically did it this way because I needed flexibility for muzzle flashes/lasers and left-to-right motion.

Basically I positioned the bullet at the object->coordinateToWorld( 0.5f, 1.0f ); for the center, and made the bullet travel in the vector direction of the barrels angle.