Skip to content

v1.0 Single Hardcoded Wall

Ayush Basak edited this page Jun 22, 2021 · 1 revision

v1.0 screenshot

For test purposes I created a hard coded wall on the 2D space and ran the collision of rays on it.

Main.cpp

sf::Vertex testWall[] = {
        sf::Vector2f(40.0f, 300.0f),
        sf::Vector2f(500.0f, 100.0f)
};

While writing this project, I did not focus too much on the time and space complexity of the raycast. which created some problems and slowdowns later.

Initially I was storing the angles of each individual ray in a float vector.

Player.h

float theta;
std::vector<float> angles;

Each angle was del_angle apart from the previous angle.

120deg = 2.09rads

Player.cpp

for (int i = 1; i <= this->ray_density;i++) {
        float del_angle = 2.09f / this->ray_density;
        this->angles.push_back(this->theta + del_angle * i);
    }

It was a pretty bad idea to store angles individually

void Player::rotateClockWise(float angle)
{
	this->theta += angle;
	if (theta >= 2 * PI)
		theta -= 2 * PI;

    /*
    This function added another O(N) level of time complexity,
    every time the player was rotated.
    */
	for (int i = 0; i < this->ray_density;i++) {
		this->angles[i] += angle;
		if (this->angles[i] > 2 * PI)
			this->angles[i] -= 2 * PI;
	}
}

To stop the ray at the wall of collision, we need the intersection point of the ray and the wall.

code from userexit

Another important bug I faced was jittery movement of the player object.

Since every draw call in the function is dependent on the framerate, constant values for motion gives this uneven feel to the gameplay.

So, I used delta time. This is the difference in time between in each draw call multiplied with the speed of motion of the player, smoothens the movement.

refer more about delta time wikipedia The Cherno

And, that concluded this version of the raycaster.

Clone this wiki locally