Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.

Similar presentations


Presentation on theme: "CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby."— Presentation transcript:

1 CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby Lecture 5 Using Vectors for Collision Detection

2 Reading about Vectors  Rabin, Introduction to Game Development:  4.2: "Collision Detection and Resolution"  Van Verthe, Essential Mathematics for Games:  14.6: "A Simple Collision System"

3 Lecture Outline Vector practice (Revision) Collision detection and resolution

4 Topic 1:A reminder about Vectors

5 Right-angled triangles  Remember that with a right-angled triangle the length of the longest side (called the hypotenuse) is equal to the square root of the sum of the squares of two shorter sides. (Pythagoras’ Theorem)

6 Vector between two points  The vector V going from point P1 to point P2 is P1 subtracted from P2 (i.e. P2 – P1)  Remember that P1, P2 and V are all expressed as Cartesian co-ordinates.  The vector V from P1 to P2 is: = V( X 2 - X 1, Y 2 - Y 1, Z 2 - Z 1 )

7 Length of a vector  A vector makes a right-angled triangle in 3 dimensions... ...so use Pythagoras’ Theorem about right-angle triangles to calculate the length of the vector.  If we have a vector V(X, Y, Z) then:

8 Normalised vector  A normal is any vector whose length is 1.  It is also caled the direction vector.  A vector is converted to a normal (i.e. normalised) by dividing the components by the length:

9 Topic 2:Collision Detection

10 Collision Detection  Collision detection is a common operation.  You often need to able to determine whether two objects in a game have come into contact:  A bullet hitting a target  A racing car reaching the winning point  The player-character trying to move through a wall  Checking whether an event has triggered, e.g. a triggered piece of dialogue or a monster jumping out.  Distance calculations also come under this topic.

11 Point to Point Collsions  The objects move frame by frame. Calculate the distance between the two points each frame.

12 Point to Point  Point to point is the simplest case.  Example: a bullet striking a bull's eye.  Point to point is simply a matter of working out the distance between two points. This is the same calculation as the length of a vector.

13 Bounding volumes  Most objects in a game are not points so you cannot use point to point calculations.  It is time consuming and mathematically complex to test whether two complex objects intersect.  Instead use a simplified shape that fits around the object – a bounding volume.

14 Bounding volumes  Complex objects:  Bounding spheres:

15 Bounding volumes  Make the bounding volume as close a fit as possible.  Spheres and boxes are two commonly used shapes.  The technique sacrifices precision for efficiency (speed) – an issue which crops up frequently in games programming...  Bounding volumes are also used for a initial coarse check before engaging in more fine detailed analysis,  e.g. a bounding sphere can be employed to see whether there is any chance of collision.

16 Point to Sphere collisions  Check to see whether a point has collided with a sphere.  The radius of a sphere is the distance from the centre of the sphere to the edge of the sphere.  Calculate the distance from the point to the centre of the sphere.  If the distance is smaller than the radius of the sphere then a collision has occurred.

17 Point to Sphere

18 Sphere to Sphere Collisions  Surround both object with close fitting spheres.  The radius of a sphere is the distance from the centre of the sphere to its edge.  Calculate the distance from the centre of the first sphere to the centre of the second sphere.  If the distance is smaller than the sum of the sphere radii then a collision has occurred.

19 Sphere to Sphere

20 More bounding volumes  Spheres are useful because the maths is comparatively simple - it's just an extension of vector calculations.  Spheres also suit some objects, e.g. people (you would probably use a cylinder in practice... ...but 2 dimensionally, a sphere (i.e. a circle) is equivalent to a cylinder (with zero height).  But spheres do not suit all objects, e.g. a crate or a house.  In these cases a box (cuboid) would be a more appropriate approximation.

21 Bounding boxes  The simplest box is an "axis-aligned bounding box”.  The box is exactly aligned with the axes, e.g. horizontally and vertically.

22 Bounding boxes  A box which is not axis-aligned will be more difficult to use.  However, some objects fit better inside a box than a sphere.  A problem arises if the object can rotate. Boxes better suit objects that cannot rotate.  Of course, it is possible to do the calculations for a non-axis aligned bounding box. However, I am going to leave this until we move on to planes and point to plane collisions...

23 Point to Axis-Aligned Box  Check the position of the point against the minimum and maximum x, y and z values of the box.

24 Sphere to Box Collision  The easiest way to implement this is to do a bit of sideways thinking...  The sphere collides with the box when its edge crosses over the edge of the box.  This can only happen if the distance from the centre of the sphere to the edge of the box is smaller than the radius of the sphere.  So lets expand the size of the bounding box by the radius of the sphere.  Now do a simple point to box calculation instead.

25 Sphere to Box  Same method as point to box, but add the radius to the maximum x, y and z values of the box, and subtract it from the minimum x, y and z values. Note that this method is inaccurate at the corners...

26 Collision Resolution  The order of operation is always:  Collision Detection  Collision Resolution  Need to have a resolution because:  An object moves over the course of one frame to the next  If an object collides with an obstacle and we do not attempt to resolve the collision immediately...  The object could become stuck because it may well be inside the obstacle.

27 Collision Resolution  Moving frame- by-frame...  Therefore the object is inside the obstacle when the collision is detected...  Hence it will become stuck inside the object.

28 Simple Collision Resolution Algorithm  Move the model.  Record the movement vector you used to move the model (x, y, z).  Check if a collision has occurred.  If a collision has occurred:  Move the model back to where it was at the beginning of the frame (-x, -y, -z)  Note that this is simplistic.  You should check to see where along the path of movement the collision occurred and set the position to this location  You will look at the maths to do it at a later time.


Download ppt "CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby."

Similar presentations


Ads by Google