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

Raycast Triangle


Raycasting against a triangle is a three step process:

  1. Create a plane from the three points of the triangle

  2. Raycast against that plane

  3. Check if the Raycast result is inside the triangle

We already have functions to implement this entire process. The FromTriangle function will create a plane from the triangle. We already have a Raycast function that casts a ray against a plane. We also have a PointInTriangle function.

We can improve the performance of the Raycast by using barycentric coordinates instead of the existing PointInTriangle test. Barycentric coordinates are a way to represent the position of a point relative to a triangle.

Getting ready

We are going to implement a new function, Barycentric. This new function will return the barycentric coordinates of a point with respect to a triangle. We will use this new function, along with the existing FromTriangle and Raycast functions created in Chapter 10, 3D Line Intersections to make a new Raycast against triangle function.

How...

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