News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Need example code for intersection

Started by Farabi, January 26, 2014, 03:45:27 PM

Previous topic - Next topic

Farabi

Anyone have example code for line-triangle intersection? I've tried lots of formula but since I had no background of math I cant tell wheter my code is correct or not. Here is one of the code


http://www.angelfire.com/fl/houseofbartlett/solutions/line2tri.html
fCheckSamClockDir proc uses esi edi lpTri:dword,norm:dword
LOCAL testi, testj, testk:real4
LOCAL dotprod:real4
LOCAL tmp,tmp2:real4

mov esi,lpTri
mov edi,norm

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fstp tmp

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fmul tmp
fstp tmp

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fstp tmp

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fmul tmp2
fstp tmp2

fld tmp
fsub tmp2
fstp testi
;--------------------------------------------------------------------------------

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fstp tmp

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fmul tmp
fstp tmp

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fstp tmp

fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fmul tmp2
fstp tmp2

fld tmp
fsub tmp2
fstp testj
;--------------------------------------------------------------------------------
fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fstp tmp

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fmul tmp
fstp tmp

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fstp tmp

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fmul tmp2
fstp tmp2

fld tmp
fsub tmp2
fstp testj
;--------------------------------------------------------------------------------

fld testi
fmul [edi].VERTEX.x
fstp tmp

fld testj
fmul [edi].VERTEX.y
fadd tmp
fstp tmp

fld testk
fmul [edi].VERTEX.z
fadd tmp
fstp dotprod

FCMP dotprod,FP4(0.)
.if SIGN?
xor eax,eax
.else
mov eax,1
.endif


ret
fCheckSamClockDir endp

fRayTriIntersect proc uses esi edi ebx lpRay:dword,lpTri:dword,lpIntersect:dword
LOCAL v1:VERTEX
LOCAL v2:VERTEX
LOCAL v3:VERTEX
LOCAL norm:VERTEX
LOCAL dotprod:real4
LOCAL t:real4
LOCAL tmp,tmp2:real4
LOCAL PCHECK:Tri
LOCAL q:qword
LOCAL buff[256]:dword

mov esi,lpTri
mov edi,lpRay
mov ebx,lpIntersect

invoke Vec_Sub,addr v1,addr [esi].Tri.pt2,addr [esi].Tri.pt1
invoke Vec_Sub,addr v2,addr [esi].Tri.pt3,addr [esi].Tri.pt2

invoke Vec_CrossProduct,addr norm,addr v1,addr v2
invoke Vec_DotProduct,addr norm,addr [edi].Ray.vector
fstp dotprod

FCMP dotprod,FP4(0.)
jl @f
xor eax,eax
ret
@@:

fld [edi].Ray.linept.x
fsub [esi].Tri.pt1.x
fmul norm.x
fstp tmp
fld [edi].Ray.linept.y
fsub [esi].Tri.pt1.y
fmul norm.y
fadd tmp
fstp tmp
fld [edi].Ray.linept.z
fsub [esi].Tri.pt1.z
fmul norm.z
fadd tmp
fchs
fstp tmp

fld norm.x
fmul [edi].Ray.vector.x
fstp tmp2
fld norm.y
fmul [edi].Ray.vector.y
fadd tmp2
fstp tmp2
fld norm.z
fmul [edi].Ray.vector.z
fadd tmp2
fstp tmp2

fld tmp
fdiv tmp2
fstp t

FCMP t,FP4(0.)
jnl @f
xor eax,eax
dec eax
ret
@@:

fld [edi].Ray.vector.x
fmul t
fadd [edi].Ray.linept.x
fstp [ebx].VERTEX.x

fld [edi].Ray.vector.y
fmul t
fadd [edi].Ray.linept.y
fstp [ebx].VERTEX.y

fld [edi].Ray.vector.z
fmul t
fadd [edi].Ray.linept.z
fstp [ebx].VERTEX.z

invoke MemCopy,lpTri,addr PCHECK,sizeof Tri
invoke MemCopy,ebx,addr PCHECK.pt3,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
invoke MemCopy,addr [esi].Tri.pt2,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt1,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
ret
.endif
.endif
.endif



xor eax,eax
ret
fRayTriIntersect endp


Maybe I just need a working code for this  :lol:
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

I made a progress


fRayTriIntersect proc uses esi edi ebx lpRay:dword,lpTri:dword,lpIntersect:dword
LOCAL v1:VERTEX
LOCAL v2:VERTEX
LOCAL v3:VERTEX
LOCAL norm:VERTEX
LOCAL dotprod:real4
LOCAL t:real4
LOCAL tmp,tmp2:real4
LOCAL PCHECK:Tri
LOCAL q:qword
LOCAL buff[256]:dword

mov esi,lpTri
mov edi,lpRay
mov ebx,lpIntersect

invoke Vec_Sub,addr v1,addr [esi].Tri.pt2,addr [esi].Tri.pt1
invoke Vec_Sub,addr v2,addr [esi].Tri.pt3,addr [esi].Tri.pt2

invoke Vec_CrossProduct,addr norm,addr v2,addr v1
invoke Vec_DotProduct,addr norm,addr [edi].Ray.vector
fstp dotprod

; fld dotprod
; fstp q
; invoke FloatToStr2,q,addr buff
; invoke MessageBox,0,addr buff,0,0

fldz
fld dotprod
fcomip st(0),st(1)
jb @f
xor eax,eax
dec eax
ret
@@:

fld [edi].Ray.linept.x
fsub [esi].Tri.pt1.x
fmul norm.x
fstp tmp
fld [edi].Ray.linept.y
fsub [esi].Tri.pt1.y
fmul norm.y
fadd tmp
fstp tmp
fld [edi].Ray.linept.z
fsub [esi].Tri.pt1.z
fmul norm.z
fadd tmp
fchs
fstp tmp

fld norm.x
fmul [edi].Ray.vector.x
fstp tmp2
fld norm.y
fmul [edi].Ray.vector.y
fadd tmp2
fstp tmp2
fld norm.z
fmul [edi].Ray.vector.z
fadd tmp2
fstp tmp2

fld tmp
fdiv tmp2
fstp t

; fld t
; fstp q
; invoke FloatToStr2,q,addr buff
; invoke MessageBox,0,addr buff,0,0


fldz
fld t
fcomip st(0),st(1)
ja @f
xor eax,eax
ret
@@:

;invoke MessageBox,0,CADD("T is above 0"),0,0

fld [edi].Ray.vector.x
fmul t
fadd [edi].Ray.linept.x
fstp [ebx].VERTEX.x

fld [edi].Ray.vector.y
fmul t
fadd [edi].Ray.linept.y
fstp [ebx].VERTEX.y

fld [edi].Ray.vector.z
fmul t
fadd [edi].Ray.linept.z
fstp [ebx].VERTEX.z

invoke MemCopy,lpTri,addr PCHECK,sizeof Tri
invoke MemCopy,ebx,addr PCHECK.pt3,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
invoke MemCopy,addr [esi].Tri.pt2,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt1,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSamClockDir,addr PCHECK,addr norm
.if eax==1
ret
.endif
.endif
.endif



xor eax,eax
ret
fRayTriIntersect endp


If I fill the value to this

fld FP4(0.)
fstp tr.pt1.x
fld FP4(0.)
fstp tr.pt1.y
fld FP4(0.)
fstp tr.pt1.z

fld FP4(0.)
fstp tr.pt2.x
fld FP4(3.)
fstp tr.pt2.y
fld FP4(3.)
fstp tr.pt2.z

fld FP4(2.)
fstp tr.pt3.x
fld FP4(0.)
fstp tr.pt3.y
fld FP4(0.)
fstp tr.pt3.z

fld FP4(0.)
fstp ra.linept.x
fld FP4(0.)
fstp ra.linept.y
fld FP4(6.)
fstp ra.linept.z

fld FP4(0.)
fstp ra.vector.x
fld FP4(2.)
fstp ra.vector.y
fld FP4(-4.)
fstp ra.vector.z


It yield intersection warning. And if I changed it to this

fld FP4(0.)
fstp tr.pt1.x
fld FP4(0.)
fstp tr.pt1.y
fld FP4(0.)
fstp tr.pt1.z

fld FP4(0.)
fstp tr.pt2.x
fld FP4(3.)
fstp tr.pt2.y
fld FP4(3.)
fstp tr.pt2.z

fld FP4(2.)
fstp tr.pt3.x
fld FP4(0.)
fstp tr.pt3.y
fld FP4(0.)
fstp tr.pt3.z

fld FP4(0.)
fstp ra.linept.x
fld FP4(0.)
fstp ra.linept.y
fld FP4(0.)
fstp ra.linept.z

fld FP4(0.)
fstp ra.vector.x
fld FP4(2.)
fstp ra.vector.y
fld FP4(-4.)
fstp ra.vector.z


It doesnot yield intersection warning. I think I close to the result.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

I changed the code to this

fCheckSamClockDir proc uses esi edi lpTri:dword,norm:dword
LOCAL testi, testj, testk:real4
LOCAL dotprod:real4
LOCAL tmp,tmp2,tmp3,tmp4:real4
LOCAL AA,BB:real4

mov esi,lpTri
mov edi,norm

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fstp tmp

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fstp tmp2

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fstp tmp3

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testi
;--------------------------------------------------------------------------------

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fstp tmp

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fstp tmp2

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fstp tmp3

fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testj
;--------------------------------------------------------------------------------
fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fstp tmp

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fstp tmp2

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fstp tmp3

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testk
;--------------------------------------------------------------------------------

fld testi
fmul [edi].VERTEX.x
fstp tmp

fld testj
fmul [edi].VERTEX.y
fadd tmp
fstp tmp

fld testk
fmul [edi].VERTEX.z
fadd tmp
fstp dotprod


xor eax,eax
fldz
fld dotprod
fcomip st(0),st(1)
jb @f
inc eax
@@:


ret
fCheckSamClockDir endp


I hope somebody can help on this. I upload the vector source code, hitchkr made it.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

Phew I got it worked yesterday https://www.asuswebstorage.com/navigate/s/9EB6C10FE1254B91B9CF5B5B6A31E2D1Y

I though building a physic engine would be just as simple as intersect test every triangle, I never though there would be a paralel plane where there is no intersection but it collide each other, it very difficult to test it. I only able to test collision if there is any intesection.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

K_F

Are you looking at 3d object collisions ?

I 'created' a way (theoretically) to detect object collisions, down to any resolution,  with colour coded image objects.
It works purely on integers, but you can extended it to FPs - but it bypasses most of the Matrix Maths, that is generally used in such calculations.

Essentially an optimised idea, particular to an application.. but general in it's idea.
:bgrin:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

Farabi

Why dont you post it on the laboratory subforum and try to create a scratch code for it?
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165