How to reference Bump's "bounce variable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

How to reference Bump's "bounce variable

Post by ward614 »

Hello all,

I am working on collision detection using bump for the first time, and I have run into a situation where a need to access the cols.bounce.x and cols.bounce.y variables (looking to continue the bouncing motion by changing the movement direction), however I am getting an error that it is attempting to index a nil value when trying to resolve this variable. I am sure it is something simple, but I am missing it. any help would be greatly appreciated!

Code: Select all

function listofbad.move(dt)

 for i,v in ipairs(listofbad) do
    
    --Movement
     
    local goalX = v.x + (v.speed*v.xdir*dt)
    local goalY = v.y + (v.speed*v.ydir*dt)
    
    local actualX, actualY, cols, len = world:move(v, goalX, goalY, bounce)
    v.x, v.y = actualX, actualY
    
    for i = 1, len do
    	local col = cols[i]
      
      if cols[v].bounce.x > v.x then
      
        v.dirx = 1
      
      else
      
        v.dirx = -1
      
      end
    
      if cols[v].bounce.y > v.y then
      
        v.diry = 1
      
      else
      
        v.diry = -1
      
      end
    end
        
  end

end
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How to reference Bump's "bounce variable

Post by MrFariator »

When you are looping through the collisions, you're accidentally using the wrong variable to access the list of collisions:

Code: Select all

for i = 1, len do
  if cols[v].bounce.x > v.x then -- the '[v]' should be '[i]'
    -- ...
  end
end
In addition, it might be wise to first check that the collision response is a bounce one in the first place. This avoids issues in cases where a different type of collision is detected, potentially leading to another nil index error.

Code: Select all

for i = 1, len do
  if cols[i].bounce and cols[i].bounce.x > v.x then -- check that cols[i].bounce exists first, before touching cols[i].bounce.x
    -- ...
  end
end
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Re: How to reference Bump's "bounce variable

Post by ward614 »

Thank you! I knew it had to be something silly! That being said, I am still getting no return from the col.bounce. Even when the sprite on screen has clearly collided none of my direction change if statements are activating, nor does a statement asking if col.bounce returns true. Any insight you have would be greatly appreciated!

Code: Select all

function listofbad.move(dt)

 for i,v in ipairs(listofbad) do
    
    --Movement
     
    local goalX = v.x + (v.speed*v.xdir*dt)
    local goalY = v.y + (v.speed*v.ydir*dt)
    
    local actualX, actualY, cols, len = world:move(v, goalX, goalY, bounce)
    
    
    for i = 1, len do
    	local col = cols[i]
      if col.bounce then print("Yes") end
        if col.bounce and col.bounce.x > v.x then
      print("Right")
          v.dirx = 1
      
        elseif col.bounce and col.bounce.x < v.x then
      print("Left")
          v.dirx = -1
                 
        elseif col.bounce and col.bounce.y > v.y then
      print("Down")
          v.diry = 1
      
        elseif col.bounce and col.bounce.y < v.y then
      print("Up")
          v.diry = -1
      
        end
      
    end
        v.x, v.y = actualX, actualY
  end

end
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How to reference Bump's "bounce variable

Post by MrFariator »

Going to need to see more code to properly inspect what the issue could be. Can you provide a minimal working example? At the very least, going to need to see what your collision filter is ("bounce" in your code), and how you have defined your entities (the contents of "listofBad").
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Re: How to reference Bump's "bounce variable

Post by ward614 »

Okay, so it turns out that having a collision filter is super necessary for this and I was trying to directly pass "bounce". After adding one in, I was able to get it working! Thank you!
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How to reference Bump's "bounce variable

Post by MrFariator »

Yeah, filters are the bread and butter of how to make your bump objects behave the way you want them. If you tried to pass bounce as an underfined variable (global or otherwise), or leave the parameter empty (nil), bump will register any and all collisions. Depending on the type of function, these collisions are either registered as of type "slide" (eg. world:move or world:check), or the world will just return a list of detected objects (eg. world:queryRect).

So, if something is behaving incorrectly, always a good place to check is if the filter you are using is a non-nil value.
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests