World:rayCast callback with no fixture
Posted: Fri Mar 17, 2017 6:06 am
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.
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
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
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