[Go to “teaching” in Morimoto Lab]

Game by ball 2

Now you have implemented bouncing of balls by basic programming.
Based on that, here we study easy simulation, for loop, and array to use many balls.


1. Simulation of balls considering gravity etc.

Alt text

Gravity is acceleration of velocity to downward.
It is uniform acceleration so the velocity of a ball changes by an equal amount in every time.

(Ex. C) Actually, the amount to change velocity is .
However, draw() is called 30 times in a sec, and pixel is not meter.
So we don’t adjust simulation to real world properly.
We can achieve gravity-like behavior by adding a constant value when draw() is called.

float bx=200;
float by=100;
float vx = 10;
float vy = 0;
float ballSize = 100;
float gravity = 0.98f;//重力
void setup() {
size(500, 600);
}
void draw() {
background(255);
ellipse(bx, by, ballSize, ballSize);
vy = vy + gravity;//② (Ex. C)
bx = bx+vx;
by = by+vy;//③⑤
if (bx+ballSize/2 >= width) {//(Ex. A) Bounce considering the radius of a ball.
vx = -vx;
bx=width-1-ballSize/2; //(Ex. B)壁に当たるたびに壁の内側まで強制移動→壁に沈むのを防ぐ
}
if (bx < 0) {
vx = -vx;
}
//画面の y 座標は逆なので
//↓が床とのあたり判定
if (by >= height) {//④
vy = -vy; //①
}
if (by < 0) {
vy = -vy;
}
}

(Ex. B) Why the balls sink into the floor?
When a ball is detected by collision detection,
① Velocity vector in Y direction becomes inverted.
② Velocity is added gravity in the next turn.
③ Ball’s position is updated. But, ball cannot come back because the velocity in Y direction was reduced.
④ Then, ball is detected because it is under floor again. Its velocity in Y direction becomes inverted (downward).
⑤ Then the ball sink again.
This process would be repeated.


Ex A. Please implement bouncing to walls and ceiling and floor by considering the radius of balls

Ex B. When balls bounce, move them into the walls (including ceiling and floor).

Ex C. Moreover, consider air drag, wall drag (friction), and reflection coefficient during this simulation.

Hint 1. Air drag makes balls slower always because the balls are always touch with air.
Hint 2. Frictions of walls, ceiling, and floor occur when balls touch them.
Hint 3. Reflection coefficient make balls slower when reflection occurs.
Hint 4. Vector of gravity is always downward however air drags and frictions are not related to directions. They make balls slower anyway. Here, additive calculation is not suitable?
Hint 5. I use below values.

float air_drag = 0.99f;//空気抵抗
float wall_drag = 0.99f;//壁・床の摩擦
float reflection_coe = 0.9f;//はねかえり係数

Ex D. Make two balls and implement their bouncing.

Hint 1. It is different from collision between a ball and mouse. In that case, we have to think distance between a point and a circle. In this case, we have to think distance between circles.
Hint 2. If you worry about overlaps of balls, move them out of each other like bouncing with walls.


2. for and if

Under construction…
From japanese version, you can implement the examples.

2.1

Alt text

size(300,300);
for(int i=0 ; i < 5; i++)
{
println(i);
}

2.2

Alt text

for(int i=0 ; i < 5; i++)
{
ellipse(i*200+100,100,100,100);
//楕円の中心の x 座標、y 座標、横幅、縦幅
}

2.3

Alt text

for(int j=0 ; j < 5; j++)
{
for(int i=0 ; i < 5; i++){
ellipse(i*200+100,j*200+100,100,100);
}
}

2.4 if statement

[See the link about if.]

2.5 else

[See the link about if.]

2.6 operators for comparison

under construction…

2.7 operators for calculation

under construction…


Ex A. Please implement the below by single for loop.

Alt text

Ex B. Draw more than 10 balls and paint balls by different color when they are at triples of 3.

Hint. Use %.

Ex C. Arrange the balls


3. Array

If you want to make animation with many balls, you need each diameter and velocity.
If you do not know array, you have to do this in the below.

int vel0;
int vel1;
int vel2;
int vel3;
int vel4;
int vel5;
//(snip)

If you know array, you can define data like the below.

int [] vel = new int [10];

You can access each data as the below, however

vel[0] = 0;

if you use array with for loop, it is easy like the below.

for(int i=0 ; i < 10; i++)
{
vel[i]=0; //example of assignment
int a = vel[i] + 10; //example of calculation
}

Array is like a shelf which has a lot of withdraws.
We can have a lot of data by one array.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Note!
If you make array with 10 data and access 11th data, then an error will occur.
With array, 0th data is there. So, if you make 10 data, vel[0] to vel[9] are created. There is no vel [10]!

The below example is to draw many balls.

Ex A. Make 10 balls whose velocities and radii are different using array.

Hint. Random method is convenient. random() . [See the link].

Ex B. Make 10 balls animation using array but bouncing between balls are not necessary.

Ex C. ★Please implement the simulation of bounding 10 balls.

Hint 1. Use for loop to deal with each ball. At that time, calculate collision detection between other every balls using another for loop.
Hint 2. So you use double for loop. If you skip the detection between same balls, continue is convenient. [See the link.]

Below should be used when something is happened (mouse click, collision occur…).

Note:
Be careful to adjust the volume of your pc.


Ex A. Find your favorite sound to use for your program and adjust it by parameters of play(rate, amplitude).

[play() in the official web]
In the sample code of Keyboard, there are play(rate, amplitude).