Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Manic

Is there any way to get entities inside of an area (for instance, a radius around a point), potentially combined with a getEntitiesByType method? I know how to code it inefficiently (get entities by type and then check the distance from the point), but is there any way to do it better?

1 decade ago by lTyl

You basically have to do a collision check for every entity on the game screen and compare to see if the entity position falls within the rectangle/circle/whatever shape.

If you are creating a rectangle selection, then it will be: top= rectangle.x, right = rectangle.x + rectangle.width, bottom = rectangle.y + rectangle.height, left = rectangle.y. Once you have the dimensions of your selection rectangle, you simply compare the entity position to the dimensions of the rectangle. If the entity X and Y falls within the dimensions of the selection rectangle, then you know the entity is within the range.

For a circle-rectangle collision: this is probably a slow method of doing it, but you get the center point of the circle and the radius and then calculate the distance from the circles center to each point of the rectangle (see above). If the distance between the point and circle center is lower than the radius of the circle, then you know the rectangle is within the circle
Page 1 of 1
« first « previous next › last »