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

Linetest Triangle


Much like testing a line and an axis aligned or OBB intersection, testing a line and triangle intersection utilizes the existing Raycast function. We are going to cast a ray against the triangle being tested. If the Raycast succeeds, we need to make sure that the t value is along the line segment being tested.

Getting ready

We are about to implement a new Linetest function, which will test if a line and a triangle intersect. This function returns a Boolean result.

How to do it…

Follow these steps to check if a line intersects a triangle:

  1. Declare the new Linetest function in Geometry3D.h:

    bool Linetest(const Triangle& triangle, const Line& line);
  2. Implement the Linetest function in Geometry3D.cpp:

    bool Linetest(const Triangle& triangle, const Line& line) {
  3. Construct a ray out of the line being tested:

        Ray ray;
        ray.origin = line.start;
        ray.direction = Normalized(line.end - line.start);
  4. Perform a Raycast:

        float t = Raycast(triangle, ray);
  5. Check that the result...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Game Physics Cookbook
Published in: Mar 2017Publisher: PacktISBN-13: 9781787123663

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