View Full Version : Enclosed moving ball
This beginner here is strugling to solve some math problems:
Imagine a ball moving in a room. When the ball hits a wall, its movement vector is reflected with the same incident angle. (disconsider friction and loss of energy).
Now, in my game, I have an object where I know it's position (x,y) and it's direction angle (0 to 360). How to reproduce the above cited behavior?
I remember one of the first virus around for PC-XT's, the pingpong virus that do exactly what I'm describing.
Any hints?
Stevie56
16-01-2003, 09:30 PM
Well, luckily, you've kept the problem simple and are only concerned with orthogonal collisions (the floor, ceiling and walls are flat, horizontal and vertical).
First, I believe you would find it easier to control the balls by defining a type
type ball=record
X,Y:integer;
dX,dY:real;
end;
X, Y are the current location.
dX and dY are the speeds (positive or negative) along X and Y.
they might be kept in an array:-
var balls:array[0..16] of ball;
And initialised at random:-
procedure randomisethem(width,height:integer);
//width, height are the area where we want the balls to stay from 0,0
var t:integer;
begin
for t:=0 to 16 do
begin
balls[t].X:=random(width);
balls[t].Y:=random(height);
balls[t].dX:=random-0.5; //random speeds
balls[t].dY:=random-0.5;
end;
end;
and I'd move them....
Procedure movethem(width,height:integer);
var t:integer;
begin
for t:=0 to 16 do
begin
with balls[t] do
begin
X:=round(X+dX);
Y:=round(Y+dY);
//and test for out of bounds
if X<0 or X>width then begin
dX:=-dX; //reverse direction
X;=round(X+2*dX); //bring back inside arena
end;
if Y<0 or Y>height then begin
dY:=-dY; //reverse direction
Y;=round(Y+2*dY); //bring back inside arena
end;
end;
end;
end;
Hope this helps a little.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.