How to make the player and enemy collide like they're solid?

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 make the player and enemy collide like they're solid?

Post by redsled »

Yo, quite frankly I'm pretty new to this sort of stuff. As the usual, I've followed some of sockmunkee dev's tutorials, but those haven't really helped me build an "actual" game. I'm trying to make a small arcade game and I want it so the player and the enemy collide as solid objects. So that if the player runs into the enemy, the enemy bounces off almost. As of yet, if there is a way to do this with the implemented physics engine, I do not know how to do it. I've included the .love file and thanks a bunch.

PS: As I've said, I'm pretty new, so if you happen to provide a suggestion, please explain it to me like I'm a five year old.
Attachments
smash_em.love
alpha v0.000000000001
(740 Bytes) Downloaded 137 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to make the player and enemy collide like they're so

Post by ivan »

If you only use axis aligned bounding boxes then it's a question of sorting/separation:

Code: Select all

-- checks for a collision between rectangles "a" and "b"
-- returns "nil" if there is no collision
-- returns the separation vector (sx,sy) if there is a collision
function checkcol(a, b)
  local x1,y1, w1,h1 = a.x,a.y, a.w/2, a.h/2
  local x2,y2, w2,h2 = b.x,b.y, b.w/2, b.h/2
  -- center points
  local c1x, c1y = x1 + w1, y1 + h1
  local c2x, c2y = x2 + w2, y2 + h2
  -- distance between centers
  local dx, dy = c2x - c1x, c2y - c1y
  local dxa, dya = math.abs(dx), math.abs(dy)
  -- overlap check
  local sw, sh = w1 + w2, h1 + h2
  if dxa >= sw or dya >= sh then return end
  -- separation
  local sx, sy = sw - dxa, sh - dya
  -- ignore the longer axis
  if sx > 0 and sy > 0 then
    if sx > sy then sx = 0 else sy = 0 end
  end
  -- preserve sign
  if dx < 0 then sx = -sx end
  if dy < 0 then sy = -sy end
  return sx, sy
end
This only gives you the "separation vector" or by how much do you have to displace rectangle "a" so that the two rectangles are no longer overlapping.
Also the code has one caveat - it can't figure out the separation when the two rectangle have the same center positions - since there could be more than one solution, however this case is very unlikely with games.
If you want to have elastic collisions, friction and so on it becomes a little more complicated.
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: How to make the player and enemy collide like they're so

Post by Foxcraft »

Hey redsled,

There's two parts to this. One, detecting an actual collision. Two, handling what happens once a collision is detected.

The bump.lua library is a great way to get going with collisions, especially if you want to keep it simple. It handles checking for collisions via bounding boxes, and then it helps you set certain scenarios. (This object you stop at, this object you go passed, etc.) Other than giving the right coordinates for your objects if they were to stop right at the surface, the actual physics is up to you.

You can really see this sort of stuff for bump.lua on its Github.
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: How to make the player and enemy collide like they're so

Post by redsled »

Hey thanks for the code, but I'm having a hard time understanding it. I've heard of the library but I've never used it... gonna look into it. Oh yeah, what would the code be for elastic collisions cause that's kinda what I was shooting for.
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: How to make the player and enemy collide like they're so

Post by Foxcraft »

Hey redsled,

You probably could use bump.lua's bounce collision resolution to help, or setup something like that. That's for things bouncing off of another thing.

If you look at the link for bump.lua on Github, that page right there is a pretty decent walkthrough on how everything works. And the demos are good at showing what it can do, as is this.

Hopefully that helps you try to rule in or out if bump.lua is the right thing for you. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 109 guests