IOCT Masters in Creative Technogies
Major Project
An augmented reality game based on "Breakout"

Showing posts with label collision detection. Show all posts
Showing posts with label collision detection. Show all posts

Tuesday, 8 July 2008

Sound effects

Bloop:






Used during the countdown, synchronised with the countdown

Explosion:





Used when a brick explodes

Waaaa:







Used when a "life" is lost

Boing:






Used when the ball collides with an object on the game screen, including:
  • ball hits brick
  • ball hits border
  • ball hits paddle
Completion:








Used when a level is completed

Sound

I have carried out some research into the use of sound in game and have decided to add sound to the game, to make playing the game a more interesting experience, as it involves more of the senses.

Times in the game that may benefit from sound:
  • Ball and border collision
  • Ball and paddle collision
  • Ball and brick collision
  • Brick explosion
Other times during the game when sound effects or music could be used:
  • Losing a “life”
  • Completing a level
  • Congratulation state
  • During the countdown
  • Introduction to the game

Friday, 4 July 2008

Brick collision each row

The calculations shown in this blog are for the first row of bricks. The calculations are the same for each row, changing the number in the code, for example, bricks1 is used for the first row, bricks 2 for the second row and so on.

Brick collision from above to sides of brick

The collisions to the sides of the brick when the ball is travelling from above are the same as when the ball is travelling from below.

Brick collision from below to right

The code for this is similar to collision detection when the ball is travelling to the left, using i+1 instead of i-1, as used when the ball is travelling to the left.

x1 = (bricks1[i+1].posx – bricks1[i+1].halfwidth) – prevballx – gameball.radius + 4;






Again, check that the collision occurs between the top and bottom of the brick, add one to the hit count each time a brick is hit, add ten to the score when a brick is deleted, reverse the ball direction, calculate the final ball position and check if the level has been completed.

Brick collision from below to the left

Collision on the side of a brick only occurs when the ball is travelling sideways and the brick next to it no longer exists. I need to create an if statement, which says if the ball is travelling to the left, but do not check if the brick being hit is the left-most brick (as there is no brick to the left of it to be hit), then check the side collision.

x1 = prevballx – (bricks1[i-1].posx + bricks1[i-1].halfwidth) – gameball.radius – 4;
t = (float)*1 / (float)gameball.velx;
y1 = (int) (t*(float)gameball.vely);
tempballposx = prevballx + x1;
tempballposy = prevbally + y1;


//Check that the collision is between the top and bottom of the brick
if (bricks1[i-1].posy + bricks1[i-1].halfheight + 7 >= tempballposy && bricks1[i-1].numberhits < level)
{
bricks1[i-1].numberhits ++; //Add one to hit count
if (bricks1[i-1].numberhits == level); //Set up to check if brick hits equals level number then a sound effect could be added later
}
score = score+10; //Add 10 to the score each time a brick is deleted
gameball.velx = - gameball.velx; //Reverse the direction of the ball
gameball.posx = tempballposx + (int) ((1.0-t)*(float)gameball.velx); //Calculate the final ball position
gameball.posy = - tempballposy + (int) ((1.0-t)*(float)gameball.vely); //Calculate the final ball position
checklevelcomplete(); //Check if the level has been completed

Wednesday, 2 July 2008

Brick collision from above

Collision detection to the top edge of the brick when the ball is travelling from above.



y1 = previous ball posy – bricks1[0].posy – bricks1[0].halfheight – radius – 1
t = y1/vely
x1 = t*velx

tempballposx = previous ball posx – x1
tempballposy = previous ball posy – y1

for (i=0; i<12; i++)
if (tempballposx >= bricks1[i].posx – halfwidth && tempballposx =< bricks1[i].posx + halfwidth)
if (bricks1[i].numberhits < level) //Check brick still exists
bricks1[i].numberhits++ //Add one to hit count
vely = - vely //Reverse ball direction
ballposx = tempballposx + (1-t)* velx //Check final ball position
ballposy = tempballposy + (1-t)* vely //Check final ball position

Brick collision from below

Collision detection to the base edge of the brick when the ball is travelling from below.

y1=(bricks1[0].posy-bricks1[0].halfheight) – previous ball position – radius – 1
t = y1/vely
x1 = t*velx

tempballposx = previous ball posx + x1
tempballposy = previous ball posy + y1

for (i=0; i<12;i++)
if (tempballposx>= bricks1[i].posx – halfwidth && tempballposx =< bricks1[i].posx + halfwidth)
if (bricks1[i].numberhits < level) //Check brick still exists
bricks1[i].number hits++ //Add one to hit count
vely = - vely //Reverse the ball direction
ballposx = tempballposx + (1-t)* velx //Check final ball position
ballposy = tempballposy + (1-t)* vely //Check final ball position

Brick collision

To create the collision detection on the bricks, I need to calculate a number of collision detection circumstances, these include:
  • ball travelling from below, hits base of brick
  • ball travelling from above, hits top of brick
  • ball travelling from below, hits right side of brick
  • ball travelling from below, hits left side of brick
  • ball travelling from above, hits right side of brick
  • ball travelling from above, hits left side of brick

Monday, 30 June 2008

Brick properties

Each brick needs properties for positioning, collision detection, design information and keeping a record of how many times each brick has been hit.

Each brick has the following properties:
  • Position in x
  • Position in y
  • Width
  • Height
  • Half width
  • Half height
  • Number of times brick has been hit
  • Main colour
  • Darker colour
  • Lighter colour

To draw each brick, set it’s position and three colours (to create the pseudo 3D effect), then draw it to the screen if the number of times it has been hit is lower than the level number.

pos x = position in x
pos y = position in y

Levels

There are three levels in the game. Each level must be completed, by deleting all of the bricks in the level before the player can progress to the next level. Each level has a different brick design, to make the difference between the levels clear visually.

The levels also differ in the ease with which they are completed.
This increases the difficulty rate of each level as the player progresses through the game.

Level 1 = Each brick hit once then it's deleted
Level 2 = Each brick hit twice
then it's deleted
Level 3 = Each brick hit three times then it's deleted

Monday, 16 June 2008

Collision detection: Paddle

The ball must be projected on to the paddle and the point at which the ball would have contacted the paddle and this point must be calculated. As the paddle has curved edges I tried to angle the edges of the paddle collision detection lines, however, having tried to use the intersection of two lines I could not get the collision detection process to work as desired. I have decided to extend the line across the top of the paddle, so that the collision occurs whenever the ball collides within the length of the paddle.

paddley = top edge of paddle in the y axis

Test:
paddley > position in y – radius

y1=paddley – ball position y + radius
t=y1/vely
x1=t*velx


Test condition, ensure ball hits between paddle edges:
previous ball position x + x1 > paddle position – paddle half width
&&
previous ball position x + x1 < paddle position + paddle half width

ball position x = previous ball position x + x1
ball position y = previous ball position y + y1
vely=-vely
ball position x = ball position x + (1-t)*velx
ball position y = ball position y + (1-t)*vely

Saturday, 14 June 2008

Collision detection: Top border

Test:
maximum y < position in y + radius

y1=maximum y – ball position – radius
t=y1/vely
x1=t*velx
ball position x=previous ball position x + x1
ball position y=previous ball position y + y1
vely=-vely
ball position x=ball position x + (1-t)* velx
ball position y=ball position y + (1-t)* vely

Collision detection: Right border

x1=maximum x - ball position x – ball radius










t=x1/velx
y1=t*vely
ball position x=previous ball position x + x1
ball position y=previous ball position y + y1
velx=-velx
ball position x=ball position x + (1-t)* velx
ball position y=ball position y + (1-t)* vely

Thursday, 12 June 2008

Collision detection: intermediate point

The intermediate point is the collision point.

ball position x = previous ball position x – x1
ball position y = previous ball position y + y1

ball position x = ball position x + (1-t)* velx //end ball position in x equals whole distance to be travelled minus distance travelled before intermediate point, multiplied by velocity in x
ball position y = ball position y + (1-t)* vely //end ball position in y equals whole distance to be travelled minus distance travelled before intermediate point, multiplied by velocity in y

Collision detection: Left border

Collision detection needs to be calculated, as the ball contacts and needs to rebound from the borders around the game board, the paddle and the bricks.

This works on the physics equation:
speed=distance/time

To calculate the movement of the ball after collision I need to know how much time it has taken to reac
h the collision, as a proportion of a time step. The remaining time in the time step after the collision is used by the ball travelling in the opposite direction.
x1=ball position x – minimum x + ball radius //calculate distance ball travelled before collision








t //amount of time step as proportion, between 0 and 1
y1 //distance travelled in y before collision
velx //total distance moved (real and projected) in x in time step
vely //total distance moved (real and projected) in y in time step

t=x1/velx //distance travelled in x in proportion to whole distance to be travelled in time step
y1=t*vely //calculate distance travelled in y before time step, multiply whole distance the ball would travel in time step by proportion already travelled
velx=-velx //reverse direction

Tuesday, 10 June 2008

Creating the paddle

The paddle consists of a rectangle and two circles, one at either end of the rectangle. This gives the paddle curved edges, which makes it more visually interesting than just a rectangular design.

The paddle is created using a number of variables. These variables are used to calculate the position of the paddle, the distances it can travel along the x axis inside of the game board and the way in which it interacts with the ball, through collision detection.

The design ideas for the paddle include a highlight and lowlight stripe in the code to add detail and to help create the pseudo 3D effect.

Monday, 2 June 2008

Planning

I've been busy planning my ideas for the project. I've been creating initial design ideas for the game board/screen and have been trying to work out where the objects will be placed on the screen, how they will interact with one another and specifiying some names for key variables.

The project is heavily based on mathematics, to understand the collision detection and spatial position of objects on the screen. I have been researching and working in my logbook at this planning stage so that I have firm ideas in place before trying to code the game.