New here

Previous topic - Next topic

TheFOL

Hi all, im completely new to GLBasic but after looking through this website and at some sample code im tempted to purchase it as it seems pretty powerfull, but im struggling a little.

Could some one help me with a bit of code, what i want to do is have a number of circles, say 10, bounce around the screen and off each other, this probably seems quite simple to some of you.

Any help with that will be greatly appreciated.

Ian Price

Firstly, Hi! :)

Secondly your example isn't actually as simple as it would seem from that sentence.

Having the balls bounce around independently is easy - just use COS and SIN values for their rise and fall, however the bouncing off each other is actually quite complex, as you'd have to take into account collisions, trajectory, velocity, etc. etc. You can fudge this, using collision detection routines and reversing ball trajectories, however your best bet would be to use a physics library. GLBasic can use some physics libs such as Bullet physics, iXors, Newton etc. There are examples of these in the GLBasic Samples directory. And of course, there's also plenty of help on the forums.
I came. I saw. I played.

TheFOL

Thanks for the reply,

Sounds more complicated than i thought, just to clarify i am talking 2D rather than 3D, so does newton etc apply to 2D for physics too?.

Ian Price

If you want realistic/semi-realistic interaction then yes. 2D and 3D.

Like I said, you can do it without, but it's not going to look very real.
I came. I saw. I played.

Moebius

Here's code for circle to circle collisions (co-incidentally involved heavily in my current GLB project...)
given the positions and velocities of each "ball" it checks for collisions and calculates the new velocities.  It's not perfect, but as long as things aren't moving at very high speeds everything works out very close to perfectly (i.e. it looks VERY real I assure you):

Code (glbasic) Select
// (x1,y1) = coords of first ball.  (x2,y2) for second ball.
// (vx1,vy1) = velocity of first ball.  vx2, vy2 for second.

LOCAL dx, dy, d2
dx = x1 - x2
dy = y1 - y2
d2 = dx * dx + dy * dy

IF d2 < Size * Size

LOCAL d, ax, ay, vj1, vi1, vj2, vi2

d = SQR(d2)
ax = dx / d
ay = dy / d

//Calculate new velocities:
vi1 = ax * vx1 + ay * vy1
vi2 = ax * vx2 + ay * vy2
vj1 = ay * vx1 - ax * vy1
vj2 = ay * vx2 - ax * vy2

vx1 = ax * vi2 + ay * vj1
vx2 = ax * vi1 + ay * vj2
vy1 = ay * vi2 - ax * vj1
vy2 = ay * vi1 - ax * vj2

// Move balls away from each other (i.e. fix overlaps)
x1 = x1 + ax * (Size - d)
y1 = y1 + ay * (Size - d)
x2 = x2 - ax * (Size - d)
y2 = y2 - ay * (Size - d)

ENDIF



Of course, you'll need to make this work with arrays of types in order to handle multiple circles bouncing:

Code (glbasic) Select
TYPE Circle
x
y
vx
vy
ENDTYPE

LOCAL Circles[] AS Circle
DIM Circles[10]


And use ALIAS or change the variable names for the collision detection.....

Simple enough?   :P
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Ian Price

I'm guessing he wanted something a bit simpler than that... ;) :P
I came. I saw. I played.

Moebius

Yeah lol :P

You could just make it into a function actually:
Code (glbasic) Select
// BYREF means the variables you plug in will be changed by the function:
FUNCTION Collision:  BYREF x1, BYREF y1, BYREF x2, BYREF y2, BYREF vx1, BYREF vy1, BYREF vx2, BYREF vy2
// Collision code here
ENDFUNCTION


and that would work at lot more simply:
Code (glbasic) Select
Collision( Ball1x, Ball1y,  Ball2x, Ball2y,  Ball1vx, Ball1vy,  Ball2vx, Ball2vy )
just call it once for each pair of circles and that would work out rather nicely.
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

quangdx

I spent a while trying to work out simple 2d circle collision routines,
for a hungry hippos type game.
but in the end it drove me a little mad and I went with the physics engine Box2D
http://www.glbasic.com/forum/index.php?topic=3561.0

Asobi tech - the science of play.
Spare time indiegame developer.

Ian Price

QuoteI spent a while trying to work out simple 2d circle collision routines,
for a hungry hippos type game.
Matchy (IIRC) did an unofficial Hungry Hippos game for iOS. Sadly it got pulled. Someone over at RR did a remake of Zampabalos for the Speccy - http://www.worldofspectrum.org/infoseekid.cgi?id=0013946
I came. I saw. I played.

TheFOL

Thanks for all the help guys, going to work from the code given and see if i can get it all working.

matchy

I've hesitated to change the hippos and include Box2D and re-release it but it's not really an exciting and original game otherwise.

Albert

Or simply try the box2d sample on your glbasic\sample directory and modify it.

Ian Price

QuoteOr simply try the box2d sample on your glbasic\sample directory and modify it.

QuoteGLBasic can use some physics libs such as Bullet physics, iXors, Newton etc. There are examples of these in the GLBasic Samples directory.

Beat ya! ;) :P
I came. I saw. I played.

Moru

And gravity is simplest of them all. You need a variable with vertical speed that you add a constant to every timeframe. On contact with ground you invert it and reduce it with a small amount so the bounce will gradually die down. Looks very realistic if you have the right values.

Code (glbasic) Select

WHILE TRUE
    INC vertical_speed, 1
    INC y_pos, vertical_speed
    IF (y_pos > screen_height)
        vertical_speed = -vertical_speed * 0.9
    ENDIF
    Draw_ball()
    SHOWSCREEN
WEND