(2D DBP code )
So,
This is probably the messiest code I’ve ever wrote (It was ripped out of a project so there’s a lot of junk code), It’s supposed to be an example on how to code 2d instant bullet collisions(with spheres) the “Right” (vectorial) way, so that one can use the same concept in 3d...
Features ( or feature LoL)
+It’s quite fast,This method gives you the first "hit" object (supposed to be a bullet right ...
...) w/o a single sqrt()
Anyway , Compile and see if you like it, if you do then post something and I’ll write a readable/understandable version and replace this entry with it...
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART
-------------------------------- www.english.pharosfx.com -------------------------------
___
/ / / _ __ __ __ __ _ _
/__/ /_ __/ /_/ / / /_ /_ /
/ / / /_/ / /_/ __/ / _/_
...presents...
-----------------------------------= Line Position 2D =----------------------------------
Got myself a whole day and a "Linear Algebra I" book...
What else could happend...
----------------------------------------------------------------------------------------
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REREMEND
`setup
sync on : autocam off : make matrix 1,1000,1000,10,10
make object sphere 1,50:make object box 1935,1,200,1:pitch object down 1935,90:make mesh from object 1,1935
add limb 1,1,1:offset limb 1,1,0,0,30:position object 1,500,0,500:color object 1,rgb(0,0,255)
randomize timer() : make object cube 666,25
`objects
type obj
x as float
z as float
endtype
dim objects(130) as obj
for t =3 to 130
make object sphere t,50:`DARN IT's the diameter not the radius!!!
objects(t).x = rnd(1000)
objects(t).z = rnd(1000)
position object t, objects(t).x,0,objects(t).z
ghost object on t
next t
`loop vars
angle as float
linha =1 :ok = make vector2(linha)
Perp =2 : ok = make vector2(perp)
rpos=3 : ok = make vector2(rpos)
tempv=4 : ok = make vector2(tempv)
REM main loop
do
set cursor 0,0
`Get vectors
set vector2 linha,newxvalue(0,angle,1),newzvalue(0,angle,1)
set vector2 perp,-y vector2(linha),x vector2(linha)
`normalize vector2 perp,perp <- we don't need to normalize anything cause it's already 1 in lenght (see 2 lines above)
`Check all objects
current#= 999999
for t = 3 to 130
`flag'em
if object in screen(t) then ink 0,0:text object screen X(t),object screen Y(t),Str$(t)
`set rpos to "Object 2 Object" Vector
set vector2 rpos,object position X(1)-object position X(t),Object position Z(1) - object position Z(t)
dist#=dot product vector2(perp,rpos)
`if lower than radius
if abs(dist#)< 25
inlinefactorx as float
inlinefactorz as float
copy vector2 tempv,perp
multiply vector2 tempv,-dist#
`position dummy at "point A"
Ax# =object position X(t)-X vector2(tempv)
Az# =object position Z(t)-Y vector2(tempv)
set vector2 tempv,Ax#- object position x(1),Az# -object position Z(1)
inlinefactorx = x vector2(tempv)/x vector2(linha)
inlinefactorz = y vector2(tempv)/y vector2(linha)
ink rgb(255,255,255),0
if inlinefactorx > 0
if inlinefactorx < current# then current# = inlinefactorx :position object 666, Ax# , 0 , Az#
print "Factor x ";inlinefactorx
print "Line Collision on ";t
endif
endif
next t
`rotate obj
if upkey() then move object 1,1
if downkey() then move object 1,-1
if leftkey() then dec angle,0.5
if rightkey() then inc angle,0.5
Yrotate object 1,angle
position camera object position x(1),500,object position z(1):point camera object position x(1),0,object position z(1)
sync
loop
Bye