Skip to main content
gamedev.net
🔒 Locked

Collision Detection

Started by mc30900 Oct 13, 2005 at 10:30 AM 0 replies 750+ views
Original Post
mc30900
mc30900
hello i need collision detection for my game. i looked in the tutorial of nehe and didnt understand much. can anyone give me an explanation about a simple 3d collision detection plz? (i need it so no one can get out of the walls) thanks in advance
dawidjoubert
dawidjoubert
im going 2 explain a simple Bounding sphere collision detection.

First for this tutorial we assume objects dont move really fast (ie 1/10 of there size per frame) and secondly that there are only spheres.

Now a sphere only needs 2 Vars
1 --> A Position --> CVector3 Position;
2 --> A Radius --> float Radius;

class Sphere
{
CVector3 Pos;
float Radius;
};


void CheckSpearAgainstSkyBox(Sphere *p)
{
if (p.x > Max.x - p.Raduis) p.x = max.x-p.Raduis;
if (p.x < Min.x + p.Raduis) p.x = Min.x+p.Raduis;
.. And continue this with all the y and z axis
};

void CheckSphereAgainstSphere(Sphere s1,s2)
{
if (Distance(s1.Pos,s2.Pos) < s1.Radius + s2.Radius)
{ // Move both spheres back
s1.Pos = s1.Pos + (s1.Pos-s2.Pos) / 2;
s1.Pos = s1.Pos - (s2.Pos-s1.Pos) / 2;
}
}
----------------------------http://djoubert.co.uk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.