firing at an angle + other things
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
firing at an angle + other things
how would i fire at an angel and have a weapon move around the player in a circular path which follows the mouse?
- ArchAngel075
- Party member
- Posts: 319
- Joined: Mon Jun 24, 2013 5:16 am
Re: firing at an angle + other things
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
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 , the best is anglef.love
- Attachments
-
- anglef.love
- (641 Bytes) Downloaded 158 times
-
- angle.love
- (636 Bytes) Downloaded 160 times
Re: firing at an angle + other things
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.
- JohnBobSmith
- Prole
- Posts: 4
- Joined: Tue Nov 03, 2015 9:17 pm
- Location: Alberta, Canada
Re: firing at an angle + other things
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...
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...
I'm a catholic and I like code.
Code: Select all
if god.isReal() and god.recievedPrayers() then
god.endPoverty()
god.endWars()
god.answerAllPrayers()
else
prayer = human.prayFor(poverty.end(), wars.end())
table.insert(god.prayerStorage, {prayer})
end
Who is online
Users browsing this forum: Bing [Bot] and 10 guests