Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.

Similar presentations


Presentation on theme: "OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation."— Presentation transcript:

1

2 OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation (with the use of public and private) The other three are what we’re going to look at in this lesson

3 We’re going to work with vectors in this lesson
Not the list type vector But the maths type vector That’s because Unreal uses vector mathematics for a lot of things So it’ll be worth looking at them now

4 Vectors But first, some notation that we’re going to have to learn
Vectors are one-dimensional data structures that contain any number of values Usually relating to the space we’re working with 2 values: 2D space (x, y) 3 values: 3D space (x, y, z)

5 Vectors 𝑥 𝑦 or 𝑥 𝑦 Vectors are often shown in brackets, like so
Either direction is fine – they both mean the same 𝑥 𝑦 or 𝑥 𝑦

6 Vectors themselves only contain data
Like any other data structure However, they can be used in different ways Here are a few examples

7 Vectors (Position) We can use a vector to represent a position of something in space For example, this point here We can create a vector to represent its position 5 11

8 Vectors (Direction) We can also use a vector to represent a direction
These always have a start and end The start here is the origin 0 0 The end is where the origin plus the direction – the result here is roughly 7 7 Note the 𝑥 and 𝑦 parts form sides of a right-angled triangle? This will be useful knowledge later

9 Vectors (Addition) We can add vectors together, and we’ll usually add them in these two combinations A direction to a position This moves the element in that direction A direction to a direction When we add a ‘force’ to an element

10 Vectors (Addition) Adding vectors works like adding any two numbers
We just do it for every component of the vector Take this example A circle is at position and is moving in direction We can see the circle’s new position by adding them together. New Position= = =

11 Vectors (Magnitude) All vectors have a magnitude
Easier to visualise with direction vectors Think of the magnitude as the overall strength of the vector The larger the magnitude, the more affect it will have on other vectors For example, adding a stronger direction to a position makes the element move further away

12 Vectors (Magnitude) When thinking of vectors as right-angled triangles, the magnitude is the hypotenuse Since we know the adjacent (the 𝑥 component) And we know the opposite (the 𝑦 component) We can use Pythagoras’ Theorem to find the hypotenuse (the magnitude)

13 We often note a vector in the following format
Vectors (Magnitude) We often note a vector in the following format And the magnitude as follows 𝐴 = 3 4 𝐴 = 𝑥 2 + 𝑦 2 = = 25 =5

14 Vectors (Unit Vector) Finally, we can think of direction vectors as a combination of Their general direction And their magnitude The magnitude affects how far we move in that general direction But how do we get the general direction?

15 Vectors (Unit Vector) That’s where unit vectors come in
These are special vectors that have a magnitude of 1 Essentially removing any strength from a vector Leaving only their general direction Unit vectors as essential in any mildly-complex 2D/3D game As they’re used to store the direction of entity movement

16 Vectors (Unit Vector) We can calculate a unit vector by dividing a vector by its magnitude Here’s an example taking the vector from earlier 𝑈 = 𝐴 𝐴 𝑈 = = =

17 Vectors (Unit Vector) This tells us, for every one magnitude, we move 0.6 along the 𝑥 axis, and 0.8 along the 𝑦 axis If this was the direction of a spaceship, we could multiply this by its speed To calculate the change in position

18 Vectors Ex A spaceship is at 20 x, 40 y in space, and is moving in x and 0.51 y at 100 pixels/s Calculate its new position after 2 seconds Calculate the unit vector for the vector 14 −3 (to two-decimal places, e.g. 0.34)

19 Creating a Vector Class (1)
Let’s create a struct to represent a 2D vector We’ll give it double variables for its 𝑥 and 𝑦 components As well as functions for Constructors Adding a vector Scaling a vector Calculating the magnitude Turning it into a unit vector Called normalising

20 Creating a Vector Class (1)
Note that we have two constructors here? One giving 𝑥 and 𝑦 values One giving a reference to a vector The second has been made in case we want to copy a vector later on Two functions with the same name like this? Known as overloading

21 Creating a Vector Class (1)
We’ll then implement these functions in a source file Including math.h for pow and sqrt functions

22 Creating a Vector Class (1) Ex
Create a new Empty C++ Project Create a header file for your Vector2D struct Give it instance variables for its 𝑥 and 𝑦 components Give it functions for Constructing (either with two doubles, or one vector) Adding (another vector to it) Scaling (multiplying itself by a double) Getting its magnitude Normalising itself (turning itself into a unit vector) Implement each of these functions in a source file

23 Creating a Vector Class (2) Ex
Add another function to the struct called GetDistanceTo Accepts a vector as a parameter This returns the distance form the current vector to the parameter Can calculate distance between vectors by Subtracting their 𝑥 and 𝑦 components from each other Making a vector out of the results, and calculating its magnitude

24 We can use this vector class in a video game environment
Game Entities (1) We can use this vector class in a video game environment Vectors for the position of entities Vectors for the directions of entities We need to start by creating a class for an Entity

25 Game Entities (1) We’ll do this in a separate header file
We’ll give this class A constructor and destructor A function for moving Functions for getting the position, and setting the direction Private variables for the position, direction, and speed

26 Game Entities (1) We can then implement these functions in a separate source file Be careful with changing the direction We’re replacing the old vector pointer Which means we need to delete it We also normalise it after setting it (to remove any magnitude from it)

27 Game Entities (1) Then we can test this out by creating an Entity
Outputting its current position Moving it a bit Then outputting its position again

28 Game Entities (1) When we run this, we can see the car move
In the direction we’ve set

29 Game Entities (1) This movement based off of
A current position A direction A speed A timeframe to move in Is one that is commonly used in 2D/3D video games to create ‘accurate’ physical movements Can tweak the Entity class to include acceleration (which can factor in forces like gravity)

30 Game Entities (1) Ex Create a header file for your entities
Create an Entity class in it that has A constructor and a destructor Functions for moving, getting its position, and setting its direction Private variables for its position and direction (pointers), and its speed Implement these functions in a separate source file Create a source file with the main() function in it Inside it, create an Entity object and set its direction Output its position before and after moving it for 5 seconds

31 For example, we could create a Spaceship
Game Entities (2) With the base class Entity set up, we can create some specific entities for a game For example, we could create a Spaceship That is an Entity as well But also has a string for the spaceship’s name

32 Game Entities (2) Making one class use another class like this is known as inheritance We can create this relationship at any point by adding a colon after the name of the class Where we put the name of the parent class

33 Game Entities (2) This means that the Spaceship is an Entity
It inherits all the functions and variables from the Entity class But, can’t use them unless they are public or protected If they are private, we can’t use them Also need to include public after the colon Otherwise we can’t use Entity functions (like Move())

34 Game Entities (2) If we try and implement the constructor for our Spaceship, we get a problem It complains that we do not have a default constructor for Entity

35 Game Entities (2) Because we’re technically creating an Entity as well, we need to run its constructor We can do that after the signature of the constructor Typically done on a new line We can’t have a space between the colon and the Entity name

36 We can then carry on with the constructor for the Spaceship
Game Entities (2) We can then carry on with the constructor for the Spaceship By storing its name

37 Let’s repeat the Entity movement from before
Game Entities (2) Let’s repeat the Entity movement from before This time for the Spaceship All using the same functions

38 Game Entities (2) Here’s what the output looks like
Since we’re running the same functions, we could store any Entity and Spaceship object in a single vector list

39 Game Entities (2) Ex Add a Spaceship class to the entities header file
Make it inherit from Entity Make sure the inheritance is public Give it a string variable for its name And a constructor which accepts a string parameter for its name In the entities source file, implement the constructor Include a colon, followed by the Entity constructor, in the Spaceship’s constructor Then store the name in the instance variable In the main() method, repeat the movement code This time for a Spaceship

40 We may want to change how a function works for its sub-classes
Overriding Functions We may want to change how a function works for its sub-classes Moving an entity may be different to moving a spaceship This is easily done, as C++ supports the idea of overriding functions

41 Overriding Functions To override a function, we first have to redeclare it in the sub-class We’re saying “I’m overriding Move()”

42 Overriding Functions We can then change how it works in its implementation If we still want to run the original as well as doing other things, we can call the Entity version as well At any point in the function

43 When we run this, we’ll see the added output when moving the Spaceship
Overriding Functions When we run this, we’ll see the added output when moving the Spaceship Even though we’re using the same function

44 Overriding Functions Ex
Add a declaration for the Move function in the Spaceship class Implement it in the entities source file Output a message to the console in the overridden function Then call the Entity version of this function Run your program and make sure this message is visible in the console

45 END OF UNIT!


Download ppt "OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation."

Similar presentations


Ads by Google