
-----------------------------------
Thatguy
Sat Jun 01, 2013 9:17 pm

Array Ball Question
-----------------------------------
What is it you are trying to achieve?
To get some help my ball array  so that all balls will hit the left side of the screen and bounce off the right side


What is the problem you are having?

Well in my code, it shows that each time one ball will hit the right side, another ball will be randomly placed onto the screen and start moving from left to right, i want the balls to move individually after each new addition from right to left, the problem is that if one ball hits the left hand side, then all the other balls will change direction as well; I really want all individual balls to make contact and bounce off the left side

Describe what you have tried to solve this problem

I tried doing for loops to model the movement of all balls but the boundry condition only works for one ball

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

View.Set ("graphics:1000;500,buttonbar,offscreenonly")
var BallRadius : int
BallRadius := 12
var BallX : array 1 .. 10 of int % Array of 10 Balls for x position
var BallY : array 1 .. 10 of int % Array of 10 Balls, for y position
var NumBall : int := 1
randint (BallX (NumBall), 0, maxx) %X Position
randint (BallY (NumBall), 0, maxy) %Y Position
loop
    for i : 1 .. NumBall
        drawfilloval (BallX (i), BallY (i), BallRadius, BallRadius, black)
        BallX (i) := BallX (i) - 1
    end for
    View.Update
    cls
    if BallX (NumBall) = BallRadius
            then
        loop
            for d : 1 .. NumBall
                drawfilloval (BallX (d), BallY (d), BallRadius, BallRadius, black)
                BallX (d) := BallX (d) + 1
            end for
            View.Update
            cls
            if BallX (NumBall) = maxx - BallRadius
                    then
                NumBall := NumBall + 1
                randint (BallX (NumBall), 0, maxx)     %X Position
                randint (BallY (NumBall), 0, maxy)     %Y Position
                exit
            end if
        end loop
    end if
end loop


Please specify what version of Turing you are using



Mod Edit:
Make sure to put your code between the code tags in order to preserve whitespace (indentation) and to highlight the syntax.
 ... code ... 
[/code]

-----------------------------------
Zren
Sat Jun 01, 2013 11:07 pm

Re: Array Ball Question
-----------------------------------
Right now, you only have two areas you are applying "movement" or velocity to the ball positions. The first is in the first for loop, when you move everything left 1 pixel. Once. Then once after a new ball is made. The second time is to move everything right until it hits the right wall.

To have each ball move independantly, each ball must have it's own variable(s) in order to represent what direction it is going. So you'd need another array to represent the horizontal velocity of each ball.

Then you'd do: ballPositionX += ballVelocityX on each frame.



Since you're about to use more arrays to represent a single object. I should introduce you to records which can help you simplify it all into a single array. Here's an example taken from a simplified particle engine. Note that each "particle" is essentially a "ball".


type Vector2D :
    record
        x, y : int
    end record

var point : Vector2D
point.x := 124
point.y := 534

type Particle :
    record
        pos : Vector2D % position
        vel : Vector2D % velocity
    end record

var particle : Particle
particle.pos.x := 0
particle.pos.y := 0
particle.vel.x := +1 % Right
particle.vel.y := 0


Basically, we define our own data type by using 
var particles : array 1 .. 10 of Particle

% Init
for i : lower (particles) .. upper (particles)
    particles (i).pos.x := Rand.Int (0, maxx)
    % ...
end for


View.Set ("graphics;offscreenonly")
loop
    % Update
    for i : lower (particles) .. upper (particles)
        % ...
        particles (i).pos.y += particles (i).vel.y
    end for
end loop


If you didn't understand any of that, just ask.

-----------------------------------
Thatguy
Sat Jun 01, 2013 11:52 pm

RE:Array Ball Question
-----------------------------------
I appreciate your help Zren but I am quite a noob at turing. Essentially, I don't really know what record is but I'll look into the tutorials, thanks for your suggestion. If i would need a new array for each position for the ball, would I put that array line of code into both for loops going left and going right? I think I am understanding the general concept, just now sure how to approach it, perhaps you could elaborate a bit more?
Thank You all the same!

-----------------------------------
evildaddy911
Sun Jun 02, 2013 8:30 am

RE:Array Ball Question
-----------------------------------
you already have arrays for position. what you don't have is arrays defining direction and speed (x and y velocities), which you would declare right after (or before if you wanted) you declare the position arrays. next you want to change this
[code] for i : 1 .. NumBall 
        drawfilloval (BallX (i), BallY (i), BallRadius, BallRadius, black) 
        BallX (i) := BallX (i) - 1 
    end for [/code]
to
[code] for i : 1 .. NumBall 
        drawfilloval (BallX (i), BallY (i), BallRadius, BallRadius, black) 
        BallX (i) := BallX (i) + BallX_V (i) % instead of hardcoding "-1"
    end for
[/code]
this allows you to set which direction the individual ball is travelling without changing any of the other balls' direction. if you want it to move left, set BallX_V(i) 0. basicly how you do this is through one big for loop:
[code]
% NOTE: this is not working code. this simply explains the steps

numBalls : int = 5
Ballx, Bally, BallxV, BallyV :  1 .. numBalls of int

%
% initialize ball variables
%

loop

    for 1 