collision basics....

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

collision basics....

Post by thesanman112 »

well heres about the easiest demo I can come up with for 3d collision, hope it helps..

Code: Select all


screen 12,,2
vp=1
dim  as single s,dis4,dis5,dis6,dis8,dis9,disa
'
xp=180:yp=180:zp=100
x1=100:x2=200:x3=100:y1=200:y2=200:y3=100:z1=100:z2=100:z3=100

dis8=sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
dis9=sqr((x2-x3)^2+(y2-y3)^2+(z2-z3)^2)
disa=sqr((x3-x1)^2+(y3-y1)^2+(z3-z1)^2)

start:
cls
line(x1,y1)-(x2,y2):
line(x2,y2)-(x3,y3)
line(x3,y3)-(x1,y1)

dis4=sqr((x1-xp)^2+(y1-yp)^2+(z1-zp)^2)
dis5=sqr((xp-x2)^2+(yp-y2)^2+(z2-zp)^2)
dis6=sqr((x3-xp)^2+(y3-yp)^2+(z3-zp)^2)
'start:
pset (xp,yp)
k$=inkey$
if k$="d" then xp=xp+1
if k$="s" then xp=xp-1
if k$="e" then yp=yp-1
if k$="x" then yp=yp+1
if k$="q" then zp=zp+1
if k$="a" then zp=zp-1
if k$=chr$(27) then end

locate 1,1
print"press e,x,s,d to move"
locate 15,1:
s=(dis8+dis9+disa)/2
Area=sqr(s*(s-dis8)*(s-dis9)*(s-disa)):'print area''
s=(dis4+dis5+dis8)/2
area1=sqr(s*(s-dis4)*(s-dis5)*(s-dis8)):'print area1
s=(dis5+dis6+dis9)/2
area2=sqr(s*(s-dis5)*(s-dis6)*(s-dis9)):'print area2
s=(dis4+disa+dis6)/2
area3=sqr(s*(s-dis4)*(s-disa)*(s-dis6)):'print area3

line(x1,y1)-(xp,yp),4
line(x2,y2)-(xp,yp),4
line(x3,y3)-(xp,yp),4

print int(area1+area2+area3+.5)
print area

swap vp,wp
 screenset wp,vP
goto start
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

looky

Post by thesanman112 »

here is a demo for noob
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

bing

Post by thesanman112 »

scotty, hope it helps.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: collision basics....

Post by thesanman112 »

Just to refresh my memory...
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: collision basics....

Post by Tourist Trap »

It is to be compiled in lang=qb I guess. No, it's fblite.
Ok this should work for everyone so:

Code: Select all

#lang "fblite"

screen 12,,2
vp=1
dim  as single s,dis4,dis5,dis6,dis8,dis9,disa
'
xp=180:yp=180:zp=100
x1=100:x2=200:x3=100:y1=200:y2=200:y3=100:z1=100:z2=100:z3=100

dis8=sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
dis9=sqr((x2-x3)^2+(y2-y3)^2+(z2-z3)^2)
disa=sqr((x3-x1)^2+(y3-y1)^2+(z3-z1)^2)

start:
cls
line(x1,y1)-(x2,y2):
line(x2,y2)-(x3,y3)
line(x3,y3)-(x1,y1)

dis4=sqr((x1-xp)^2+(y1-yp)^2+(z1-zp)^2)
dis5=sqr((xp-x2)^2+(yp-y2)^2+(z2-zp)^2)
dis6=sqr((x3-xp)^2+(y3-yp)^2+(z3-zp)^2)
'start:
pset (xp,yp)
k$=inkey$
if k$="d" then xp=xp+1
if k$="s" then xp=xp-1
if k$="e" then yp=yp-1
if k$="x" then yp=yp+1
if k$="q" then zp=zp+1
if k$="a" then zp=zp-1
if k$=chr$(27) then end

locate 1,1
print"press e,x,s,d to move"
locate 15,1:
s=(dis8+dis9+disa)/2
Area=sqr(s*(s-dis8)*(s-dis9)*(s-disa)):'print area''
s=(dis4+dis5+dis8)/2
area1=sqr(s*(s-dis4)*(s-dis5)*(s-dis8)):'print area1
s=(dis5+dis6+dis9)/2
area2=sqr(s*(s-dis5)*(s-dis6)*(s-dis9)):'print area2
s=(dis4+disa+dis6)/2
area3=sqr(s*(s-dis4)*(s-disa)*(s-dis6)):'print area3

line(x1,y1)-(xp,yp),4
line(x2,y2)-(xp,yp),4
line(x3,y3)-(xp,yp),4

print int(area1+area2+area3+.5)
print area

swap vp,wp
screenset wp, vP
goto start
However, I don't really see what is colliding with what here. Can a word of tutorial be added?
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: collision basics....

Post by thesanman112 »

Its for 3d collision that I wrote ten years ago.
Basically it finds the area of a triangulated point and compares it to a triangle that is stationary or static.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: collision basics....

Post by thesanman112 »

I know thats really simple terms.
There are three red triangles and one white triangle,
The area of the three red triangles is calculated,
And the area of the white triangle is calculated,
If we want to remain a constant distance away from grey triangle as we move around then we just move player or object up or down in the 3d environment so that the area of the red triangles are, say for example a value of 500 higher. I used this routine for a 3d demo walkaround world that looked like rolling hills..
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: collision basics....

Post by thesanman112 »

I should also mention that apart from qb syntax, this was tested with fb .13...and was optimised as such...i believe at the time it was upwards of 1500 triangles that maintained a good high frame rate using opengl, on a pentium M 1.6gig laptop. And i had all visual opengl effects on. I dont even remember what they were but they were enabled...however to the point, i found that certain ways of doing calculations made things run alot faster for collision. I should also mention the laptop at the time couldnt run q3, but ran quake2 excellently with his res features enabled.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: collision basics....

Post by thesanman112 »

I also remember someone thinking it was strange to see a 2000 line program without a sub or function....hahahaa
If you want it fast then you need to count all the steps and make less of them...
Post Reply