Raycasting issue: wry neck

32 views
Skip to first unread message

Egar Almeida

unread,
Jul 23, 2015, 5:35:13 PM7/23/15
to HaxeFlixel
Hello everyone,

I'm trying to use FlxTilemap.ray() to determine a line of vision between my player and the mouse.

The problem I have is that what I wrote only seems to work for coordinates that are (more or less) in the fourth quadrant of a cartesian plane (with the player being (0, 0)). 

This is the code:

var playerPoint:FlxPoint = getMidpoint();
var mousePoint:FlxPoint = FlxG.mouse.getWorldPosition();
var resultPoint:FlxPoint = FlxPoint.get();
if (!_tilemap.ray(playerPoint, mousePoint, resultPoint, 100))
{
var tileIndexByRay:Int = _tileManager.getTileIndex(resultPoint.x, resultPoint.y);
var tileIndexAtMouse:Int = _tileManager.getTileIndex(mousePoint.x, mousePoint.y);
if (tileIndexByRay == tileIndexAtMouse)
{
_shooting = true;
_tileManager.drillTile(mousePoint.x, mousePoint.y, _weaponData.damage);
}
}

and this is the getTileIndex function called there:

public function getTileIndex(X:Float, Y:Float):Int
{
return Math.floor(Y / PlayState.TILE_HEIGHT) * _tilemap.widthInTiles + Math.floor(X / PlayState.TILE_WIDTH);
}

The other function, drillTile() works well on its own.

Either my player has a wry neck or a condition similar to Zoolander's, but it doesn't seem to be able to look to the left or up on the screen...

Thanks in advance for any help!


Egar Almeida

unread,
Jul 25, 2015, 6:52:52 PM7/25/15
to haxef...@googlegroups.com
Sorry to insist, but I haven't found a fix yet. Any ideas would be really helpful!

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to the Google Groups "HaxeFlixel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/6f1a09f7-0114-41cd-8c33-e3d29f71ad54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Egar Almeida

unread,
Jul 29, 2015, 9:38:59 AM7/29/15
to haxef...@googlegroups.com
Ok, just in case anyone was wondering, I think I've found where the error was, but I'm not 100% sure:

What I was trying to do: I shoot a ray from the player's coordinates to the mouse's coordinates. If the ray hits a tile, I compare the index of the tile at the hit coordinates with the index of the tile at the mouse's coordinates. If they are the same, I remove said tile. 

The problem: Hitting any tile that was above the origin of the ray (the player's position) or to its left, resulted in an incorrect value for the index. The coordinates resulting from the raycast, for any right-wall hit seem to actually correspond to its right-side neighbor's left-wall. For any bottom-wall hit, the coordinates seem to correspond to its bottom-side neighbor's top-wall.

The solution: For now, I'm just checking the position of the origin point in relation to the end point, and substracting 1 to the corresponding resulting coordinate.

I thought it was a rounding issue with my "getTileIndex" function, and checking the dev branch of the FlxTilemap I found that now this function is implemented, so I replaced mine with the one in gitHub but that didn't solve it.


Is there a problem with ray() or am I doing something wrong?

Cheers,



Reply all
Reply to author
Forward
0 new messages