Page 1 of 1

Lasers with box2d

Posted: Tue Mar 22, 2011 3:54 am
by slime
What is a good, clean way to have laser-type (instantaneous beam which will go in a straight line to its max distance or to any object it hits, with a long lifetime) objects using the box2d API? Stuff I've tried has been very hack-ish and doesn't work quite right.

Re: Lasers with box2d

Posted: Tue Mar 22, 2011 4:21 am
by Taehl
Well, /technically/ a laser wouldn't have any effect on an object's velocity. :P

But to achieve what you want to do, I guess you would need to make a function that checks if any bodies intersect with a given line, then impart velocity appropriately.

Re: Lasers with box2d

Posted: Tue Mar 22, 2011 4:27 am
by slime
Right, I'm not too worried about the collision response, I just want something that works well with box2d and won't cause performance problems (line detection might have slowdowns because as far as I'm aware I would be checking against every damageable object in the game, which could be a lot).

Re: Lasers with box2d

Posted: Tue Mar 22, 2011 8:21 am
by Robin
Things like spacial hashes and quadtrees might help.

Re: Lasers with box2d

Posted: Tue Mar 22, 2011 8:28 am
by kikito
What about a 3-pixel-width rectangular sensor shape (or several of them aligned)?

You can detect collisions with them, and at the same time they "go through" everything.