Hello,
In my game, I'm trying to create a laser tripmine (think those green boxy things with the blue lasers in the original Half-Life). The basic idea is that when the player is within a certain distance of, and is facing, a PhysicsObject of type Obstacle, they can place the mine and it will "attach" to the Obstacle. The mine has a bit of a laser graphic sticking out of it that fades out. The mine will then detect if there is another Obstacle across from it within a certain distance; if there is, it draws an end piece of the laser from that point, also fading out, and pointing back at the mine.
I have some of this working already. I can place the mine (currently just anywhere I drop it), and it draws the end piece of the laser at whatever distance away I tell it. To solve the problem of detecting nearby Obstacles, I believe I need to use raycasting. I've been trying to follow Emanuele Feronato's tutorial on slicing objects with Box2D, since he uses raycasting to determine if an object is being "sliced", but I can't get it to work right.
Here is my code (I'm using the Citrus Engine, but it uses Box2D for all of its physics):
The idea is that I would update _laserLength with the distance between the ray's start point and its intersection point with an Obstacle. Since the levels of my game are surrounded with Obstacles, it should hit one eventually (and they're small levels). Before, I didn't have the Sprite in there with the event listener, since I figured the raycast only needed to be called once. But I never got a response in the laserFired() callback, so I added the event listener; unfortunately, I'm still not getting anything from that function. The event listener just fires without ever stopping.
Am I implementing the raycast wrong? Is there something that I'm missing? Should I use something else instead? Can anyone tell me what I'm doing wrong?
In my game, I'm trying to create a laser tripmine (think those green boxy things with the blue lasers in the original Half-Life). The basic idea is that when the player is within a certain distance of, and is facing, a PhysicsObject of type Obstacle, they can place the mine and it will "attach" to the Obstacle. The mine has a bit of a laser graphic sticking out of it that fades out. The mine will then detect if there is another Obstacle across from it within a certain distance; if there is, it draws an end piece of the laser from that point, also fading out, and pointing back at the mine.
I have some of this working already. I can place the mine (currently just anywhere I drop it), and it draws the end piece of the laser at whatever distance away I tell it. To solve the problem of detecting nearby Obstacles, I believe I need to use raycasting. I've been trying to follow Emanuele Feronato's tutorial on slicing objects with Box2D, since he uses raycasting to determine if an object is being "sliced", but I can't get it to work right.
Here is my code (I'm using the Citrus Engine, but it uses Box2D for all of its physics):
Code:
public function Tripmine(name:String, params:Object=null)
{
params.view = "assets/img/tripmine.png";
var distX:Number = params.x - Registry._gs.player.x;
var distY:Number = params.y - Registry._gs.player.y;
params.angle = Math.atan2(-distY, -distX);
params.rotation = params.angle*Registry.rotateBy;
params.damage = 8;
params.static = true;
trace("Creating tripmine");
super(name, params);
_laserMaxLength = 100;
_laserSegmentStart = new V2(x+6, y);
cos = Math.cos(angle);
sin = Math.sin(angle);
_laserSegmentEnd = new V2(x+_laserMaxLength*cos, y+_laserMaxLength*sin);
//ray cast from start x,y
//if Obstacle is <= max length of laser,
//plot laser end
//otherwise, do nothing
spr = new Sprite();
spr.addEventListener(Event.ENTER_FRAME, updateWorld);
_world = new b2World(new V2(0,10));
_laserLength = 100;
if(_laserLength <= _laserMaxLength)
{
var _laserX:Number = x+_laserLength*cos;
var _laserY:Number = y+_laserLength*sin;
_laserSegmentIntersect = new V2(_laserX, _laserY);
_laserEnd = new BulletLaser("laserEnd", {x: _laserSegmentIntersect.x, y: _laserSegmentIntersect.y, rotation: Math.atan2(distY, distX)*Registry.rotateBy});
Registry._gs.add(_laserEnd);
}
}//
private function laserFired(fixture:b2Fixture, point:b2Vec2, normal:b2Vec2, fraction:Number):Number
{
trace("LASER FIRED CALLBACK");
trace("intersection at x: "+point.x+", y: "+point.y);
spr.removeEventListener(Event.ENTER_FRAME, updateWorld);
return 1;
}
private function updateWorld(e:Event):void
{
trace("update");
_world.Step(1/30,10,10);
_world.RayCast(laserFired, _laserSegmentStart, _laserSegmentEnd);
}//
{
params.view = "assets/img/tripmine.png";
var distX:Number = params.x - Registry._gs.player.x;
var distY:Number = params.y - Registry._gs.player.y;
params.angle = Math.atan2(-distY, -distX);
params.rotation = params.angle*Registry.rotateBy;
params.damage = 8;
params.static = true;
trace("Creating tripmine");
super(name, params);
_laserMaxLength = 100;
_laserSegmentStart = new V2(x+6, y);
cos = Math.cos(angle);
sin = Math.sin(angle);
_laserSegmentEnd = new V2(x+_laserMaxLength*cos, y+_laserMaxLength*sin);
//ray cast from start x,y
//if Obstacle is <= max length of laser,
//plot laser end
//otherwise, do nothing
spr = new Sprite();
spr.addEventListener(Event.ENTER_FRAME, updateWorld);
_world = new b2World(new V2(0,10));
_laserLength = 100;
if(_laserLength <= _laserMaxLength)
{
var _laserX:Number = x+_laserLength*cos;
var _laserY:Number = y+_laserLength*sin;
_laserSegmentIntersect = new V2(_laserX, _laserY);
_laserEnd = new BulletLaser("laserEnd", {x: _laserSegmentIntersect.x, y: _laserSegmentIntersect.y, rotation: Math.atan2(distY, distX)*Registry.rotateBy});
Registry._gs.add(_laserEnd);
}
}//
private function laserFired(fixture:b2Fixture, point:b2Vec2, normal:b2Vec2, fraction:Number):Number
{
trace("LASER FIRED CALLBACK");
trace("intersection at x: "+point.x+", y: "+point.y);
spr.removeEventListener(Event.ENTER_FRAME, updateWorld);
return 1;
}
private function updateWorld(e:Event):void
{
trace("update");
_world.Step(1/30,10,10);
_world.RayCast(laserFired, _laserSegmentStart, _laserSegmentEnd);
}//
The idea is that I would update _laserLength with the distance between the ray's start point and its intersection point with an Obstacle. Since the levels of my game are surrounded with Obstacles, it should hit one eventually (and they're small levels). Before, I didn't have the Sprite in there with the event listener, since I figured the raycast only needed to be called once. But I never got a response in the laserFired() callback, so I added the event listener; unfortunately, I'm still not getting anything from that function. The event listener just fires without ever stopping.
Am I implementing the raycast wrong? Is there something that I'm missing? Should I use something else instead? Can anyone tell me what I'm doing wrong?