Page 1 of 1

3DCollision: Problem in my engine(memory&maths programmi

Posted: Mon Jul 26, 2004 9:21 am
by DarkDragon
Hello,

I wanted to include my collision(viewtopic.php?t=11857) into my engine. Now I have one problem: The check says always there is a collision, but I don't know why. The MeshData is saved in a Buffer:

NormalX.f
NormalY.f
NormalZ.f
X1.f
Y1.f
Z1.f
X2.f
Y2.f
Z2.f
X3.f
Y3.f
Z3.f

NormalX.f
NormalY.f
NormalZ.f
X1.f
Y1.f
Z1.f
X2.f
...


Then I'm checking the collision so:

Code: Select all

Procedure CheckCollision(*Buffer, Size, SizeX.f, SizeY.f, SizeZ.f, X.f, Y.f, Z.f)
  While k < Size ;Check the data of the 1st object
    CopyMemory(*Buffer+k, @Tri1_Normal.Point3D, 12)
    k + 12
    CopyMemory(*Buffer+k, @Tri1_1.Point3D, 12)
    k + 12
    CopyMemory(*Buffer+k, @Tri1_2.Point3D, 12)
    k + 12
    CopyMemory(*Buffer+k, @Tri1_3.Point3D, 12)
    k + 12
    
    Tri1_1\X = Tri1_1\X * SizeX + X
    Tri1_1\Y = Tri1_1\Y * SizeY + Y
    Tri1_1\Z = Tri1_1\Z * SizeZ + Z
    Tri1_2\X = Tri1_2\X * SizeX + X
    Tri1_2\Y = Tri1_2\Y * SizeY + Y
    Tri1_2\Z = Tri1_2\Z * SizeZ + Z
    Tri1_3\X = Tri1_3\X * SizeX + X
    Tri1_3\Y = Tri1_3\Y * SizeY + Y
    Tri1_3\Z = Tri1_3\Z * SizeZ + Z
    
    If Isnan(Tri1_1\X)
      Tri1_1\X = X
    EndIf
    If Isnan(Tri1_1\Y)
      Tri1_1\Y = Y
    EndIf
    If Isnan(Tri1_1\Z)
      Tri1_1\Z = Z
    EndIf
    If Isnan(Tri1_2\X)
      Tri1_2\X = X
    EndIf
    If Isnan(Tri1_2\Y)
      Tri1_2\Y = Y
    EndIf
    If Isnan(Tri1_2\Z)
      Tri1_2\Z = Z
    EndIf
    If Isnan(Tri1_3\X)
      Tri1_3\X = X
    EndIf
    If Isnan(Tri1_3\Y)
      Tri1_3\Y = Y
    EndIf
    If Isnan(Tri1_3\Z)
      Tri1_3\Z = Z
    EndIf
    
    j=0
    While j < OBJ()\VertBufferSize ;Check the data of the 2nd object
      CopyMemory(OBJ()\VertBuffer+j, @Tri2_Normal.Point3D, 12)
      j + 12
      CopyMemory(OBJ()\VertBuffer+j, @Tri2_1.Point3D, 12)
      j + 12
      CopyMemory(OBJ()\VertBuffer+j, @Tri2_2.Point3D, 12)
      j + 12
      CopyMemory(OBJ()\VertBuffer+j, @Tri2_3.Point3D, 12)
      j + 12
      
      Tri2_1\X = Tri2_1\X * OBJ()\SizeX + OBJ()\X
      Tri2_1\Y = Tri2_1\Y * OBJ()\SizeY + OBJ()\Y
      Tri2_1\Z = Tri2_1\Z * OBJ()\SizeZ + OBJ()\Z
      Tri2_2\X = Tri2_2\X * OBJ()\SizeX + OBJ()\X
      Tri2_2\Y = Tri2_2\Y * OBJ()\SizeY + OBJ()\Y
      Tri2_2\Z = Tri2_2\Z * OBJ()\SizeZ + OBJ()\Z
      Tri2_3\X = Tri2_3\X * OBJ()\SizeX + OBJ()\X
      Tri2_3\Y = Tri2_3\Y * OBJ()\SizeY + OBJ()\Y
      Tri2_3\Z = Tri2_3\Z * OBJ()\SizeZ + OBJ()\Z
      
      If Isnan(Tri2_Normal\X)
        Tri2_Normal\X = 1
      EndIf
      If Isnan(Tri2_Normal\Y)
        Tri2_Normal\Y = 1
      EndIf
      If Isnan(Tri2_Normal\Z)
        Tri2_Normal\Z = 1
      EndIf
      If Isnan(Tri2_1\X)
        Tri2_1\X = OBJ()\X
      EndIf
      If Isnan(Tri2_1\Y)
        Tri2_1\Y = OBJ()\Y
      EndIf
      If Isnan(Tri2_1\Z)
        Tri2_1\Z = OBJ()\Z
      EndIf
      If Isnan(Tri2_2\X)
        Tri2_2\X = OBJ()\X
      EndIf
      If Isnan(Tri2_2\Y)
        Tri2_2\Y = OBJ()\Y
      EndIf
      If Isnan(Tri2_2\Z)
        Tri2_2\Z = OBJ()\Z
      EndIf
      If Isnan(Tri2_3\X)
        Tri2_3\X = OBJ()\X
      EndIf
      If Isnan(Tri2_3\Y)
        Tri2_3\Y = OBJ()\Y
      EndIf
      If Isnan(Tri2_3\Z)
        Tri2_3\Z = OBJ()\Z
      EndIf
      
      If IntersectedPolygon(@Tri1_1.Point3D, @Tri1_2.Point3D, @Tri1_3.Point3D, @Tri2_1.Point3D, @Tri2_2.Point3D, @Tri1_Normal.Point3D) Or IntersectedPolygon(@Tri1_1.Point3D, @Tri1_2.Point3D, @Tri1_3.Point3D, @Tri2_2.Point3D, @Tri2_3.Point3D, @Tri1_Normal.Point3D)
        ProcedureReturn 1
      ElseIf IntersectedPolygon(@Tri2_1.Point3D, @Tri2_2.Point3D, @Tri2_3.Point3D, @Tri1_1.Point3D, @Tri1_2.Point3D, @Tri2_Normal.Point3D) Or IntersectedPolygon(@Tri2_1.Point3D, @Tri2_2.Point3D, @Tri2_3.Point3D, @Tri1_2.Point3D, @Tri1_3.Point3D, @Tri2_Normal.Point3D)
        ProcedureReturn 1
      EndIf
    Wend
  Wend
EndProcedure
OBJ() Is the next element and the normal parameters are the privious element data

Posted: Mon Jul 26, 2004 4:47 pm
by Codemonger
You should have a look at using newton physics engine as it includes collision detection as well as physics ... i believe it is a dll written in 'c' so it should work nicely.

http://www.physicsengine.com

Posted: Mon Jul 26, 2004 5:15 pm
by DarkDragon
Uhm, I know it, it is used by many of the 3DGameStudio A5/A6 users and it is great, but I found my fault: PI wasn't global :oops: (what a mistake :evil: )

Thanks for the reply, my real 3DCollision works now very, very, very well.

collition in 3d mode =) using prosedure... tutorial

Posted: Fri Mar 04, 2005 9:07 pm
by zefiro_flashparty
(note: mi english is bad :S:S:S:S) i speack spanish......
i use a structure , whit linked list to player and a procedure to detect collition in 3d mode ( realli 2d colition add the other Y vector to full 3d )

i use sphere colition... the player use a radio ( + ) and other player ( + ) other radio ,.... the player have a vector of direction ( + )--> when if collision the direction, back two speed back ,
<<------( + ( )---> + )
i calcule radian an trayectorie in a procedure ...

ok , i don publik all source of mi game , but only the nesesari...

firts declare procedures ( procedure of collision ) and procedure of vector direction whit radian calcle...

ah.... i use a procedure to search collision of any tipe of object collision
ej
car collision treee
car collision rock
car collision pipoll...

pipoll collision tree
pipol collision rock
etc...
ok go to source...
;declare procedures :D

Declare.f angrad(grado) ;radian calcle use to vector of direction calcle...
Declare.f colision(entidad) ;player collision other player <----only write ;theseprocedure change the other linked list to otrher object ...
Declare.f colision2(entidad);player collision jeep
Declare.f colision3(entidad);player collision heli
Declare.f colision4(entidad);jeep collision hely

;make player structure
Structure player
x.f ;xpos
y.f ;ypos
z.f ;zpos
radio.f ;radio of player ( + )
angulo.f ;rotation angle
rotacion.f ;rotation step
nangulo.f ; new angle , use rotate step to = to angle
corrige.f
radian.f
velocidad.f ;velocity of object
;oriented player vector
xl.f ;x look ( the player look the camera tho coordenates xyz)
yl.f ;ylook (wen the player rotate rotate the camera )
zl.f ;zlook (using the radian proyection of the xl yl zl)
;oriented camera vector
xlc.f ;xlookcamera
ylc.f ;y lookcamera
zlc.f ;zlookcamera
vista.f
nombre.s
mesh.f ;object mesh ... ( i create the radio , if the type of mesh...)

EndStructure

NewList artic.player(); player 1
NewList gsg.player(); player 2
NewList veicle.player(); veicles

;here load the mesh textures etc...

repeat
;
; here is the game...
; keys modifi the x z coord of player
; use the mouse to rotate player look and camera..
; note! ( the camera rotate more fast to player.... :S ) is a problem..
; i use only the vector to rotate camera and mesh....

; now use the procedure to calcule collitions

ForEach gsg()
; see all player1 linked list , and detect whit the procedure
;other proximiti player2
colision(gsg()) ;<---- here is the procedure
Next
ForEach veicle();colicion uno con veiculos
; see all veicles linked list , and detect whit the procedure
;other proximiti veicle
colision2(veicle());<---- here is the procedure
Next
ForEach veicle() ;colicion viculo con otros player
; see all veicles linked list , and detect whit the procedure
;other proximiti player2
colision3(gsg());<---- here is the procedure
Next
ForEach veicle() ;colicion veiculos con veiculos
; see all veicles linked list , and detect whit the procedure
;other proximiti veicle2
colision3(veicle());<---- here is the procedure
Next

;display all mesh ...

;procedures.....
; the procedure of radian is colavoration of madmax... :D
;use it to calcule rotation of the vector direction
Procedure.f angrad(grado)
Protected pi.f
Protected ra.f
Protected rad.f
pi=3.14159265
ra=(2*pi)/360
grado=grado % 360
rad.f=grado*ra
ProcedureReturn rad.f
EndProcedure

;and collision procedures

Procedure.f colision(entidad)

Protected x1.f
Protected z1.f
Protected x2.f
Protected z2.f
x1=artic()\x
z1=artic()\z
x2=gsg()\x
z2=gsg()\z

rchoke.f
rchoke= artic()\radio + gsg()\radio

If x1>0 And x2>0 And x1 - x2 < rchoke And x1 > x2 And z1>0 And z2>0 And z1 - z2 < rchoke And z2 < z1
artic()\radian=angrad(artic()\angulo)
artic()\x=artic()\x - (Sin (artic()\radian) * artic()\velocidad)
artic()\z=artic()\z - (Cos (artic()\radian) * artic()\velocidad)
EndIf
If x1>0 And x2>0 And x2 - x1 < rchoke And x2 > x1 And z1>0 And z2>0 And z1 - z2 < rchoke And z1 > z2
artic()\radian=angrad(artic()\angulo)
artic()\x=artic()\x - (Sin (artic()\radian) * artic()\velocidad)
artic()\z=artic()\z - (Cos (artic()\radian) * artic()\velocidad)
EndIf
If x1>0 And x2>0 And x2 - x1 < rchoke And x1 < x2 And z1>0 And z2>0 And z2 - z1 < rchoke And z1 < z2
artic()\radian=angrad(artic()\angulo)
artic()\x=artic()\x - (Sin (artic()\radian) * artic()\velocidad)
artic()\z=artic()\z - (Cos (artic()\radian) * artic()\velocidad)
EndIf
If x1>0 And x2>0 And x1 - x2 < rchoke And x2 < x1 And z1>0 And z2>0 And z2 - z1 < rchoke And z2 > z1
artic()\radian=angrad(artic()\angulo)
artic()\x=artic()\x - (Sin (artic()\radian) * artic()\velocidad)
artic()\z=artic()\z - (Cos (artic()\radian) * artic()\velocidad)
EndIf

EndProcedure
; note ... these is only a collision from the foreach of player2 to player 1
; artic is a player 1
;other variable protected is the varables of for each of gsg(player2)
;
; for each player 2
; procedure collision player 1
;
; in procedure.. first copy player 2 values, to protected variables
;
;and compare values to foreach player1
;
;if collision detected invert the velociti and backward the player....
; to detect x y z collision use these comparacion x z and y z ....:S
;good luuk!!!

see the demo 3d :D of the collision

Posted: Fri Mar 04, 2005 9:14 pm
by zefiro_flashparty
http://graygriffen.net/~zefiro/demo3dzefiro.zip

is the demo , using the collision type

use wasd tho walk , mouse to rotate....
ry to up down the copter
tfgh to rotate and aceletate copter

nmjk to acelerate and rotate jeep

see the colition whit the other player
and the collition of the jeep whit the plater
and the elicopter... the copter collision whit the player but not see Y vector
y need add tho the procedure =P