"Help" with RayCasting

Hey guys,
Im using Ashley and LibGDX to make a top-down game, but i wanted to use my own “physics engine”.
In my previous projects i always integrated Box2DLights for lighting, but since it uses Box2D to do the math, i had a problem.
So i downloaded the sources of Box2DLights and changed it to use Entities instead of Bodies.
Everything is working fine, except the shadows. I cant get raycasting to work.

TL;DR Teach me how 2 raycast pls (or point me in the right direction)

Additional info:
-2D Environment
-Each entity has a Position and CollisionBox component.
-CollisionBox has 2 fields : size and offset
-Engine object manages enities. You can get any entity or group you want at any given time
-The function that gets called for raycasting:

public static void rayCast(Engine engine, RayCastCallback ray, Vector2 start, Vector2 tmpEnd){
		//TODO: Implement raycasting
}

-What it should do in the end:

ray.reportRayFixture(Entity body, Vector2 point, Vector2 normal, float fraction);

Please? Im sure someone on this forum knows something about the topic.

Can those boxes rotate? Are they in uniform grid or can they move freely?
Easiest way is to just use bruteforce ray vs all objects test and choose always the collision that is nearest. Libgdx probably has segment vs box test that you can use.


public static void rayCast(Vector2 start, Vector2 tmpEnd)
{
  hitFraction = 1.0f
  for all boxes
    hitFraction = min(hitFraction, raySegmentVsBoxTest())

 tmpEnd = startPos + fraction * (tmpEnd - startPos) 
}

No, they cant rotate, and they can move around freerly.
Heres what i got so far:
Code.

http://puu.sh/f11k3/408a4a2966.png

Keep it simple and minimal. Box2dLights does not need collision normal so don’t pretend that its needed. Also maybe whole call back business can be streamlined to more convenient style. Also lot of redudancy in code which usually tend to cause more bugs.

Like this one is just calculating these values second time.

point.dst(start)/end.dst(start)

Also you need to calculate this outside of entity loop float maxDst = end.dst(start); and only call ray.reportRayFixture(entity, point, normal, point.dst(start)/end.dst(start)) if calculated fraction is smaller than previous ones. This is box2d behavior and box2dLights expect that kind caller logic.

Edit:
Also if you need masking you need to take callback return values(negative fraction value implies that that entity should’t collide with the ray.

			if ((filterA != null) && !contactFilter(fixture))
				return -1;

I always clean the code up after i’m sure its working as intended. I will keep fiddling with the system, but for now, i have this (moved some code from my previous game):

http://puu.sh/f1qex/1f78435b3a.png

http://puu.sh/f1qa1/8fb1e8dd35.png

It even works when the tiles are in motion!

http://puu.sh/f1r5B/eb9086d2f1.png

http://puu.sh/f1rcN/2187321ae1.png

Thank you for your help, i will probably convert the code to .jar when i succeed in porting the framework and post it here for wanderers.

Can you show the updated code? How good is the performance?

Actually its really medium quality if it comes to actually determining if the entity is visible to the light or not. It casts a ray from the center of the light to the center of the entity, and lights the entity up if there are no bodies between them. What i will probably do is cast the ray to each 4 of the entity’s corners, and one to the middle. If at least one is “clean”, it will light it up.
And there needs to be some optimization (only updating entities on the screen) and cleanup done.
Performance-wise its also medium i think. Right now the amount of entities on screen is small so there are no problems, but if f.ex. there would be 20 mobs, some junk and particles, the performance will decrease. Oh, yea, filters, i will need to implement filters.

Code.

http://www.redblobgames.com/articles/visibility/