Showing posts with label Sample Problem. Show all posts
Showing posts with label Sample Problem. Show all posts

Thursday, January 24, 2019

Sample Problem: C++ - Order of Execution of Constructors & Destructors Between a Parent & Child Class

You have a parent class that has a constructor and a non-virtual destructor, as well as a child class that inherits from the parent class. What is the execution order for the constructors and destructors?


  1. Parent constructor is called
  2. Child constructor is called
  3. Child destructor is called
  4. Parent destructor is called
Source Code
Source.cpp
#include "Parent.h"

#include "Child.h"
#include <iostream>

int main()
{
Child* testChild = new Child();
testChild->~Child();

int endInput;
std::cin >> endInput;

return 0;
}

Parent.h
#pragma once
#include <iostream>

class Parent
{
public:
Parent();
~Parent();
};

Parent.cpp
#include "Parent.h"

Parent::Parent()
{
std::cout << "Parent Constructor Called" << std::endl;
}

Parent::~Parent()
{
std::cout << "Parent Destructor Called" << std::endl;
}

Child.h
#pragma once
#include "Parent.h"
#include <iostream>

class Child : Parent
{
public:
Child();
~Child();
};

Child.cpp
#include "Child.h"

Child::Child()
{
std::cout << "Child Constructor Called" << std::endl;
}

Child::~Child()
{
std::cout << "Child Destructor Called" << std::endl;
}

Console Output



Wednesday, January 16, 2019

Sample Problem: Rotating a 2D Character

The Problem
In a top down shooter, there is a scripted in game cutscene where an explosion happens and the player must turn to face the explosion. The turn to face the explosion must be a smoothed transition and since the cutscene can be triggered in a variety of ways, the initial direction of the player can be different between playthroughs. We need to know the angle the player needs to rotate and whether the direction they need to rotate is clockwise or anticlockwise.

The Variables
The current variables we have access to is the initial direction the player is facing (Vector C) , the position of the player (P) and the position of the explosion (E).

The Solution
The first step is to calculate the direction towards the explosion from the player, which can be done by subtracting P from E. This gives us Vector N and since we don't care about the distance between the two positions, we can normalise Vector N.

Since we now have two unit vectors (Vector N and Vector C), we can calculate the angle between them by using the dot product calculation. So the angle is equal to arccos(dotProduct(Vector C, Vector N)). However, this doesn't tell us if the direction we need to rotate is clockwise or anti clockwise.

This can be done with the cross product. However, since both vectors are in 2D space, they need to be converted into 3D vectors. Once the cross product is calculated we can determine the direction of the rotation needed. If the z component of the resultant vector is positive then the rotation is anti clockwise, which means if the z component is negative then the rotation is clockwise.

Friday, January 11, 2019

Sample Problem: Vector Reflection

Say we have a scenario, where a ball is moving towards a wall, if the ball hits a wall then it will reflect off of it. If the wall is parallel to an axis then this is fairly simple, we just invert the velocity of the ball in the axis perpendicular to the walls axis. For example, if the wall is on the x axis we would invert the velocity of the ball in the y axis. However, its not that simple should the wall not be parallel.

The formula to solve this is: Vi = Vo - 2n(Vo * n)
where Vi is the final velocity after the ball has been reflected, Vo is the initial velocity before the ball has been reflected and n is the normal of the wall that the ball will reflect on. All of which are vectors.

How this formula is derived

At the start of the problem we know two variables, the inital velocity and the normal of the wall, which is a unit vector. If we had a vector that started at the tail of the inital velocity and ended at the head of the final velocity, then we could use vector subtraction to figure out what the final velocity is. The parallel vector that shows the distance the ball will travel from its inital point to the wall is s and we can presume that it will travel the same after the bounce therefore our vector we need for the subtraction is 2s. This means we need to find s.

So currently Vi = 2s - Vo

To find s we would need to extend n so that it would reach the head of the s vector. This can be achieved by using vector projection and inverting the inital velocity to find the distance. Doing so gives us -Vo * n. As n is a unit vector we can multiply n by the scalar distance we just found. This gives us extended n = n *  (-Vo * n). Then by using vector addition we can find s, so s = Vo + n * (-Vo * n).

Now that we have the value of s we can subsitute that value into our original equation and then simplfy.

Vi = 2(Vo + n * (-Vo * n)) - Vo

Vi = 2Vo + 2n * (-Vo * n) - V0

Vi = Vo + 2n * (-Vo * n)

Vi = Vo - 2n * (Vo * n)

Below are diagrams to help explain the process.