mail

Billiard

Portfolio Page

Introduction

I made this game in the first year of the NHTV, it is entirely made in C++. This was a solo project. I used for this my own openGL render, physics and collision system. The OpenGL is on an older version as the project I made in my second year.

The game is a billiard game where you have to get 3 balls in the holes. With the mouse you can apply force by holding the mouse button shorter or longer. The line will indicate where the ball is headed towards.

My work

1
2
3
4
5
6
//Make the ball the player has  to shoot
ControllableBall* b = new ControllableBall();
m_PlayerBall = m_Engine->CreateGameObject("assets/Sprites/player_ball.png", b, "PlayerBall", true);
m_PlayerBall->sprite->SetPosition(500, 500);

m_Engine->AddCollision(m_PlayerBall, CollisionTypes::CIRCLE, true, false);

Above you see how I make in the game.cpp the player ball. I first make a class, ControllableBall, which is an inheritance of ball, which on it is turn inherits from ObjectInterface which is an engine class. Then I call an engine function which registers the ControllableBall as a game object within the engine. As arguments I give it an url to a sprite, the class, the name of the object and lastly true for rigid body. This will make sure it gets a rigid body which means I can apply forces to it easily with the AddForce engine function. Then I set a position on the sprite via the engine. I then add a circle collision to it with the AddCollision call. In this call you give the object, then the type of collision, then if it has motion (needed for calculations) and then if it is a trigger or not.

As you can see the engine I made is quite extensive and is easy to use for making small 2D games. See for a deeper look at the engine code my portfolio piece; Own OpenGL engine.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (m_RigidBody->IsMoving())
{
	//Calculate the vector between the 2 positions (it will go through the collision point) to send the other object to
	glm::vec2 colVec = _CollidingObject->GetPosition() - _OwnCollider->GetPosition();
	glm::vec2 colVecNormal = glm::normalize(colVec);

	glm::vec2 perpendicular = { colVecNormal.y, -colVecNormal.x };

	//Calculate the angle between the perpendicular and your current direction, if it is bigger than 90 degrees the perpendicular is the wrong way
	float angle = Maths2D::GetAngleBetweenVectors(perpendicular, m_RigidBody->GetCurrentDirection());
	if (angle > 90)
		perpendicular = { -colVecNormal.y, colVecNormal.x };

	float dotProduct = glm::dot(m_RigidBody->GetCurrentDirection(), perpendicular);
	if (dotProduct > 0 && dotProduct < 1)
	{
		glm::vec2 veloCity = m_RigidBody->GetCurrentVelocity();

		//We got the directions now so apply the forces
		_CollidingObject->GetGameObject()->GetRigidBody()->AddForce(veloCity * (1 - dotProduct), m_RigidBody->GetCurrentFriction(), colVecNormal);
		m_RigidBody->AddForce(veloCity *  dotProduct, m_RigidBody->GetCurrentFriction(), perpendicular);
	}
}

Above you see the calculations for applying the force in the PlayerBall class. You see that it heavily relies on the engine I made myself. For example, it uses AddForce on both the other object and player ball itself. It calculates the force by using the velocity and the dot product.