We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm having some real trouble getting my two Classes to acknowledge one another and create some sort of collision physics. i'll post my code below, just wondering if there's a way in which this can be achieved. The game is based on table top football, so the ball should be reacting to the 'player' by moving in the opposite direction once they collide, both on the X and Y axis.
Thanks in advance
Ball ball;
Timer startTimer;
Blue blue;
Red red;
void setup () {
size (1200, 800);
frameRate(100);
startTimer = new Timer (0);
ball = new Ball (600, 400,15);
blue = new Blue (400, 230, 50,0,0,255);
red = new Red (800, 230, 50, 200, 255, 0, 0);
}
void draw () {
drawPitch ();
ball.display();
ball.update();
ball.bCollision();
startTimer.countUp();
blue.displayBlue();
blue.moveBlue();
fill(0);
textSize (20);
text (startTimer.getTime(), 575, 20);
println (mouseX, mouseY);
}
void drawPitch () {
background (#4BAF22);
//SIDELINES
strokeWeight (5);
stroke(255);
line (50, 780, 1150, 780);
line (50, 20, 1150, 20);
//GOAL LINES
line (50, 20, 50, 780);
line (1150, 780, 1150, 20);
//CENTER LINE
line (600, 20, 600, 780);
//CENTER ELLIPSE
fill (255);
ellipse (600, 400, 20, 20);
//CENTER BOX (CIRCLE)
noFill();
ellipse (600, 400, 250, 250);
//REDTEAM BOX
line (50, 120, 280, 120);
line (50, 680, 280, 680);
line (280, 120, 280, 680);
//REDTEAM INNER BOX
line (50, 275, 120, 275);
line (50, 525, 120, 525);
line (120, 275, 120, 525);
//REDTEAM PENATLY SPOT
fill(255);
ellipse (200, 400, 20, 20);
//REDTEAM GOAL
noFill();
rect (0, 337, 50, 124);
//REDTEAM SEMICIRCLE
arc (280, 400, 125, 200, -HALF_PI, HALF_PI);
//BLUETEAM BOX
line (1150, 120, 925, 120);
line (1150, 680, 925, 680);
line (925, 120, 925, 680);
//BLUETEAM INNER BOX
line (1080, 275, 1150, 275);
line (1080, 525, 1150, 525);
line (1080, 275, 1080, 525);
//BLUETEAM PENALTY SPOT
fill(255);
//BLUETEAM GOAL
ellipse (1000, 400, 20, 20);
noFill();
rect(1150, 337, 1200, 124);
//BLUETEAM SEMICIRCLE
arc (925, 400, 125, 200, HALF_PI, PI+PI-HALF_PI);
//CORNERS
arc (50, 20, 70, 75, -QUARTER_PI+QUARTER_PI, QUARTER_PI+QUARTER_PI);
arc (50, 780, 70, 75, -HALF_PI, PI-PI);
arc (1150, 780, 70, 75, PI, PI+QUARTER_PI+QUARTER_PI);
arc (1150, 20, 70, 75, HALF_PI, PI);
}
class Ball {
PVector pos1 = new PVector (0, 0);
PVector velo;
float radius;
float m;
Ball (float x, float y, float r_) {
pos1 = new PVector (x, y);
velo = PVector.random2D();
velo.mult(3);
radius = r_;
m = radius * .1;
}
void update () {
pos1.add(velo);
}
void pCollision () {
}
void bCollision () {
if (pos1.x > width-60) { //RIGHT WALL
pos1.x = width-60;
velo.x *= -1;
} else if (pos1.x < 60 ) { //LEFT WALL
pos1.x = 60;
velo.x *= -1;
} else if (pos1.y > height-35) { //BOTTOM WALL
pos1.y = height-35;
velo.y *= -1;
} else if (pos1.y <40) { //TOP WALL
pos1.y = 40;
velo.y *= -1;
}
}
void display () {
noStroke();
fill(0);
ellipse (pos.x, pos.y, radius*2, radius*2);
}
}
class Blue {
PVector pos;
float Size = 50;
float Speed = 0.1;
float radius;
color Colour = color (0, 0, 255);
Blue (float x, float y, float r_, int red, int green, int blue) {
pos = new PVector(x, y);
radius = r_;
Colour = color (red, green, blue);
}
void displayBlue () {
fill (Colour);
strokeWeight (2);
stroke(255);
ellipse(pos.x, pos.y, radius, radius);
}
void moveBlue () {
if ((keyPressed == true) && (key == 'a')) {
pos.x = pos.x -3;
}
if ((keyPressed == true) && (key == 'd')) {
pos.x = pos.x +3;
}
if ((keyPressed == true) && (key == 'w')) {
pos.y = pos.y -3;
}
if ((keyPressed == true) && (key == 's')) {
pos.y = pos.y +3;
}
}
}
class Red {
float xPos;
float yPos;
float Size = 50;
float Speed = 0.1;
color Colour = color (0, 0, 255);
PVector Direction;
Red (float x, float y, float size, float speed, int red, int green, int blue) {
yPos = y;
xPos = x;
Size = size;
Speed = speed;
Colour = color (red, green, blue);
}
void display () {
fill (Colour);
strokeWeight (2);
stroke(255);
ellipse(xPos, yPos, Size, Size);
}
void move () {
if ((keyPressed == true) && (key == 'a')) {
xPos = xPos -2;
}
if ((keyPressed == true) && (key == 'd')) {
xPos = xPos +2;
}
if ((keyPressed == true) && (key == 'w')) {
yPos = yPos -2;
}
if ((keyPressed == true) && (key == 's')) {
yPos = yPos +2;
}
}
}
class Timer {
float Time; //ATTRIBUTES
Timer (float set) { //CONSTRUCTOR
Time = set;
}
float getTime() { //RETURNS CURRENT TIME
return(Time);
}
void setTime (float set) { // SET TIMER
Time = set;
}
void countUp () { //UPDATE TIMER +
Time += 1/frameRate;
}
void countDown () {
Time -=1/frameRate;
}
}
Answers
We cannot run your code: Timer, Red, Blue, etc. missing. Please update your code above.
Kf
I've updated the code, you should have it all now
Solved?
Sadly not, still can't quite figure out how to make them collide properly, i was hoping to have an element of velocity that worked in such a way when the player collided with the ball, the ball moved a certain distance but no luck
Have you seen the various examples?
For example
https://processing.org/examples/bouncybubbles.html
I'll check that out thanks a bunch :)