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 Sphere


Given a ray with origin o, indirection d and a sphere with origin c and radius r; we want to check if the ray ever intersects the sphere:

If the ray intersects the sphere, this intersection will happen at some distance along the ray. Within the context of ray casting, we often assume it takes one second to travel one unit along the ray. Because of this, distance and time are often used interchangeably.

Because of this ambiguity with the vocabulary, many resources might say that the ray intersects the sphere at some time, t. If the ray does not intersect the sphere, t is undefined.

Getting ready

We are going to implement a function to check if a ray and a sphere intersect. This function will return t, the time along the ray at which the intersection takes place. If there is no intersection, we will set t to be a negative number.

How to do it…

Follow these steps to implement raycasting against a sphere:

  1. Declare the Raycast function in Geometry3D.h:

    float Raycast(const Sphere& sphere...
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