Post by Nicky Peter Hollyoake on Dec 20, 2015 17:45:35 GMT -5
It isn't much I was experimenting trying to get the circles to behave how they should when they collide with each other (have them go in the right direction). If anyone has anything much closer or have any changes that will better this, feel free to post it.
- Nicky
const w=50, h=40
'setup 2d screen
glMatrixmode(GL_PROJECTION)
glLoadidentity()
glOrtho(0, w, h, 0, 0, -1)
glMatrixMode(GL_MODELVIEW)
glDisable(GL_DEPTH_TEST)
'''''''''''''''''''''''''''''''''''''''''''''
const CIRCLES= 45 -1
const R# = M_PI*2/360.0
struc sCircle
x#, y#, s# 'x, y, size
vx#, vy#
endstruc
dim sCircle c(CIRCLES) 'circle array
dim j, i, x 'check, loop, loop2
function Collision(x#, y#, a#, b#, s#)
return sqr(pow(x#-a#, 2) + pow(y#-b#, 2))<=s#
endfunction
function angle#(p1#(), p2#())
dim b# = p2#(0)-p1#(0)
dim a# = p2#(1)-p1#(1)
if a#<=0 and b#>=0 then
return abs(atand(b#/a#))
elseif a#>=0 and b#>=0 then
return 180-atand(b#/a#)
elseif a#>=0 and b#<=0 then
return 180-atand(b#/a#)
elseif a#<=0 and b#<=0 then
return 360-atand(b#/a#)
endif
endfunction
'randomize circle positions (with no overlapping)
for i = 0 to CIRCLES
c(i).x#=1+(rnd()/(RND_MAX*1.0))*(w-2)
c(i).y#=1+(rnd()/(RND_MAX*1.0))*(h-2)
if (rnd() % 2 = 1) then c(i).vx# = 0.1 else c(i).vx# =-0.1 endif
if (rnd() % 2 = 1) then c(i).vy# = 0.1 else c(i).vy# =-0.1 endif
for j = 0 to i
if (i <> j) then
if Collision(c(i).x#, c(i).y#, c(j).x#, c(j).y#, 2) then
j=i
i=i-1
endif
endif
next
next
'main loop
while true
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
for i = 0 to CIRCLES
c(i).x#=c(i).x#+c(i).vx#
c(i).y#=c(i).y#+c(i).vy#
for j = 0 to CIRCLES
if (i <> j) then
if Collision(c(i).x#, c(i).y#, c(j).x#, c(j).y#, 2) then
c(i).vx#=-sind(180-angle#(vec2(c(i).x#, c(i).y#), vec2(c(j).x#, c(j).y#)))*0.1
c(i).vy#=-cosd(180+angle#(vec2(c(i).x#, c(i).y#), vec2(c(j).x#, c(j).y#)))*0.1
endif
endif
next
if c(i).x# < 1 or c(i).x# > w-1 then c(i).vx#=-c(i).vx# endif
if c(i).y# < 1 or c(i).y# > h-1 then c(i).vy#=-c(i).vy# endif
glBegin(GL_POINTS)
glVertex2f(c(i).x#, c(i).y#)
glEnd()
glBegin(GL_LINE_LOOP)
for x = 0 to 359
glVertex2f(c(i).x# + sin(R#*x), c(i).y# + cos(R#*x))
next
glEnd()
next
swapBuffers()
sleep(20)
wend
- Nicky