firing at an angle + other things

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

firing at an angle + other things

Post 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?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: firing at an angle + other things

Post 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
User avatar
jurses
Prole
Posts: 9
Joined: Sun May 03, 2015 10:23 am
Location: Canary Islands

Re: firing at an angle + other things

Post 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
Attachments
anglef.love
(641 Bytes) Downloaded 156 times
angle.love
(636 Bytes) Downloaded 156 times
Connorses
Citizen
Posts: 63
Joined: Fri Sep 06, 2013 7:02 am

Re: firing at an angle + other things

Post 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.
User avatar
JohnBobSmith
Prole
Posts: 4
Joined: Tue Nov 03, 2015 9:17 pm
Location: Alberta, Canada

Re: firing at an angle + other things

Post 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...
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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest