Trouble with multiple enemies

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Ross
Citizen
Posts: 98
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Trouble with multiple enemies

Post by Ross »

...Yup. As soon as you get past the basics, making a platformer involves a whole slew of annoying problems. I haven't personally solved the moving platform problem myself, but here are some ideas:
  1. Update your moving platforms before your player. Each platform needs a list of objects sitting on it, and moves them each frame, the same distance it moves itself. (I think this is the best option.)
  2. Have your character check the velocity of whatever it's standing on, and move itself appropriately. This could work fine, but you'd have to duplicate this code for each object that could ever sit on a moving platform (enemies, pushable blocks, pickups, etc.) and could be a bit more complex to record what you were standing on last frame, and so on.
  3. Instead of artificially setting the position of the platform each frame, let the physics engine handle it. I'm not 100% sure if you can just set the platform velocity for this to work or if you need to use forces/impulses to push it up and down. This method is OK-ish, but things on the platform can still go flying off the top or have to fall after it when the platform changes direction quickly, because that's how "real" physics work.
  4. Instead of only using direct contact for your "grounded" check, use a Sensor fixture, a couple of raycasts, or some other method of checking for the ground within a few pixels (vertically). This is a good idea in general to make the character controls a bit more forgiving and solve some jitter, but won't solve your problem for faster platforms (or platforms that move sideways) or fix all the visual issues.
You can also google "platformer moving platforms" or something similar and find a lot of stuff about this (for various engines).

Problems like this are why people will tell you "you should never use a physics engine for a platformer!", but it's all personal preference. Maybe with some engines with limited physics support that is true, but Love2D gives you full access to Box2D so it's easy to override the default behavior when you need to. You can do it either way.
SlyXPG
Prole
Posts: 7
Joined: Sun Apr 19, 2020 1:50 am

Re: Trouble with multiple enemies

Post by SlyXPG »

Ross wrote: Tue May 19, 2020 12:45 pm ...Yup. As soon as you get past the basics, making a platformer involves a whole slew of annoying problems. I haven't personally solved the moving platform problem myself, but here are some ideas:
  1. Update your moving platforms before your player. Each platform needs a list of objects sitting on it, and moves them each frame, the same distance it moves itself. (I think this is the best option.)
  2. Have your character check the velocity of whatever it's standing on, and move itself appropriately. This could work fine, but you'd have to duplicate this code for each object that could ever sit on a moving platform (enemies, pushable blocks, pickups, etc.) and could be a bit more complex to record what you were standing on last frame, and so on.
  3. Instead of artificially setting the position of the platform each frame, let the physics engine handle it. I'm not 100% sure if you can just set the platform velocity for this to work or if you need to use forces/impulses to push it up and down. This method is OK-ish, but things on the platform can still go flying off the top or have to fall after it when the platform changes direction quickly, because that's how "real" physics work.
  4. Instead of only using direct contact for your "grounded" check, use a Sensor fixture, a couple of raycasts, or some other method of checking for the ground within a few pixels (vertically). This is a good idea in general to make the character controls a bit more forgiving and solve some jitter, but won't solve your problem for faster platforms (or platforms that move sideways) or fix all the visual issues.
You can also google "platformer moving platforms" or something similar and find a lot of stuff about this (for various engines).

Problems like this are why people will tell you "you should never use a physics engine for a platformer!", but it's all personal preference. Maybe with some engines with limited physics support that is true, but Love2D gives you full access to Box2D so it's easy to override the default behavior when you need to. You can do it either way.
Taking into consideration your first and second suggestion it was such an trivial fix that I just could not figure out. This is only for the players body but I believe easily implemented in the way you were explaining, (getting a list of objects touching the floating platform). What you posted on my github is very interesting aswell. Seems way more clean than my current mess lol. I did try something like the current solution in the collision callback function but I was getting the error that the box2d world was locked, I think I was trying to manipulate things mid-step which caused the error.

Pre-fix

Code: Select all

  function moveFloat(float, dt)

    local getY = float.body:getY()
    if getY <= float.minY then
      float.top = true
    elseif getY >= float.maxY then
      float.top = false
    end

    if float.top == false then
      float.body:setY(getY - float.speed * dt)
    elseif float.top == true then
      float.body:setY(getY + float.speed * dt)
    end
  end
Post-fix

Code: Select all

  function moveFloat(float, dt)
    local getY = float.body:getY()
    if getY <= float.minY then
      float.top = true
    elseif getY >= float.maxY then
      float.top = false
    end

    if float.top == false then
      float.body:setY(getY - float.speed * dt)
      if float.body:isTouching(player.body) then
         player.body:setY(player.body:getY() - float.speed * dt)
       end
    elseif float.top == true then
      float.body:setY(getY + float.speed * dt)
      if float.body:isTouching(player.body) then
        player.body:setY(player.body:getY() + float.speed * dt)
      end
    end
  end
Post Reply

Who is online

Users browsing this forum: No registered users and 121 guests