Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
2bad4you
2bad4you's Profile
2
Posts
0
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
struggling with collision detection! - beginner
[1 Reply]
23-Jan-2011 03:42 AM
Forum:
Programming Questions
// Basic Breakout game
// brick position
float brickX;
float brickY;
// brick width and height
float brickH;
float brickW;
// ball position
float ballX;
float ballY;
// ball diameter
float ballD;
// ball direction
float ballDx;
float ballDy;
// bat position
float batX;
// bat width
float batW;
float batH;
// bat colour
float batB;
void setup() {
size (500, 500, P2D);
// set sizes of game items
brickW = 100;
brickH = 50;
batW = 100;
batH = 25;
ballD = 25;
batB = 255;
// random brick position
brickX = random(0, width - brickW);
brickY = random(0, height / 2);
// bat in the centre
batX = (width/2) - (batW/2);
// ball atop bat
ballX = batX + (batW/2);
ballY = height - batH - (ballD/2);
// ball movement
ballDx = random(-5, 5);
ballDy = -5;
rectMode(CORNER);
ellipseMode(CENTER);
}
void draw() {
// check for ball collision
// with top or sides of bat
checkBallAgainstBat();
// check for ball collision with
// left right and top walls
// and bounce
checkBallAgainstWalls();
// check ball against brick
checkBallAgainstBrick();
// move the ball
ballX += ballDx;
ballY += ballDy;
background(0);
// draw the bat
fill(0, 255, 0);
rect(batX, height - batH, batW, batH);
// draw the brick
fill(0, 0, batB);
batB = (batB + 10) % 255;
rect(brickX, brickY, brickW, brickH);
// draw the ball
fill(255, 0, 0);
ellipse(ballX, ballY, ballD, ballD);
if (keyCode == 37) { // left cursor key
batX -= 10;
// keep it on the screen
if (batX < 0) {
batX = 0;
}
}
if (keyCode == 39) {
batX += 10;
if (batX > (width - batW)) {
batX = width - batW;
}
}
}
// when they let go of the key, reset the keyCode
void keyReleased() {
keyCode = -1;
}
// this function checks if the ball has hit the top or sides of the bat and update its
// direction as appropriate so the ball bounces off the bat
void checkBallAgainstBat(){
}
// this function checks if the ball has hit the brick. It should bounce off the brick and return true if so
boolean checkBallAgainstBrick(){
return false;
}
// this function checks if the ball has hit the top, left or right walls and update its
// direction as appropriate so the ball bounces off the walls
void checkBallAgainstWalls(){
if (ballX + ballD > width){// right wall
ballDx *= -1;
}
if (ballX - ballD < 0){// left wall
ballDx *= -1;
}
if (ballY - ballD < 0){// top wall
ballDy *= -1;
}
}
this is the code that I have been given. i've tried to update the checkBallAgainstBat method and came up with something like that:
void checkBallAgainstBat(){
if (ballY - ballD > height - batH){
ballDy *= -ballD;
}
if (ballX - ballD < width/2 + batW/2){
ballD *= -ballD;
}
if (ballX - ballD < width/2 - batW/2){
ballD *= -ballD;
}
}
but it's wrong and doesn't work. i can't fully understand how this kind of collision detection is supposed to work.
anyone kind enough to explain it to me?
ps. all the basic concepts seemed pretty easy to me and this is the first brick wall i encountered :/
moving into object oriented programming
[2 Replies]
12-Dec-2010 02:56 AM
Forum:
Programming Questions
Hi,
I am a beginner at Processing so forgive me if my question is stupid/simple.
This is Roger following the mouse:
void setup() {
size (400, 400);
}
void draw() {
background (255);
ellipseMode(CENTER);
noStroke();
fill(156, 109, 203);
//main body
ellipse(mouseX, mouseY, 180, 180);
//ears
ellipse(mouseX-20, mouseY-90, 40, 120);
ellipse(mouseX+20, mouseY-90, 40, 120);
//eyes
fill(0, 0, 0);
ellipse(mouseX-15, mouseY-30, 30, 30);
ellipse(mouseX+15, mouseY-30, 30, 30);
fill(255, 255, 255);
ellipse(mouseX-7, mouseY-24, 10, 10);
ellipse(mouseX+7, mouseY-24, 10, 10);
//lips
smooth();
strokeWeight(10);
stroke(255);
line(mouseX-40, mouseY+20, mouseX+40, mouseY+20);
}
It works just fine. However, when I wanted to translate it into object oriented code something doesn't work and I don't really know why.
Roger roger;
void setup() {
size (500, 500);
background(255);
roger = new Roger(200, 180, 180, 180);
}
void draw() {
roger.display();
roger.rabbitFollowsMouse();
}
class Roger {
float a;
float b;
float c;
float d;
Roger (float a, float b, float c, float d) {
a = a;
b = b;
c = c;
d = d;
}
void display() {
noStroke();
fill(156, 109, 203);
//main body
ellipse(a, b, c, d);
//ears
ellipse(a-20, b-90, c-140, d-60);
ellipse(a+20, b-90, c-140, d-60);
//eyes
fill(0);
ellipse(a-15, b-30, c-150, d-150);
ellipse(a+15, b-30, c-150, d-150);
fill(255);
ellipse(a-7, b-27, c-170, d-170);
ellipse(a+7, b-27, c-170, d-170);
//lips
smooth();
strokeWeight(10);
stroke(255);
line(a-40, b+20, c+60, d+20);
}
void rabbitFollowsMouse() {
a = mouseX;
b = mouseY;
//main body
ellipse(mouseX, mouseY, c, d);
//ears
ellipse(mouseX-20, mouseY-90, c-140, d-60);
ellipse(mouseX+20, mouseY-90, c-140, d-60);
//eyes
ellipse(mouseX-15, mouseY-30, c-150, d-150);
ellipse(mouseX+15, mouseY-30, c-150, d-150);
ellipse(mouseX-7, mouseY-24, c-170, d-170);
ellipse(mouseX+7, mouseY-24, c-170, d-170);
//lips
line (mouseX-40, mouseY+20, c+60, d+20);
}
}
Can anyone see any mistakes?
Thanks is advance!
2bad
«Prev
Next »
Moderate user : 2bad4you
Forum