Reader small image

You're reading from  Game Physics Cookbook

Product typeBook
Published inMar 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787123663
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Gabor Szauer
Gabor Szauer
author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer

Right arrow

Chapter 8. 3D Point Tests

Now that we have some 3D primitives defined, it's time to implement some simple point tests for them. In this chapter, we are going to implement the following point-related test functions:

  • Point contained in sphere

  • Closest point on sphere

  • Point contained in AABB

  • Closest point on AABB

  • Point contained in OBB

  • Closest point on OBB

  • Point on surface of plane

  • Closest point on plane

  • Point on line segment

  • Closest point along line

  • Point on ray

  • Closest point along ray

Introduction


For each primitive (other than the point) we will implement two test functions. The first function will tell us if a point is located inside or on the surface of a primitive. The second test will tell us what the closest point on the primitive is to a test point.

Point and sphere


Given a point and a sphere there are two operations we want to perform. First, we want to check whether a test point is inside the sphere or not. Alternately, we may want to get the closest point to the test point along the surface of the sphere:

Getting ready

To test whether a point is within a sphere we have to compare the distance from the center of the sphere and the test point to the radius of the sphere. If the distance is less than the radius, the sphere contains the point. We can get the point on the surface of the sphere closest to a test point by obtaining a vector that points from the center of the sphere to the test point. This vector should have the same magnitude as the radius of the sphere.

How to do it…

Perform the following steps to implement point tests for a sphere:

  1. Declare PointInSphere and ClosestPoint in Geometry3D.h:

    bool PointInSphere(const Point& point, 
        const Sphere& sphere);
    Point ClosestPoint(const Sphere& sphere, 
        const Point&...

Point and AABB


If we think of an Axis Aligned Bounding Box (AABB) as a Min and Max point, a test point is only inside the AABB if it is greater than Min and less than Max. Similarly, to get the closest point to a test point on the surface of the AABB, we just have to clamp the test point to the min and max points of the AABB:

Getting ready

We are going to implement a function to test if a point is contained within an Axis Aligned Bounding Box. This test will compare the point component-wise to the min and max points of the AABB. We are also going to implement a function to find the point on the Axis Aligned Bounding Box closest to a given test point. To find the closest point, we will clamp the test point to the min and max points of the AABB, component-wise.

How to do it…

Perform the following steps to implement point tests for an AABB:

  1. Declare PointInAABB and ClosestPoint in Geometry3D.h:

    bool PointInAABB(const Point& point, const AABB& aabb);
    Point ClosestPoint(const AABB&aabb,...

Point and Oriented Bounding Box


To test if a point is inside an Oriented Bounding Box (OBB), we could transform the point into the local space of the OBB, and then perform an AABB containment test. However, transforming the point into the local space of the OBB is needlessly expensive.

A more efficient solution is to project the point onto each axis of the OBB, then compare the projected point to the length of the OBB on each axis. To get the closest point to a test point on the surface of the OBB, we perform the same projection. Once the point is projected, we clamp it to the length of the OBB on each axis:

This diagram demonstrates a test point being projected and clamped to the Y axis of the OBB.

The test point must also be projected and clamped to the X and Z axes as well.

Getting ready

We are going to implement two functions. The first function will test if a point is contained within an Oriented Bounding Box. The second function will find the closest point to a given test point on the surface...

Point and plane


We have seen the plane equation before; a point is on a plane if the result of the plane equation is 0. To find the point on the plane closest to a test point, we must project the test point onto the normal of the plane. We then subtract this new vector from the test point to get the closest point:

Getting ready

We are going to implement two functions. The first function will test whether a point is on the surface of a plane using the plane equation. The second function will find the point on a plane closest to a given test point.

How to do it…

Perform the following steps to implement point tests for a plane:

  1. Declare PointOnPlane and ClosestPoint in Geometry3D.h:

    bool PointOnPlane(const Point& point, const Plane& plane);
    Point ClosestPoint(const Plane& plane, const Point& point);
  2. Implement PointOnPlane in Geometry3D.cpp:

    bool PointOnPlane(const Point& point, const Plane& plane) {
        float dot = Dot(point, plane.normal);
        // To make this more robust, use...

Point and line


To test if a point is on a line, or to get the point on a line closest to a test point, we first have to project the point onto the line. This projection will result in a floating point value, t. We use this new t value to find the distance of the point along the line segment using the distance(t) = start + t * (end - start)function. The start point of the line is at t = 0, the end point is at t = 1. We have to take two edge cases into account, when t is less than 0 or greater than 1:

Getting ready

We are going to implement two functions, one to get the point on a line closest to a test point and one to determine if a test point is on a line. The ClosestPoint function is going to project the test point onto the line and evaluate the parametric function, distance(t) = start + t * (end - start).

To determine if a test point is on a line segment, we still need the point on the segment closest to the test point. We are then able to measure the distance between the test point and...

Point and ray


A ray is the same as a directed line. Unlike a line segment, which has a start and an end point, a ray has only a start point and a direction. The ray extends infinitely in this one direction. Because of the ray's similarity to a line, operations on a ray are similar to those on a line.

Because a ray's direction is a normal vector, we can use the dot product to check its direction against other known vectors. For example, to test whether a point is on a ray, we need to get a normalized vector from the origin of the ray to the test point. We can then use the dot product to see if this new normal vector is the same as the normal of the ray. If two vectors point in the same direction, the result of the dot product will be 1:

Getting ready

We are going to implement two functions: one to check if a test point is on a ray and one to get the closest point on a ray to a test point. Both of these functions are going to rely heavily on the dot product.

How to do it…

Perform the following...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Physics Cookbook
Published in: Mar 2017Publisher: PacktISBN-13: 9781787123663
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer