Page 1 of 1

How to create enemy and player collision?

Posted: Wed Jun 04, 2014 6:42 am
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.

Re: How to create enemy and player collision?

Posted: Wed Jun 04, 2014 10:03 am
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.

Re: How to create enemy and player collision?

Posted: Wed Jun 04, 2014 9:40 pm
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.

Re: How to create enemy and player collision?

Posted: Fri Jun 06, 2014 6:53 pm
by davisdude
Hey, I fixed your code. Check out the changes I made. :)

Re: How to create enemy and player collision?

Posted: Fri Jun 06, 2014 7:25 pm
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:

Re: How to create enemy and player collision?

Posted: Sun Jun 08, 2014 2:21 pm
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. ;)

Re: How to create enemy and player collision?

Posted: Tue Jun 10, 2014 6:42 pm
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!