Turret Projectile Offset

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Turret Projectile Offset

Post 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 1513 times
Img2.png
Img2.png (7.74 KiB) Viewed 1513 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.
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Turret Projectile Offset

Post 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.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: Turret Projectile Offset

Post 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
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Turret Projectile Offset

Post 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.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Post Reply