Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Removed some unnecessary code and files. Removed copyright notices.
Unified formatting and line endings. Resolves #3.
  • Loading branch information
fonsp committed Oct 8, 2014
1 parent 14f6684 commit 327d4a4
Show file tree
Hide file tree
Showing 43 changed files with 427 additions and 63,018 deletions.
81 changes: 38 additions & 43 deletions GraphicsEngine/Collision/CollisionBox.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
// CollisionBox.cs
//
// Copyright 2013 Fons van der Plas
// Fons van der Plas, fonsvdplas@gmail.com

using System;
using System;
using OpenTK;

namespace GraphicsLibrary.Collision
{
/* De collisionAABB class is een doos, gedefinieerd met twee hoekpunten lb en rt
* AABB: Axis Aligned Bounding Box
*/
public class CollisionAABB
{
public Vector3 lb, rt;

public CollisionAABB(Vector3 lb, Vector3 rt)
{
this.lb = lb;//min
this.rt = rt;//max
this.lb = lb; //min
this.rt = rt; //max
}

/* Deze functie berekent de afstand van een lijn die de doos snijdt
*/
public float Intersect(CollisionRay ray)
{
// Algorithm: http://i.stack.imgur.com/tfvSJ.png
Vector3 t = new Vector3();

if (ray.dir == Vector3.Zero)
if(ray.dir == Vector3.Zero)
{
return -1;
}
Expand All @@ -37,9 +27,9 @@ public float Intersect(CollisionRay ray)
Vector3 _lb = ray.eye - lb;
Vector3 _rt = ray.eye - rt;


//X
if (r.X > 0) // Determine candidate plane
if(r.X > 0) // Determine candidate plane
{
t.X = Max(-1, _lb.X / -r.X); // Save distance to hit point
}
Expand All @@ -48,7 +38,7 @@ public float Intersect(CollisionRay ray)
t.X = Max(-1, _rt.X / -r.X);
}
//Y
if (r.Y > 0)
if(r.Y > 0)
{
t.Y = Max(-1, _lb.Y / -r.Y);
}
Expand All @@ -57,74 +47,79 @@ public float Intersect(CollisionRay ray)
t.Y = Max(-1, _rt.Y / -r.Y);
}
//Z
if (r.Z < 0)
if(r.Z < 0)
{
t.Z = Max(-1, _lb.Z / r.Z);
}
else
{
t.Z = Max(-1, _rt.Z / r.Z);
}
r.Z *= -1; //....
r.Z *= -1; //why?

// The largest hit distance must be that of the hit plane
if (t.X >= t.Y && t.X >= t.Z)
if(t.X >= t.Y && t.X >= t.Z)
{
// test X
// Check if hit point is inside the box using the other 2 dimensions
Vector3 hit = ray.eye + Vector3.Multiply(r, t.X);
if (hit.Y >= lb.Y && hit.Y <= rt.Y && hit.Z >= lb.Z && hit.Z <= rt.Z)
if(hit.Y >= lb.Y && hit.Y <= rt.Y && hit.Z >= lb.Z && hit.Z <= rt.Z)
{
return t.X;
}
}
else if (t.Y >= t.X && t.Y >= t.Z)
else if(t.Y >= t.X && t.Y >= t.Z)
{
// test Y
Vector3 hit = ray.eye + Vector3.Multiply(r, t.Y);
if (hit.X >= lb.X && hit.X <= rt.X && hit.Z >= lb.Z && hit.Z <= rt.Z)
if(hit.X >= lb.X && hit.X <= rt.X && hit.Z >= lb.Z && hit.Z <= rt.Z)
{
return t.Y;
}
}
else if (t.Z >= t.Y && t.Z >= t.X)
else if(t.Z >= t.Y && t.Z >= t.X)
{
// test Z
Vector3 hit = ray.eye + Vector3.Multiply(r, t.Z);
if (hit.Y >= lb.Y && hit.Y <= rt.Y && hit.X >= lb.X && hit.X <= rt.X)
if(hit.Y >= lb.Y && hit.Y <= rt.Y && hit.X >= lb.X && hit.X <= rt.X)
{
return t.Z;
}
}
return -1;
}

/* Een simpelere functie die alleen kijkt of een bepaald punt in de AABB zit
*/
public bool isInside(Vector3 position)
{
return position.X >= lb.X && position.X <= rt.X && position.Y >= lb.Y && position.Y <= rt.Y && position.Z >= lb.Z && position.Z <= rt.Z;
}

public void Translate(Vector3 delta)
{
lb += delta;
rt += delta;
}

// Float math
private float Max(float a, float b)
{
return Math.Max(a, b);
}

// Voorbeeld:

//CollisionBox collisionBox = new CollisionBox(new Vector3(0, 0, 0), new Vector3(1000, 100, 1000));
//Vector3 dir = Vector3.Zero;
//
//fpsCam.Y = Math.Min(fpsCam.Y, 1.57f);
//fpsCam.Y = Math.Max(fpsCam.Y, -1.57f);
//
//dir.X = (float) Math.Sin(fpsCam.X);
//dir.Z = (float) Math.Cos(fpsCam.X);
//dir.Y = (float) Math.Tan(fpsCam.Y);
//
//CollisionRay collisionRay = new CollisionRay(Camera.Instance.position, dir);
//Console.WriteLine(collisionBox.Intersect(collisionRay));
// Example:
/*
CollisionBox collisionBox = new CollisionBox(new Vector3(0, 0, 0), new Vector3(1000, 100, 1000));
Vector3 dir = Vector3.Zero;
fpsCam.Y = Math.Min(fpsCam.Y, 1.57f);
fpsCam.Y = Math.Max(fpsCam.Y, -1.57f);
dir.X = (float) Math.Sin(fpsCam.X);
dir.Z = (float) Math.Cos(fpsCam.X);
dir.Y = (float) Math.Tan(fpsCam.Y);
CollisionRay collisionRay = new CollisionRay(Camera.Instance.position, dir);
Console.WriteLine(collisionBox.Intersect(collisionRay));
*/
}
}
}
10 changes: 1 addition & 9 deletions GraphicsEngine/Collision/CollisionRay.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// CollisionRay.cs
//
// Copyright 2013 Fons van der Plas
// Fons van der Plas, fonsvdplas@gmail.com

using OpenTK;
using OpenTK;

namespace GraphicsLibrary.Collision
{
/* Deze class slaat de richting en positie van een lijn op
* Wordt gebruikt in CollisionBox.cs
*/
public class CollisionRay
{
public Vector3 eye, dir;
Expand Down
Loading

0 comments on commit 327d4a4

Please sign in to comment.