-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sphere intersection code. Looks like an ellipse for some reason…
…, need some fix
- Loading branch information
1 parent
c1dd031
commit 3dcaf81
Showing
5 changed files
with
49 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,29 @@ | ||
function Sphere(x, y, z, r) { | ||
var Vector = require("../common/Vector"); | ||
|
||
function Sphere(x, y, z, r) { | ||
this.x = x; | ||
this.y = y; | ||
this.z = z; | ||
this.r = r; | ||
} | ||
|
||
Sphere.prototype.getIntersection = function(ray) { | ||
|
||
} | ||
var a = ray.direction.dot(ray.direction); | ||
var oc = new Vector(ray.origin.x - this.x, ray.origin.y - this.y, ray.origin.z - this.z); | ||
var b = 2 * (oc.dot(ray.direction)); | ||
var c = oc.dot(oc) - this.r * this.r; | ||
var detSq = b * b - 4 * a * c; | ||
if (detSq < 0) { | ||
return null; | ||
} | ||
var det = Math.sqrt(detSq); | ||
var t0 = (-b - det) / (2 * a); | ||
var t1 = (-b + det) / (2 * a); | ||
return ray.pointAtDistance(t0); //this is the closer of the two intersection points. the other will be useful for refraction maybe? | ||
} | ||
|
||
Sphere.prototype.getNormal = function(point) { | ||
return new Vector(point.x - this.x, point.y - this.y, point.z - this.z); | ||
} | ||
|
||
module.exports = Sphere; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters