Bullets with rotating player[SOLVED]

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
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Bullets with rotating player[SOLVED]

Post by baconhawka7x »

I have a rotating player that is always facing the mouse, the player shoots when they click.

I want the bullet to go in the direction of wherever the mouse was when it was clicked, and after searching for it a few times on the forums, I can't find a solid answer. Can someone help me out?
Last edited by baconhawka7x on Wed Aug 21, 2013 4:48 am, edited 1 time in total.
User avatar
veethree
Inner party member
Posts: 868
Joined: Sat Dec 10, 2011 7:18 pm

Re: Bullets with rotating player

Post by veethree »

Allow me to quote myself here

Code: Select all

function love.load()
	bullets = {}
	bullet_speed = 200
end

function love.update(dt)
	for i,v in ipairs(bullets) do 
		v.x = v.x + math.cos(v.a) * bullet_speed * dt
		v.y = v.y + math.sin(v.a) * bullet_speed * dt
	end
end

function love.mousepressed(x, y, k)
	if k == "l" then
		bullets[#bullets + 1] = {
				x = player.x,
				y = player.y,
				a = getAngle(player.x, player.y, x, y) --Angle between player and mouse

			}
		end
	end
end

function getAngle(x1, y1, x2, y2)
	return math.atan2(x2-x1, y2-y1)
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets with rotating player

Post by davisdude »

Check out this little game I made to test just that. Just warning you: the game only functions in the sense of shooting and moving, not the bullet hitting the player.
Attachments
gunner.love
(54.24 KiB) Downloaded 162 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Bullets with rotating player

Post by baconhawka7x »

davisdude wrote:Check out this little game I made to test just that. Just warning you: the game only functions in the sense of shooting and moving, not the bullet hitting the player.
Yuss! Thank you, I got it figured out. A lot of the issues I was having were with an obvious issue that slipped my mind. I am zooming in the camera, which tends to complicate things. But It's all figured out now:D
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets with rotating player[SOLVED]

Post by davisdude »

Scaling ALWAYS seems to mess things up for me. No problem!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests