How to create enemy and player collision?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

How to create enemy and player collision?

Post by redsled »

Hello, this is my first post from snooping around the forums and decided I try it out. I'm still a little new to Love2d, but this is more of a coding problem than a Loved2d problem. So I'm making a little game where enemies are generated and the player goes around and shoots them. I'm having problems figuring out so that when a enemy intersects the player, the enemy stops moving yet keeping it's original x and y. I've tried using this code for the left of the player intersecting with the right of the enemy:

Code: Select all

for i,v in ipairs(enemy) do
	local startX = v.x
	local startXvel = v.xvel
	if player.x + player.width > v.x then
		v.x = player.x - v.width
		v.xvel = player.xvel
	end
	v.x = startX
	v.xvel = startXvel
end
Yet the enemy always moves through the player. If I take a the last two lines in the for loop, the enemies x equals the players. Thanks for the help.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to create enemy and player collision?

Post by davisdude »

Hi and welcome to LÖVE! :)

The enemy keeps moving through the player because you just the set enemy.xvel to whatever the player's is. I assume next you change the player's velocity. Try changing that to v.xvel = 0. Not to mention, right after that, you change it back to its original velocity (v.xvel = startXvel). You need to remove the last two lines and replace the sixth with v.xvel = 0.

Without more code that's all I can do.
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
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: How to create enemy and player collision?

Post by redsled »

davisdude wrote:Hi and welcome to LÖVE! :)

The enemy keeps moving through the player because you just the set enemy.xvel to whatever the player's is. I assume next you change the player's velocity. Try changing that to v.xvel = 0. Not to mention, right after that, you change it back to its original velocity (v.xvel = startXvel). You need to remove the last two lines and replace the sixth with v.xvel = 0.

Without more code that's all I can do.
Thanks for the advice but again I still incur some problems. Here's all of the code and the code that I previously posted was inside player.physics https://www.mediafire.com/?i3f8uxamamdg6d8. Thanks again for the help because this can become very frustrating.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to create enemy and player collision?

Post by davisdude »

Hey, I fixed your code. Check out the changes I made. :)
Attachments
boxshooterthing.love
(2.66 KiB) Downloaded 221 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
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: How to create enemy and player collision?

Post by redsled »

davisdude wrote:Hey, I fixed your code. Check out the changes I made. :)
Thanks davis, I grateful for the advice and the help. But is there any way to make so that the player cannot pass through the enemies or is that inevitable? I am happy to know that the LOVE2D forums is very kind and helpful. :awesome:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to create enemy and player collision?

Post by davisdude »

Yeah, there is. Just look at the enemy.physics code, and use the same logic for the player. I wanted to leave something for you to do- you learn more from doing. ;)
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
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: How to create enemy and player collision?

Post by redsled »

davisdude wrote:Yeah, there is. Just look at the enemy.physics code, and use the same logic for the player. I wanted to leave something for you to do- you learn more from doing. ;)
Thanks for the help davis!
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests