|
Hi! I’m going to make a post about how I solved my shadowcasting for a summer project that is a top down fighting game! First I made a wall object It’s just a simple square. I had a playerobject since earlier, let’s include that on the picture… Like that… This is how I wanted the shadows to be drawn:

So I thought of it as point perspective 3D sketch, like this…

This giganting square is an object I added to the wall object. I then drew sf::Shapes between the corners of the squares and filled them with a layer of grey, drawing above everything else (it’s not drawing above everything else in the picture, but still!)
The big square is actually alot bigger in the project, and can thus be drawn outside the camera’s view, like this:
(camera is blue square)
Then I just had to make the big square stay on the opposite side of the small square from the player. To do this, I calculated the player’s x- and y-distance from the square and added the opposite to the giant square. But since the giant square is like 50 times bigger in my code I had to multiply that distance by 49. Why 49? That’s hard to explain… It’s like scrolling with parallaxes… If we say that the big square is twice the size of the small, for simplicity…. BAH, I can’t explain it without my fingers and a face to talk to.
It has to move 1 time less anyway. If the big square is 10000000 times the size of the small one it has to have the oposite position of the player multiplied by 9999999, because otherwise it’s position will be wrong.
But yeah that’s pretty much it. I have thought alittle bit about optimizing it buuut…. I have not done that. If I were to I would probably just draw the shadow-rectangles necessary to get the shadow effect from a certain angle, instead of always drawing all of the rectangles.

|