Where I could find collison detection for masm opengl example for camera (sphere collision) ?
Posted on 2003-06-18 09:38:38 by zabnik
I've never touched ogl (eww)
Sphere to Point collision is a matter of checking whether the distance from the point to the sphere's origin is less than the radius or not.
Sphere to Sphere is a little more complex - we need to first determine the angle between the two sphere origins, and then use it to calculate tangential points of intersection between the radii - sounds hard, well to think about it makes ur brain hurt unless like me u have a bg in engineering - but mathematically speaking, well, lets say I did it aged 13 on 8 bit machines and leave it shall we? - Mathematically it can be done using trigonometry. Furthermore, if your trig math is cheap, it's cheap too.
Posted on 2003-06-24 02:37:01 by Homer
You could try this algorithm, could it work for you?
Calculate collision between two spheres, if passing coordinates for middle of each bounding sphere and their radius:
(c++ function, took it from a book)



bool CheckSphereCollision ( float xCenter1, float yCenter1, float zCenter1, float radius1,
float xCenter2, float yCenter2, float zCenter2, float radius2 )
{

float xDiff = (float) fabs ( xCenter2 - xCenter1 );
float yDiff = (float) fabs ( yCenter2 - yCenter1 );
float zDiff = (float) fabs ( zCenter2 - zCenter1 );

float distance = (float) sqrt ( xDiff*xDiff + yDiff*yDiff + zDiff*zDiff );

if ( distance <= ( radius1 + radius2 ) )
return true;

return false;

}


My masm translation of above code, but wants address to vector structs ( x,y,z ) instead of passing coordinates directly:



CheckSpheres PROC USES ECX EDX v1:dword, v2:dword, radi1:dword, radi2:dword

mov ecx, v1
mov edx, v2
mov eax, 8

fld radi1
fadd radi2

__lo:
fld dword ptr [ecx + eax]
fsub dword ptr [edx + eax]
fabs
fmul st, st
sub eax, 4
jns SHORT __lo

fadd
fadd
fsqrt

fcompp
fstsw ax
sahf
jbe __collision

xor eax, eax
ret
__collision:
mov eax, 1
ret

CheckSpheres ENDP


Note : I didn't test these in a game or so, only that both c++ and masm return same values ( only tested a little, seems ok ),
so I'm curious if it works ok. I did try a 2d version of this in game though, worked good.
( Also I'm no master of fpu, anybody make it better pls or spot mistakes :grin: )
Posted on 2003-06-24 14:37:23 by david
You could also remove the squareroot from algorithm, like so:

float distance = xDiff*xDiff + yDiff*yDiff + zDiff*zDiff;
float radiusDistance = (radius1+radius2)*(radius1+radius2)*3.f;
if(distance <= radiusDistance)
return true
etc
Posted on 2003-06-25 06:06:57 by david