1

Im trying to write a game in 2D with Sfml. For that game i need a Lightengine and some code that can give me the area of the world that is visible to the player. AS both problems fit very well together (are pratically the same) i would like to solve both problems at once. My world will be loaded from files in which the hitboxes of objects will be represented as Polygons. I now wrote some code that takes a list of Polygons and the Direction of a Ray that follows the mouse and finds the closest intersection with any of these polygons. The next step now would be to cast rays from the players or lights Position towards the edges of the polygons, aswell rays offset by +-0.000001 radians to determine the visible area and give it back as a polygon.

The Problem though is that my algorithm (it calculates the inersection between two lines with vector mathematics) is too slow. In my very good PC i get 100fps with 300 egdes and one Ray.

I now read many articles online but couldnt find one best solution. But as far as i read it should be much faster to calculate intersections with triangles.

My question now: would it be meaningly faster to triangulate the polygons once while loading the map and then use ray-triangle intersection or is there any better way that you know of to solve my problem? I also heard of bounding Volumen hierachies but i dont know howmuch impact that would have.

Im a bit surprised of how much power my algorithm consumes, as it only has to calculate some 2 dimensional intersections...

4

1 回答 1

1

对于寻找解决方案的每个人,我最终选择了:

我发现了 Box2D 物理引擎,现在我正在使用 b2World::RayCast(...) 函数来确定光线是否以及在何处击中场景中的对象。现在一切正常和顺利(还没有确切的基准):) http://www.iforce2d.net/b2dtut/world-querying 我在这个网站的帮助下让它工作祝你有美好的一天!:)

于 2015-07-28T15:10:03.683 回答