World:rayCast callback with no fixture

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
triggdev
Prole
Posts: 1
Joined: Fri Mar 17, 2017 5:59 am

World:rayCast callback with no fixture

Post by triggdev »

I'm trying to program a rayCast for shooting a bullet. The rayCast collision is detected but there is no fixture in the callback function. Here's the bullet class I'm using.

Code: Select all

Object = require "classic"
Bullet = Object:extend()

function Bullet:new(world, shooter)
  self.world = world
  self.angle = shooter:getAngle()
  self.x = (shooter:getX() - 
    (10 * math.sin(self.angle))) + (30 * math.cos(self.angle))
  self.y = (shooter:getY() + 
    (10 * math.cos(self.angle))) + (30 * math.sin(self.angle))
  
  self.distance = 500
end

function Bullet:fire()
  local endX = self.x + (self.distance * math.cos(self.angle))
  local endY = self.y + (self.distance * math.sin(self.angle))
  
  self.world:rayCast(self.x, self.y, endX, endY, self.hit)
end

function Bullet:hit(fixture, x, y, xn, yn, fraction)
  print("Looks like we hit something")
  return 1
end
The message "Looks like we hit something" prints properly, but the variable fixture is always a number and fraction is nil. I printed all of the values out and it looks like an actual Fixture object is not included, only x, y, xn, yn, and fraction. Am I using this wrong?

EDIT: I found the issue. I needed to declare the function self.hit as Bullet.hit, not Bullet:hit. Once I did that it returned the fixture properly.

Code: Select all

-- Fixed code
function Bullet.hit(fixture, x, y, xn, yn, fraction)
  print("Looks like we hit something")
  return 1
end
Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests