1 year ago
#339035

HenryFBP
Having trouble getting a ray cast to collide from a player to a wall in HaxeFlixel
I am trying to cast a ray from the center of a player to the point in front of them. This is to implement "mining" of collideable tiles. Once I know that a ray collided with a tile, then I can destroy it and drop an item below it.
I added a "debug" line in blue that visualizes the raycast as well...but it never shows up as red, it never detects a collision.
The game can be played here: https://henryfbp.github.io/MountainFlow/ (press F12 to see console)
Source code with the issue: https://github.com/HenryFBP/MountainFlow/blob/b6b0c95b3900298cdf4ac000cf3c71f2fa596669/source/StatePlayProcGen.hx#L119
didHitTile
should be true
in this image, but for some reason it is not. Is it possible I forgot to set some property on my tiles or something?
Thanks so much.
I tried to add debugging print statements, and also drew a line over the ray, to see if my coords were messed up. But, maybe I misunderstand the behavior of the FlxTilemap.ray()
method. I believe that I am using it correctly:
var center = player.getMidpoint();
var endPoint = player.getPointInFrontOfMe();
trace("casting a ray from " + center + " to " + endPoint);
var collidePoint = FlxPoint.get(); // we are passing this by reference...
var didHitTile = !(walls.ray(center, endPoint, collidePoint));
FlxSpriteUtil.fill(line, 0);
FlxSpriteUtil.drawLine(line, center.x, center.y, endPoint.x, endPoint.y, {thickness: 2, color: FlxColor.BLUE});
trace("didHitTile = " + didHitTile);
But didHitTile
is always false
.
EDIT: I ended up finding a way around this, (see devel
branch) by writing custom code, but I am still not able to use the FlxTilemap.ray()
method, for all intents and purposes the method is useless to me.
haxe
haxeflixel
0 Answers
Your Answer