Page 1 of 1

firing at an angle + other things

Posted: Sat Oct 31, 2015 10:40 am
by Doctory
how would i fire at an angel and have a weapon move around the player in a circular path which follows the mouse?

Re: firing at an angle + other things

Posted: Sat Oct 31, 2015 12:20 pm
by ArchAngel075
id work with the angle of player and mouse, and fetch a point x distance away from player:

Code: Select all

//Pseudo code, 
angle //angle between player and mouse
player_x // player x position
player_y // player y position
distance //distance to project the weapon at?

gun_x = distance * math.cos(angle) + player_x
gun_y = distance * math.sin(angle) + player_y

Re: firing at an angle + other things

Posted: Sat Oct 31, 2015 12:22 pm
by jurses
That's how I made it, Hope it works. I used math.atan() so you don't have to work with the distance between the mouse and the origin, but be careful. Love default works the third quadrant like the first. Sorry i had to fix one thing :crazy: , the best is anglef.love

Re: firing at an angle + other things

Posted: Sat Nov 07, 2015 4:47 am
by Connorses
You might research the math behind what is known as "polar coordinates", for some general vector math. It's useful for programming games in general.

Re: firing at an angle + other things

Posted: Sat Nov 07, 2015 9:15 pm
by JohnBobSmith
I have done this, and you may want to look into math.atan2. My basic idea is to take the X and Y positions of the mouse, convert it to an angle (atan2), and using a bit more trig you can get the bullet to head towards the mouse in a straight line. To rotate the player, take the same mouse angle from before, and apply it to the player image when drawing. Example: love.graphics.draw(player.img, player.xPosition, player.yPosition, theMouseAngleFromBefore). Note that you may have to experiment with centering things (origins) to get it to look good. And you shouldn't need to convert radians to degrees (or other way). The raw (raw?) values will work, as they should be radians, and that's what Love uses.

I would give you a code sample, but my code is super messy. That, and I'm not great at math. I had to research for a few days to get the above working. Though taking trigonometry in my math class currently has probably helped, lol.

EDIT: Basically what everyone else has said will work. My post is over-complicated. I tend to do that...