Page 1 of 1
Having issue with how bullets are generating when my player rotates
Posted: Mon Feb 22, 2016 9:20 pm
by Teshi
Hi, im new to game programming but im having a go with a top down shooter. A found an open source sprite online and ive got it to rotate based on where the mouse is but now ive hit a bit of a wall in getting the bullets to rotate their starting position too, im not too sure how to go about it. Thanks for any help!
- Game.love
- WIP
- (13.02 MiB) Downloaded 180 times
Re: Having issue with how bullets are generating when my player rotates
Posted: Mon Feb 22, 2016 9:48 pm
by rougan
I guess you could get the gradient of the line between the center of your player and the position of the cursor. Once you know that gradient, you can just move your bullet along that line until it reaches it's target. Using the same rotation amount for the bullet should keep it pointing in the same direction as your player (I hope)
Re: Having issue with how bullets are generating when my player rotates
Posted: Mon Feb 22, 2016 10:08 pm
by Teshi
I think i phrased my issue wrongly, basically i can get bullets to fire towards the cursor but I dont know how to make it look like its coming out of the gun in the sprite basically
Re: Having issue with how bullets are generating when my player rotates
Posted: Tue Feb 23, 2016 11:19 am
by farzher
Teshi wrote:im not too sure how to go about it.
Here's the math
Code: Select all
xoffset = 50
yoffset = 10
bulletx = player.x + math.cos(player.angle)*xoffset + math.cos(player.angle+math.pi/2)*yoffset
bullety = player.y + math.sin(player.angle)*xoffset + math.sin(player.angle+math.pi/2)*yoffset
Understanding math is pretty important, I recommend looking up why this works (at least I think it works)
Otherwise you'll get stuck again and again