Skip to content

Commit

Permalink
Geometry: Fix frustum/sphere intersection checks
Browse files Browse the repository at this point in the history
Fixes #715
  • Loading branch information
magcius committed Oct 29, 2024
1 parent dcb3ea2 commit db143d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 9 additions & 6 deletions rust/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ pub struct Plane {
}

impl Plane {
pub fn new(normal: Vec3, d: f32) -> Plane {
Plane { normal, d }
}

pub fn normalized(&self) -> Plane {
let mag = self.normal.magnitude();
Plane { normal: self.normal / mag, d: self.d / mag }
Plane::new(self.normal / mag, self.d / mag)
}

pub fn negate(&mut self) {
Expand Down Expand Up @@ -199,9 +203,9 @@ impl ConvexHull {
let mut result = IntersectionState::Inside;
for plane in &self.planes {
let dist = plane.distance(center);
if dist < radius {
if dist < -radius {
return IntersectionState::Outside;
} else if dist < -radius {
} else if dist < radius {
result = IntersectionState::Intersection;
}
}
Expand Down Expand Up @@ -248,9 +252,8 @@ impl ConvexHull {

pub fn push_plane(&mut self, x: f32, y: f32, z: f32, d: f32) {
// Normalize the plane equation so that sphere tests work.
let mut normal = Vec3::new(x, y, z);
let mag = normal.normalize_mut();
self.planes.push(Plane { normal, d: d / mag });
let plane = Plane::new(Vec3::new(x, y, z), d);
self.planes.push(plane.normalized());
}

pub fn js_intersect_aabb(&mut self, min_x: f32, min_y: f32, min_z: f32, max_x: f32, max_y: f32, max_z: f32) -> IntersectionState {
Expand Down
4 changes: 0 additions & 4 deletions src/StarFoxAdventures/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import { SFAAnimationController } from './animation.js';
import { SFATextureFetcher } from './textures.js';
import { ModelRenderContext, ModelInstance } from './models.js';
import { World } from './world.js';
import { AABB } from '../Geometry.js';
import { LightType } from './WorldLights.js';
import { computeViewMatrix } from '../Camera.js';
import { drawWorldSpacePoint, getDebugOverlayCanvas2D } from '../DebugJunk.js';

export interface BlockInfo {
mod: number;
Expand Down

0 comments on commit db143d2

Please sign in to comment.