Page 1 of 1

Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 2:16 am
by slimefriend
Hello,

So this bug has been driving me crazy. I have an enemy that I want to move back and forth on a given platform--ie, change direction when they hit a wall AND change direction when they find the end of the platform. Its that second part thats giving me trouble.

Here's the collision checking code:

Code: Select all

	
	self.x = self.x + self.v.x
	if not self:collide("level", self.x + sign(self.v.x)*self.width, self.y+self.height) or self:collide("level", self.x + sign(self.v.x) , self.y) then
		self.v.x = self.v.x * -1
	end
The first collide() call is the one that doesn't work, the second works just fine.

That self:collide function is more to make me feel comfortable coming from Flashpunk, it's actually very simple:

Code: Select all

	local _,_, cols, len = self.bumpWorld:check(e, x, y, checkingFilter)
	for _, v in pairs(cols) do
		if (v.other.type == type) then return v.other end
	end
	return nil
checkingFilter just returns "cross".

i've turned off any other collision checking and placed my enemy right at ground level. The problem with this is: the enemy moves almost completely off the platform before turning around. and i have absolutely no idea why.

help would be greatly appreciated. and if you need any more details from me, please let me know

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 8:09 am
by nyenye
First say that it's easier to help if we have code to work with, but anyway, here goes some thoghts.

I personally havent used bump much, but one thing is that when the enemy goes to the left (v.x is negative), you must check if the leftmost part ( x ) of the enemy "is not on ground", and when going to the right (v.x is positive), the same for the rightmost part (x + width)

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 8:28 am
by Tjakka5
Nyenye is right in that you need to check the left most or right most part accordingly. Of course you can't explicity tell bump to only check for collisions on that part of your object, which is why you can query the world.

https://gyazo.com/76a276be4c12832fb14233f006aa53f8

I hope this helps, if needed I can write up a script that shows how to do it.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 2:10 pm
by slimefriend
I don't really see how that would help? I'm already using a rectangle (the enemy's hitbox) to check for collisions, and then casting it forward to check if it still collides at that position. I tried nyenye's method of changing how far I cast ahead depending on which direction the enemy is going but it still doesn't help. I feel like there might be something else going on here but I don't know what

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 5:55 pm
by nyenye
if you can get the object which enemy collides with (object being the floor/platform), and from the object you can get the x, y (upper-left corner) and width and height (w, h), then with some simple maths, you will know if enemy is on the edge:

Pseudocode:

Code: Select all

if e.x littlethan f.x then
   -- enemy on left edge, turn right
end
if e.x + e.w greaterthan f.x + f.w then
    -- enemy on right edge, turn left
end
A problem with this could be that if you use a tilemap and each tile is a collider i wont work as expected.

Hope it helps clarify my earlier point.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 6:15 pm
by Tjakka5
Here's some somewhat 'pseudocode' demonstrating what I said earlier in quering the world:

Code: Select all

function mover:update(self)
  local newx, newy, cols, len = world:update(self, self.x + self.vx, self.y + self.vy)
  self.x, self.y = newx, newy
  
  for i = 1, len do
    -- Resolve the collisions
  end
  
  local _, len1 = world:queryPoint(self.x, self.y + self.h + 1) -- 1 pixel below the left bottom corner of the object
  local _, len2 = world:queryPoint(self.x + self.w, self.y + self.h + 1) -- 1 pixel below the right bottom corner of the object 
  
  if len1 == 0 or len2 == 0 then -- If there's 0 collisions, there is no platform under one of our corners
    self.vx = -self.vx -- Reverse direction
  end
end
I hope this helps.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 8:21 pm
by 4aiman
I personally am using "sensors" - a group of small objects (rectangles of a various width and/or height) attached to a moving entity.
Those objects are placed with specific offsets around that entity and are just bump objects with one common filter function.
However "ineffective" that may seem, I'm very satisfied with the results, as I always can check sensors with ease and convenience.

Say, I want to jump if there's an obstacle to the left and there's an open space above that obstacle:

Code: Select all

if entity.left_sensor.collided then
   if not entity.left_jump_sensor.collided then
      entity.jump()
   end
end
Or, say, I want to turn the enemy if it can't jump over the void:

Code: Select all

if entity.left_sensor.collided then
   if not entity.left_void_sensor.collided then
      if entity.left_max_jump_distance_sensor.collided then
         if not entity.left_jump_sensor.collided then
            entity.jump() -- jump if it's possible to jump over and there's nothing in the way
         else   
            entity.turn_around() -- turn around if it's possible to jump over, but something is in the way
        end
      else
        entity.turn_around() -- if it's impossible to jump over, then why bother?
     end
   end
end
You don't even need to reset the sensors, as bump will take care of everything except your entity reaction to the changing world.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 8:47 pm
by slimefriend
@Tjakka that method works! I wish i knew why my method wasn't working, but this will do for now. Thank you!

@4aiman: I've never thought of that kind of system before. I likely won't be needing that kind of system for this game, but I'm going to try and keep that idea in mind for later.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sat Dec 03, 2016 9:12 pm
by Tjakka5
Happy you got it to work! :D

4aiman: I can't believe I hadn't ever thought of that! I'll definitly use a similair system from now on.

Re: Having trouble detecting if something ISN'T colliding using bump.lua

Posted: Sun Dec 04, 2016 9:31 am
by 4aiman
Well, the idea isn't mine at all. Although the search feature is a bit inconvenient, these forums have a lot to offer for sure. :)