Best way for collision detection for lines?


(Adam Edney) #1

My tile map has tiles which have 45 degree walls in them, e.g. a corner. I know you can’t add that to the collision grid, but how would i do this in flash punk?, its quite easy in box2d in java, i done this many of time. So what the fastest and most affective why of doing it ?


(Bora Kasap) #2

You can use flashpunk’s collideLine(); function.

You can reach it from the world clas by using this.collideLine();

Or you can reach it from entities by using this.world.collideLine();

If that’s what you are looking for.


(Adam Edney) #3

Thanks for the reply, so there isn’t really information out there about collideline, how do you use it? do you simply just do this to create a line…

this.collideLine("line", 0, 0, 400, 400, 1,null)

then to check if there is a collision i just do this

if (player.collideTypes("line", player.x, player.y))
{
	trace("Collide");
}

because it doesn’t seem to work, but you can’t see the line. any suggestions?


(Ultima2876) #4

If you have experience with Box2D, you could use that to handle your collisions and physics. It integrates with FlashPunk quite easily!

If you want to go with the ‘pure’ FlashPunk approach, you might want to mess with a PixelMask (in your WallCorner or whatever entity, use mask = new PixelMask(bitmapData), with a bitmapData that mimics the wall’s shape). PixelMasks are slightly slower than regular collision testing but only if there is already a hitbox collision (ie the player is already within the hitbox).

CollideLine docs are here: http://useflashpunk.net/docs/net/flashpunk/World.html#collideLine() It’s really a type of specialised check used similarly to collideTypes. If you wanted to use that to check for this type of collision you’d have to reverse the logic somewhat, so that the walls themselves check for collision against the player in their own update cycle, then if there’s a collision they report back to the player to handle it:

//in your WallCorner entity
public override function update(): void
{
  //make sure your Player entity sets its type to "player"!
  var collidedPlayer: Entity = world.collideLine("player", this.x, this.y, this.x + 32, this.y + 32)
  if(world != null && collidedPlayer)
  {
    //collided with entity of type "player"!
    (collidedPlayer as Player).cornerCollision();  //handle the collision in the player's cornerCollision function
  }
}

That kind of thing.


(azrafe7) #5

Some time ago I made an attempt to port HaxePunk’s Polygon and Circle masks into FlashPunk (see Round and triangle hitboxes?).

Cannot guarantee they’re bug-free (haven’t touched them since), but you can test them yourself (…and report back! :smirk:).

Otherwise you can try to roll your own line-rect collision detection, google (and stackoverflow) will give you plenty of articles on the subject.


(Ultima2876) #6

Nice. I wonder if with some extensive testing we can bring those into the main FlashPunk branch?


(azrafe7) #7

Yeah, it’s definit’ly possible!

Only thing is that testing/writing tests for this kind of stuff is not that easy (not for me at least). And in any case it would be wise to adjust those classes to match the current HaxePunk’s Polygon and Circle implemenation (some more work has been done there).

Note: I seem to recall that there was a subtle issue with offsets/origins/parent-coords, but cannot remember exactly what that was… anyway the code is open (in all the interested projects), so anyone wishing to play with it is more than welcome to do so (and yeah I’ll give it a shot if I have time :smirk:).


Polygon and Circle masks for FlashPunk (round 2)