Oxygen Basic
Programming => Example Code => Topic started by: Peter on April 05, 2011, 11:20:54 AM
-
Deleted
[attachment deleted by admin]
-
Thanks Peter.
I had a few ideas for more efficient collision detection. It only makes a small difference.
But if there are large numbers of objects I would then adopt a zone mapping strategy to avoid exponential increases in collision tests.
/* Circle Collision by Peter o5/o4/2011 */
indexbase 0
include "wFunc.h"
OpenWindow "Circle Collison",800,600,ws_overlapped
SetFont 16,16,0,0
Long xCir1,yCir1,rCir1,xCir2,yCir2,rCir2,color,px,jx
Long xBox1,yBox1,xBox2,yBox2
Function CircleCollision(long xd, long yd,long rp) as long
'=========================================================
iF xd*xd+yd*yd < rp*rp
return 1
End iF
End Function
Function BoxCollision(long xd, long yd,long rp) as long
'======================================================
iF abs(yd) < rp
iF abs(xd) < rp then return 1
end if
End Function
xCir1 =0 : yCir1=300 : rCir1=20
xCir2 =800 : yCir2=300 : rCir2=30
xBox1=0 : yBox1=200
xBox1=0 : yBox2=220
'====
'MAIN
'====
While WinExit=0
ClearBuffer color
Text "Collision " + Str(px) + "/" + Str(jx),312,32,RGB(255,255,255)
color=0
Circle xCir1,yCir1,rCir1,RGB(255,255,255)
Circle xCir2,yCir2,rCir2,RGB(255,255,255)
xCir1 +=1: xCir2 -=2
iF xCir1 >=800 Then xCir1 =0
iF xCir2 <=0 Then xCir2 =800
px = CircleCollision xCir2-xCir1,yCir2-yCir1, rCir1+rCir2
iF px Then color = RGB(Rand(24,200),Rand(24,200),Rand(24,200))
Box xBox1,yBox1,40,40,RGB(255,255,255)
Box xBox2,yBox2,20,20,RGB(255,255,255)
xBox1 +=2: xBox2 -=2
iF xBox1 >=800 Then xBox1 =0
iF xBox2 <=0 Then xBox2 =840
jx = BoxCollision xBox2-xBox1,yBox2-yBox1,30
iF jx Then color or=255
DoEvents
FlipBuffer
WaitFrames 80
Wend
WinEnd
Charles
-
Hi Peter,
My collision test procedure for circles reduced the load by about 25% by eliminating some parameters and one nested subtraction.
Global and static variables are all referenced from the EBX register. This prevented you from using the ebx register and these variables at the same time. Similarly DLL procedure addresses (& other vectored calls) are also mapped to offsets of the EBX register. Local variables use this EBP register.
BYREF long/sys is the default for prototypes.
If you would like to adopt this convention with BYVAL params then all you have to do is use the #byval directive.
#byval 'default byval params
function f(a,b,c,d) as long
d*=10
function=a*10
end function
long a=1,b=2,c=3,d=4
print f a,b,c,d
print d
#byref 'default byref params
Charles
-
Another more recent innovation :)
You can apply a type to a whole group of parameters.
'These have params BYVAL
'=======================
function f(single a,b,c,d) as single
d*=10
function=b*10
end function
single f(single a,b,c,d)
{
d*=10
function=b*10
}
'These have params BYREF
'=======================
function f(single* a,b,c,d) as single
d*=10
function=b*10
end function
single f(single* a,b,c,d)
{
d*=10
function=b*10
}
single a=1.25,b=2.25,c=3.25,d=4.25
print f a,b,c,d
print d
Charles
-
Before this program gets out of control I thought I had better post it now :)
This is collision detection with zone proximity testing. It also has some elementary elastic collision physics.
The OOP helps to keep it all well encapsulated.
Charles
[attachment deleted by admin]
-
Hi Peter,
Glad you liked it. I hope the class syntax might be useful for managing game objects. I am still working on the best way to make objects interact. This is my best scheme so far.
FS and GS are reserved names along with a number of other specialised CPU registers. There are so many of them that they often interfere with the variable name space and cause inconvenience to the programmer. I should be able to make Oxygen a bit smarter and interpret these as registers only when used as Assembler operands. But maybe keep the main 32/64 bit registers reserved and available in Basic expressions: EAX, ECX etc
Charles
-
Nice example guys, thanks.