One trick about collisions is that it only does ball-ball collisions (which are easy) and ball-rectangle collisions (only against the "long" sides of the rectangles, it does not have to worry about collision on the "short' side since every rectangle is delimited by two balls, so fbefore colliding on the "short side a ball would have to collide with the ball that is there)
So it does not even have to do rectangle-rectangle collision, since a rectangle collides against another always with one of it's extremities ( a ball) first
every figure is made of balls and rectangles so in the end you just have to figure ball-ball collision and ball-rectangle (on long side) collisions.
I can give you the code for the ropes :) (Blitzmax)
much of the program is done in this fashion (all figures are really "complex ropes" with angle constraints)
As you can see opening the levels with a text editor, everything is made by
-balls (different size, mass...)
-links (every link connects 2 ball and specifies the length and the width of the rectangle that connects them)
-tensions (i.e. the angle you want between 3 linked balls)
So it does not even have to do rectangle-rectangle collision, since a rectangle collides against another always with one of it's extremities ( a ball) first
every figure is made of balls and rectangles so in the end you just have to figure ball-ball collision and ball-rectangle (on long side) collisions.
I can give you the code for the ropes :) (Blitzmax)
Global distance# Global numballs=30 Global shiftx#[numballs] Global shifty#[numballs] Global g#=0.1 'gravity Global r#=10 'radius Global x#[numballs] Global y#[numballs] Global xv#[numballs] 'x velocity Global yv#[numballs] 'y velocity Global dx# Global dy# Global oldx#[numballs] Global oldy#[numballs] For i=0 To numballs-1 x[i]=i y[i]=i Next Graphics (800,600,0,60) Repeat Cls x[0]=MouseX() y[0]=MouseY() xv[0]=0 yv[0]=0 For i=0 To numballs-1 yv[i]=yv[i]+g oldx[i]=x[i] oldy[i]=y[i] x[i]=x[i]+xv[i] y[i]=y[i]+yv[i] Next For j= 0 To 30 For i=0 To numballs-1 shiftx[i]=0 shifty[i]=0 Next For i=0 To numballs-2 dx=(x[i+1]-x[i]) dy=(y[i+1]-y[i]) distance = Sqr(dx*dx + dy*dy) shiftx[i]=shiftx[i]+((distance - 2*r) / 4.0) * (dx / distance ); shifty[i]=shifty[i]+((distance - 2*r) / 4.0) * (dy / distance ); shiftx[i+1] = shiftx[i+1]-((distance - 2*r) / 4.0) * (dx / distance ); shifty[i+1] = shifty[i+1]-((distance - 2*r) / 4.0) * (dy / distance ); Next For i=0 To numballs-1 x[i] = x[i]+shiftx[i] y[i] = y[i]+shifty[i] Next Next For i=0 To numballs-1 xv[i] = x[i]-oldx[i] yv[i] = y[i]-oldy[i] Next For i=0 To numballs-1 DrawOval(x[i],y[i],2*r,2*r) Next FlushMem() Flip Until (KeyDown(key_escape))
much of the program is done in this fashion (all figures are really "complex ropes" with angle constraints)
As you can see opening the levels with a text editor, everything is made by
-balls (different size, mass...)
-links (every link connects 2 ball and specifies the length and the width of the rectangle that connects them)
-tensions (i.e. the angle you want between 3 linked balls)