Collision Problems in Processing - java

So I'm trying to make a platformer game and I'm just starting to work on collision. Right now the collisions very glitchy and only works if its on the side of a rectangle and not the top (this makes more sense if you run the code). I'm wondering how I can get the collision to be cleaner and overall less glitchy.
float x = 100;
float y = 100;
float vy = 0;
float vx = 0;
boolean rightCollision = false;
boolean leftCollision = false;
void setup(){
size(700, 500);
}
void draw(){
background(0);
fill(255);
rect(x, y, 50, 50);
y += vy;
vy += .3;
x += vx;
vx -= vx * 0.07;
if(y > 400){
vy = 0;
y = 400;
}
if(left){
x -= 1;
vx -= .5;
}
if(right){
x += 1;
vx += .5;
}
if(touching(x, y, 50, 50, 400, 325, 100, 500) && keyCode == RIGHT){
rightCollision = true;
}
else{
rightCollision = false;
}
if(rightCollision){
vx -= 2;
}
if(touching(x, y, 50, 50, 400, 325, 100, 500) && keyCode == LEFT){
leftCollision = true;
}
else{
leftCollision = false;
}
if(leftCollision){
vx += 2;
}
fill(167);
noStroke();
rect(0, 450, 1000, 50);
rect(400, 325, 100, 500);
}
boolean left = false;
boolean right = false;
void keyPressed(){
if(keyCode == RIGHT || key == 'd'){
right = true;
}
if(keyCode == LEFT || key == 'a'){
left = true;
}
if(keyCode == UP || key == 'w'){
if(y == 400){
vy -= 10;
}
}
}
void keyReleased(){
if(keyCode == RIGHT || key == 'd'){
right = false;
}
if(keyCode == LEFT || key == 'a'){
left = false;
}
}
boolean touching(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2){
return x1 + w1 >= x2 && x2 + w2 >= x1 && y1 + h1 >= y2 && y2 + h2 >= y1;
}

You need to detect the collision when the player is moving (vx != 0, vy != 0), not just when the key is pressed.
Set the movement to 0 and limit the player's position when a collision is detected:
int pw = 50, ph = 50;
int ox = 400, oy = 325, ow = 100, oh = 400;
topCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vy > 0.0 && y < oy;
if (topCollision){
vy = 0;
y = oy-ph;
}
rightCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vx < 0.0;
if (rightCollision){
vx = 0;
x = ox+ow;
}
leftCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vx > 0.0;
if (leftCollision) {
vx = 0;
x = ox-pw;
}
Use > instread of >= for the collision detection:
boolean touching(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2){
return x1 + w1 > x2 && x2 + w2 > x1 && y1 + h1 > y2 && y2 + h2 > y1;
}

Related

Ball Collision - Collide with paddle

I'm trying to make a ball switch it's Y-Direction when it collides with the restrictions of the paddle. It is most likely a simple answer. Help would be appreciated. Thank you.
I labeled a couple of things to help.
public class GamePanel extends JPanel implements MouseMotionListener {
int Playerx;
int width;
int height;
// Ball Size
float radius = 20;
float diameter = radius * 2;
// Center of Call
float X = radius + 25;
float Y = radius + 10;
// Direction
float dx = 12;
float dy = 12;
GamePanel(){
setPreferredSize(new Dimension(1440, 900));
setFocusable(true);
setBackground(Color.BLACK);
addMouseMotionListener(this);
Thread thread = new Thread() {
public void run() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
if(X-radius>=Playerx && X-radius <- Playerx + 100 && Y+radius>=800 && Y+radius<=810){
dy = -dy;
Y = height - radius;
}//Statement Above is the issue area
repaint();
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
if(Playerx<=50 && Playerx>= 0){g.fillOval(10,800,100,10);}
if(Playerx>50 && Playerx<1390){g.fillOval(Playerx-50,800,100,10);}
if(Playerx>=1390 && Playerx<1440){g.fillOval(1340,800,100,10);}
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
Playerx = e.getX();
repaint();
}
}
I've tried setting different restrictions and different Y-Directions but it doesn't seem to work. The Ball goes straight through the paddle as if it's not there.
I managed to fix it.
I realized that my Playerx was in the middle of the paddle making everything offset 50.
float X = radius;
float Y = radius;
and
public void run() {
while (true) {
counter++;
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}if(X > Playerx-50 && X < Playerx+50 && Y+radius > paddleHeight && Y < 715){
dy = -dy;
Y = paddleHeight - radius;
}

jumping and moving in processing

I am trying to get a circle to be able to jump and move left and right at the same time, but right now its either only jumping or moving forward at a time. Anyone know how to change my code to solve this? Thanks
float px,py,vx,vy,ax,ay;
boolean canJump = false;
void setup(){
size(600, 400);
ax = 0;
ay = .32;
vx = 0;
vy = 0;
px = 300;
py = 200;
}
int x = 50;
int y = 520;
void draw(){
background(0);
ellipse(px-15, py-30, 60, 60);
vx+=ax;
vy+=ay;
px+=vx;
py+=vy;
if( py > height ){
py = height;
vy = 0;
canJump = true;
}
player();
}
void player(){
fill(255);
rect(0, 550, 1000, 50);
}
void keyPressed(){
if(keyCode == RIGHT || key == 'd'){
px += 10;
}
if(keyCode == LEFT || key == 'a'){
px -= 10;
}
if(keyCode == UP){
if(canJump) {
vy = -10;
canJump = false;
}
}
}
So you can make your ball go both up and right or up and left by checking that both keys are pressed and then you can add to the velocity instead of the position to affect the trajectory of the ball. However, then you must slow down the x component of the velocity when it hits the ground, so I added a friction variable.
float px, py, vx, vy, ax, ay;
boolean canJump = false;
float bounce = 0.2;
float friction = 0.2;
void setup() {
size(600, 400);
ax = 0;
ay = .32;
vx = 0;
vy = 0;
px = 300;
py = 200;
}
int x = 50;
int y = 520;
void draw() {
background(0);
ellipse(px-15, py-30, 60, 60);
vx+=ax;
vy+=ay;
px+=vx;
py+=vy;
if ( py > height ) {
py = height;
vy = -bounce*vy;
vx = friction*vx;
canJump = true;
}
}
void keyPressed() {
if (keyCode == RIGHT && keyCode == UP) {
if (canJump) {
vy = -10;
vx += 5;
canJump = false;
}
} else if (keyCode == LEFT && keyCode == UP) {
if (canJump) {
vy = -10;
vx += -5;
canJump = false;
}
} else {
if (keyCode == RIGHT || key == 'd') {
vx += 5;
}
if (keyCode == LEFT || key == 'a') {
vx -= 5;
}
if (keyCode == UP) {
if (canJump) {
vy = -10;
canJump = false;
}
}
}
}
You can improve the implementation of your program by taking a look at processing's PVector class.
PVector pos;
PVector vel;
PVector acc;
float friction = 0.3;
float bounce = 0.5;
float diameter = 60;
boolean canJump = false;
void setup() {
size(600, 400);
pos = new PVector(300, 200);
vel = new PVector(0, 1);
acc = new PVector(0, 0.32);
}
void draw() {
background(0);
circle(pos.x, pos.y, diameter);
vel.add(acc);
pos.add(vel);
if (pos.y + diameter/2 > height) {
pos.set(pos.x, height-diameter/2);
vel.set(vel.x*friction, -vel.y*bounce);
canJump = true;
}
}
void keyPressed() {
boolean right = keyCode == RIGHT || key == 'd';
boolean left = keyCode == LEFT || key == 'a';
boolean up = keyCode == UP || key == 'w';
if (up && right && canJump) {
vel.add(5, -10);
canJump = false;
} else if (up && left && canJump) {
vel.add(-5, -10);
canJump = false;
} else {
if (up && canJump) {
vel.add(0, -10);
canJump = false;
}
if (right) {
vel.add(5, 0);
}
if (left) {
vel.add(-5, 0);
}
}
}

Processing: Where would I initialize a counter if it is changed in draw?

Table t1;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int stickColR=0;
int stickColG=0;
int stickColB=0;
int counter=0;
//--------------
void setup(){
size(1000,600);
smooth();
t1 = new Table(1000,600);
t1.startGame();
//sound player setup
minim= new Minim(this);
player=minim.loadFile("ballsound.mp3");
}
//--------------
void draw(){
strokeWeight(10);
stroke(255,0,0);
fill(26, 218, 35);
rect(0, 0, 1000, 600);
fill(0);
noStroke();
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
t1.updatePos(); //calling the function to update the position of the balls
t1.visualize(); // Function responsible for drawing the balls
}
//--------------
void mousePressed(){
t1.mousePressed(mouseX-t1.x,mouseY-t1.y);
}
//--------------
void mouseReleased(){
t1.mouseReleased(mouseX-t1.x,mouseY-t1.y);
}
//=============================
class Table{
boolean GameIsOver=false;
float drag = 0.99;
float bounce = 0.4;
float wallBounce = 0.3;
float pushFactor = 0.4;
float maxPush = 20;
color[] ballColors = new color[]{color (255), color (216,7,17),color(17,242,225), color ( #45B4CE) , color ( #6A6347) , color (#E80909) ,color (#CEA9A9)};
Ball[] balls;
Ball selectedBall;
int x,y,width,height;
//--------------
Table(int w, int h){
width = w;
height = h;
}
//--------------
void startGame(){
buildBalls(5);
}
//--------------
void buildBalls(int count){
balls = new Ball[2*count+1];
int x_coor_red=600;
int y_coor_red=300;
int x_coor_green=626;
int y_coor_green=313;
for(int i=0;i<count;i++)
{
balls[i] = new Ball(x_coor_red, y_coor_red,i+1, this);
x_coor_red+=26;
y_coor_red-=13;
if(i>=3)
{
x_coor_red-=26;
y_coor_red+=26;
}
}
for(int i=0;i<count;i++)
{
balls[count+i] = new Ball( x_coor_green, y_coor_green,i+2, this);
x_coor_green+=26;
y_coor_green+=13;
if(i==1)
{
x_coor_green-=26;
y_coor_green-=20;
}
if(i==2)
{
y_coor_green-=20;
}
if(i==3)
{
x_coor_green-=45;
}
}
balls[2*count] = new Ball( 0.5*(width), 0.5*(height),0, this);
}
//--------------
void updatePos(){
//simulation
for(int i=0;i<balls.length;i++)
balls[i].update();
//collision detection
for(int i=0;i<balls.length;i++)
for(int j=i+1;j<balls.length;j++)
balls[i].collisionDetect(balls[j]);
}
//--------------
void visualize(){
translate(x,y);
noStroke();
//draw stick
if(mousePressed && selectedBall!= null && (mouseX<=26+selectedBall.x || mouseX>26-selectedBall.x) && selectedBall.ballColor==color(255)){
stickColR+=2;
stickColB+=2;
strokeWeight(4);
stroke(stickColR, stickColG, stickColB);
line ( mouseX , mouseY , mouseX + cos (atan2 ( mouseY -selectedBall.y , mouseX - selectedBall.x) )*300 , mouseY + sin( atan2 ( mouseY - selectedBall.y , mouseX -selectedBall.x ))*300);
}
//drawing
for(int i=0;i<balls.length;i++){
balls[i].visualize();
//Balls"disappearing" in corners
if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550)){
player.rewind();
player.play();
if(balls[i].ballColor==color(255))
{
textSize(25);
text("Cue Ball Sunk. GAME OVER",350,560);
}
fill(0);
ellipse(0,0,100,100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
balls[i].x=1200;
balls[i].y=0;
counter++;
if (balls[i].ballColor != 255 && counter>=3)
{
textSize(25);
text("YOU WIN", 350,560);
}
}
}
}
//--------------
float kineticEnergy(){
float energy=0;
for(int i=0;i<balls.length;i++)
energy += mag( balls[i].vx, balls[i].vy );
return energy;
}
//--------------
void mousePressed(int mx, int my){
for(int i=0;i<balls.length;i++)
if( dist(balls[i].x,balls[i].y,mx,my) < balls[i].radius) {
selectedBall = balls[i];
break;
}
}
//--------------
void mouseReleased(int mx, int my){
if(selectedBall != null){
float px = (selectedBall.x-mx) * pushFactor;
float py = (selectedBall.y-my) * pushFactor;
float push = mag(px,py);
stickColR=0;
stickColB=0;
if( push > maxPush ){
px = maxPush*px/push;
py = maxPush*py/push;
}
selectedBall.push(px,py);
}
selectedBall = null;
}
}
class Ball{
float x,y,vx,vy,radius,diameter;
int type;
Table table;
color ballColor;
//--------------
Ball(float x, float y, int type, Table t){
this.x = x;
this.y = y;
this.type = type;
diameter = 26;
radius = diameter/2;
table = t;
ballColor = table.ballColors[type];
}
//--------------
void update(){
vx *= table.drag;
vy *= table.drag;
x += vx;
y += vy;
wallBounce();
}
//--------------
void wallBounce(){
if(x<=radius){
vx = -table.wallBounce*vx;
x = radius;
player.rewind();
player.play();
}
if(x>=t1.width-radius){
vx = -table.wallBounce*vx;
x = table.width-radius;
player.rewind();
player.play();
}
if(y<=radius){
vy = -table.wallBounce*vy;
y = radius;
player.rewind();
player.play();
}
if(y>=t1.height-radius){
vy = -table.wallBounce*vy;
y = table.height-radius;
player.rewind();
player.play();
}
}
//--------------
void visualize(){
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x,y,diameter,diameter);
}
//--------------
void push(float dx, float dy){
vx += dx;
vy += dy;
}
//--------------
void collisionDetect(Ball b){
float distance = dist(x,y,b.x,b.y);
if( distance < diameter && (b.x>50 && (b.y>50 || b.y<550) || b.x<950 &&(b.y>50 || b.y<550))){
float vxSum = 0.5*(vx + b.vx);
float vySum = 0.5*(vy + b.vy);
float forceMagnitude = ((b.x-x)*(vx-vxSum) + (b.y-y)*(vy-vySum));
float xForce = 0.25*(x-b.x)*forceMagnitude/distance;
float yForce = 0.25*(y-b.y)*forceMagnitude/distance;
vx = vx + table.bounce * xForce;
vy = vy + table.bounce * yForce;
b.vx = b.vx - table.bounce * xForce;
b.vy = b.vy - table.bounce * yForce;
b.x = x + (diameter+1)*(b.x-x)/distance;
b.y = y + (diameter+1)*(b.y-y)/distance;
player.rewind();
player.play();
}
}
}
Im making a pool game in processing and I'm almost done but I'm stuck here. I want to add a counter that counts when a ball has been in the corner, then once the counter equals ten, the number of balls, it will display the winning message. I think something is going wrong in that the counter is being reset each time since it's in draw? But I'm not sure where else to initialize it. Btw, right now I just have it >= 3 so that I can quickly see if it's working. Thanks all
due to my low reputation, I have to show you my solution even though I think I haven't fixed all the bugs in your code.
I think your bug stated in the question has little relationship with the counter variable. I ran your code and found that you tried to hide the "disappeared" ball by changing coordinates. It means that "disappeared" balls can still be qualified to the if statement below, and as the result, value of counter will keep increasing.
//Balls"disappearing" in corners
if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550))
So you can add a boolean field to the Ball class
class Ball{
float x,y,vx,vy,radius,diameter;
int type;
Table table;
color ballColor;
// add this variable.
boolean disappeared = false;
// your constructor, wallBounce(), push(), collisionDetect()
void visualize(){
// only draw the ball when it hasn't disappeared.
if(!this.disappeared) {
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x,y,diameter,diameter);
}
}
}
and then use it to avoid double counting.
// other code in visualize()
if(!balls[i].disappeared) {
counter++;
balls[i].disappeared = true;
}
// other code
Table t1;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int stickColR=0;
int stickColG=0;
int stickColB=0;
float counter;
boolean isAllIn = false;
//--------------
void setup() {
counter=0;
size(1000, 600);
smooth();
t1 = new Table(1000, 600);
t1.startGame();
//sound player setup
minim= new Minim(this);
player=minim.loadFile("ballsound.mp3");
}
//--------------
void draw() {
strokeWeight(10);
stroke(255, 0, 0);
fill(26, 218, 35);
rect(0, 0, 1000, 600);
fill(0);
noStroke();
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
t1.updatePos(); //calling the function to update the position of the balls
t1.visualize(); // Function responsible for drawing the balls
}
//--------------
void mousePressed() {
t1.mousePressed(mouseX-t1.x, mouseY-t1.y);
}
//--------------
void mouseReleased() {
t1.mouseReleased(mouseX-t1.x, mouseY-t1.y);
}
//=============================
class Table {
float drag = 0.99;
float bounce = 0.4;
float wallBounce = 0.3;
float pushFactor = 0.4;
float maxPush = 20;
color[] ballColors = new color[]{color (255), color (216, 7, 17), color(17, 242, 225), color ( #45B4CE), color ( #6A6347), color (#E80909), color (#CEA9A9)};
Ball[] balls;
Ball selectedBall;
int x, y, width, height;
//--------------
Table(int w, int h) {
width = w;
height = h;
}
//--------------
void startGame() {
buildBalls(5);
}
void restart() {
t1 = new Table(1000, 600);
t1.startGame();
}
//--------------
void buildBalls(int count) {
balls = new Ball[2*count+1];
int x_coor_red=600;
int y_coor_red=300;
int x_coor_green=626;
int y_coor_green=313;
for (int i=0; i<count; i++)
{
balls[i] = new Ball(x_coor_red, y_coor_red, i+1, this);
x_coor_red+=26;
y_coor_red-=13;
if (i>=3)
{
x_coor_red-=26;
y_coor_red+=26;
}
}
for (int i=0; i<count; i++)
{
balls[count+i] = new Ball( x_coor_green, y_coor_green, i+2, this);
x_coor_green+=26;
y_coor_green+=13;
if (i==1)
{
x_coor_green-=26;
y_coor_green-=20;
}
if (i==2)
{
y_coor_green-=20;
}
if (i==3)
{
x_coor_green-=45;
}
}
balls[2*count] = new Ball( 0.5*(width), 0.5*(height), 0, this);
}
//--------------
void updatePos() {
//simulation
for (int i=0; i<balls.length; i++)
balls[i].update();
//collision detection
for (int i=0; i<balls.length; i++)
for (int j=i+1; j<balls.length; j++)
balls[i].collisionDetect(balls[j]);
}
//--------------
void visualize() {
translate(x, y);
noStroke();
//draw stick
if (mousePressed && selectedBall!= null && (mouseX<=26+selectedBall.x || mouseX>26-selectedBall.x) && selectedBall.ballColor==color(255)) {
stickColR+=2;
stickColB+=2;
strokeWeight(4);
stroke(stickColR, stickColG, stickColB);
line ( mouseX, mouseY, mouseX + cos (atan2 ( mouseY -selectedBall.y, mouseX - selectedBall.x) )*300, mouseY + sin( atan2 ( mouseY - selectedBall.y, mouseX -selectedBall.x ))*300);
}
//drawing
for (int i=0; i<balls.length; i++) {
balls[i].visualize();
//Balls"disappearing" in corners
if (balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550)) {
player.rewind();
player.play();
if (balls[i].ballColor==color(255))
{
textSize(25);
text("Cue Ball Sunk. GAME OVER. Press any key to restart.", 170, 560);
if (keyPressed == true) {
t1.restart();
}
}
/*
fill(0);
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
balls[i].x=1200;
balls[i].y=0;
*/
if (!balls[i].disappeared) {
counter++;
balls[i].disappeared=true;
}
if ( counter>=3) {
textSize(25);
text("YOU WIN. Press any key to restart.", 300, 560);
if (keyPressed == true) {
t1.restart();
}
}
}
}
}
//--------------
float kineticEnergy() {
float energy=0;
for (int i=0; i<balls.length; i++)
energy += mag( balls[i].vx, balls[i].vy );
return energy;
}
//--------------
void mousePressed(int mx, int my) {
for (int i=0; i<balls.length; i++)
if ( dist(balls[i].x, balls[i].y, mx, my) < balls[i].radius) {
selectedBall = balls[i];
break;
}
}
//--------------
void mouseReleased(int mx, int my) {
if (selectedBall != null) {
float px = (selectedBall.x-mx) * pushFactor;
float py = (selectedBall.y-my) * pushFactor;
float push = mag(px, py);
stickColR=0;
stickColB=0;
if ( push > maxPush ) {
px = maxPush*px/push;
py = maxPush*py/push;
}
selectedBall.push(px, py);
}
selectedBall = null;
}
}
class Ball {
boolean disappeared = false;
float x, y, vx, vy, radius, diameter;
int type;
Table table;
color ballColor;
//--------------
Ball(float x, float y, int type, Table t) {
this.x = x;
this.y = y;
this.type = type;
diameter = 26;
radius = diameter/2;
table = t;
ballColor = table.ballColors[type];
}
//--------------
void update() {
vx *= table.drag;
vy *= table.drag;
x += vx;
y += vy;
wallBounce();
}
//--------------
void wallBounce() {
if (x<=radius) {
vx = -table.wallBounce*vx;
x = radius;
player.rewind();
player.play();
}
if (x>=t1.width-radius) {
vx = -table.wallBounce*vx;
x = table.width-radius;
player.rewind();
player.play();
}
if (y<=radius) {
vy = -table.wallBounce*vy;
y = radius;
player.rewind();
player.play();
}
if (y>=t1.height-radius) {
vy = -table.wallBounce*vy;
y = table.height-radius;
player.rewind();
player.play();
}
}
//--------------
void visualize() {
if (!this.disappeared) {
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x, y, diameter, diameter);
}
}
//--------------
void push(float dx, float dy) {
vx += dx;
vy += dy;
}
//--------------
void collisionDetect(Ball b) {
float distance = dist(x, y, b.x, b.y);
if ( distance < diameter && (b.x>50 && (b.y>50 || b.y<550) || b.x<950 &&(b.y>50 || b.y<550))) {
float vxSum = 0.5*(vx + b.vx);
float vySum = 0.5*(vy + b.vy);
float forceMagnitude = ((b.x-x)*(vx-vxSum) + (b.y-y)*(vy-vySum));
float xForce = 0.25*(x-b.x)*forceMagnitude/distance;
float yForce = 0.25*(y-b.y)*forceMagnitude/distance;
vx = vx + table.bounce * xForce;
vy = vy + table.bounce * yForce;
b.vx = b.vx - table.bounce * xForce;
b.vy = b.vy - table.bounce * yForce;
b.x = x + (diameter+1)*(b.x-x)/distance;
b.y = y + (diameter+1)*(b.y-y)/distance;
player.rewind();
player.play();
}
}
}

Java BounceBall Ball Move

I have a question.
I'm not getting make the balls escape from the mouse pointer.
All balls go to the left corner when the mouse pointer enters the screen.
What am I doing wrong? Any tips??
My full code:
Java BounceBall mouse event
Or
http://ideone.com/vTGzb7
Method with problem:
public void move(Ball ball, Point mouse) {
try {
Point p = ball.getLocation();
Point speed = ball.getSpeed();
Dimension size = ball.getSize();
int vx = speed.x;
int vy = speed.y;
int x = p.x;
int y = p.y;
// ----------------------
if (mouse != null) {
int xDistance = Math.abs(x + size.width - mouse.x);
int yDistance = Math.abs(y + size.height - mouse.y);
if (xDistance < yDistance) {
if (x + size.width < mouse.x) {
if (vx > 0) {
vx *= -1;
}
} else {
if (vx > 0) {
vx *= -1;
}
}
} else {
if (y + size.height < mouse.y) {
if (vy > 0) {
vy *= -1;
}
} else {
if (vy > 0) {
vy *= -1;
}
}
}
}
// ----------------------
if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
vx *= -1;
}
if (y + vy < 0
|| y + size.height + vy > getParent().getHeight()) {
vy *= -1;
}
x += vx;
y += vy;
ball.setSpeed(new Point(vx, vy));
ball.setLocation(new Point(x, y));
} catch (Exception e) {
e.printStackTrace();
}
}
For some balls it works fine.
They hit in the mouse pointer and change your direction.
But the majority goes to the corner of the screen.
Thank You.
Problem Solved...
Problem: The bubbles were locked in the upper corner of the screen. And do not hit the mouse pointer.
Solution: I calculated the distance from the X and Y position relative to the bubble diameter and the mouse pointer. For collision.
int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);
Then calculated the X and Y radius of the bubbles.
int radiusX = (size.width / 2);
int radiusY = (size.height / 2);
Finally, I changed the IF to check the relationship between the distance of the bubble radius. Changing your direction.
if (xDistance <= radiusX && yDistance <= radiusY) {
if (xDistance < yDistance) {
vx *= -1;
} else {
vy *= -1;
}
System.out.println("Hit!");
}
New Move Method:
public void move(Ball ball, Point mouse) {
try {
Point p = ball.getLocation();
Point speed = ball.getSpeed();
Dimension size = ball.getSize();
int diameter = ball.dimeter;
int vx = speed.x;
int vy = speed.y;
int x = p.x;
int y = p.y;
int radiusX = (size.width / 2);
int radiusY = (size.height / 2);
// ----------------------
if (mouse != null) {
int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);
System.out.printf("b(%d, %d) m(%d, %d) dx(%d, %d)\n", x, y,
mouse.x, mouse.y, (x + vx) - mouse.x, (y + vy)
- mouse.y);
if (xDistance <= radiusX && yDistance <= radiusY) {
if (xDistance < yDistance) {
vx *= -1;
} else {
vy *= -1;
}
System.out.println("Hit");
}
}
if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
vx *= -1;
}
if (y + vy < 0
|| y + size.height + vy > getParent().getHeight()) {
vy *= -1;
}
x += vx;
y += vy;
ball.setSpeed(new Point(vx, vy));
ball.setLocation(new Point(x, y));
} catch (Exception e) {
e.printStackTrace();
}
}

Quick and dirty platformer with Physics has errors with moving platforms

Alright, so I'm trying to make a quick and dirty platformer engine, and I am having some problems with my collision detection and moving platforms. for one thing the "player" seems to bounce on top of moving platforms ever so slightly, and when he hits the right side errors happen as well. I'll upload a jnlp demo so you can try to find more errors and see whats happening, but here is the source:
import java.awt.Rectangle;
import java.util.Vector;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
public class Platformer extends BasicGame{
boolean keys[];
int ALL_KEYS = 0xFF;
Player player;
Vector<Vector<Thing> > things;
int level = 0;
public Platformer() {
super("You've met with a terrible fate, haven't you?");
}
public void init(GameContainer gc) throws SlickException {
keys = new boolean[ALL_KEYS];
for(int i = 0; i < ALL_KEYS; i++){
keys[i] = false;
}
player = new Player();
things = new Vector<Vector<Thing> >();
Vector<Thing> temp = new Vector<Thing>();
temp.add(new Thing(0, 440, 640, 40, 1));
temp.add(new Thing(200, 300, 240, 50, 1));
temp.add(new Thing(500, 200, 240, 50, 1));
things.add(temp);
}
public void update(GameContainer gc, int delta) throws SlickException{
if(keys[Input.KEY_UP]){
player.velo = player.maxJump;
keys[Input.KEY_UP] = false;
}
if(keys[Input.KEY_DOWN]){
keys[Input.KEY_DOWN] = false;
}
if(keys[Input.KEY_LEFT]){
player.delta -= player.speed;
if(player.delta < -player.maxSpeed)
player.delta = -player.maxSpeed;
}
else if(keys[Input.KEY_RIGHT]){
player.delta += player.speed;
if(player.delta > player.maxSpeed)
player.delta = player.maxSpeed;
}
else{
if(player.delta < -0.5){
player.delta += player.speed;
}
else if(player.delta > 0.5){
player.delta -= player.speed;
}
else if(player.delta > -0.5 && player.delta < 0.5){
player.delta = 0;
}
}
if(player.delta < 0)
player.moveLeft(things.get(level));
else if(player.delta > 0)
player.moveRight(things.get(level));
if(player.velo < 0)
player.moveUp(things.get(level));
else
player.moveDown(things.get(level));
things.get(level).get(1).moveRight(player, things.get(level));
}
public void render(GameContainer gc, Graphics g) throws SlickException{
g.setColor(new Color(0,55,55));
g.fillRect(0, 0, 640, 480);
g.setColor(new Color(255,0,0));
g.fillRect(player.x, player.y, player.width, player.height);
for(int i = 0; i < things.get(level).size(); i++){
if(things.get(level).get(i).type == 1)
g.setColor(new Color(0,100,100));
g.fillRect(things.get(level).get(i).x, things.get(level).get(i).y,things.get(level).get(i).width, things.get(level).get(i).height);
}
}
public void keyPressed(int key, char c) {
keys[key] = true;
}
public void keyReleased(int key, char c) {
keys[key] = false;
}
public static void main(String[] args) throws SlickException{
AppGameContainer app =
new AppGameContainer( new Platformer() );
app.setShowFPS(false);
app.setAlwaysRender(true);
app.setTargetFrameRate(60);
app.setDisplayMode(640, 480, false);
app.start();
}
class Player{
float x = 50;
float y = 50;
float delta = 0; // x momentum
float velo = 0;
int height = 50;
int width = 30;
float speed = 0.2f;
int maxSpeed = 6;
int maxFallSpeed = 5;
int maxJump = -8;
public void moveLeft(Vector<Thing> things){
x += delta;
if(x < 0)
x = 0;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x += (things.get(i).x + things.get(i).width) - x;
delta = 0;
}
}
}
public void moveRight(Vector<Thing> things){
x += delta;
if(x + width > 640)
x = (640 - width);
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x -= (x + width) - things.get(i).x;
delta = 0;
}
}
}
public void moveLeftWithThing(Vector<Thing> things, float thingSpeed){
x -= thingSpeed;
if(x < 0)
x = 0;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x += (things.get(i).x + things.get(i).width) - x;
delta = 0;
}
}
}
public void moveRightWithThing(Vector<Thing> things, float thingSpeed){
x += thingSpeed;
if(x + width > 640)
x = (640 - width);
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x -= (x + width) - things.get(i).x;
delta = 0;
}
}
}
public void moveUp(Vector<Thing> things){
y += velo;
velo += speed;
if(velo > maxFallSpeed)
velo = maxFallSpeed;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height/2).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
y += (things.get(i).y + things.get(i).height) - y;
velo = 0;
}
}
}
public void moveDown(Vector<Thing> things){
y += velo;
velo += speed;
if(velo > maxFallSpeed)
velo = maxFallSpeed;
boolean b = false;
for(int i = 0; i < things.size(); i++){
if(!b && new Rectangle((int) x, (int) y + (height/2), width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
y -= (y + height) - things.get(i).y;
velo = 0;
}
}
}
}
class Thing{
float x = 50;
float y = 50;
int height = 50;
int width = 30;
int type = -1;
float speed = 0.5f;
public Thing(float x, float y, int width, int height, int type){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.type = type;
}
public void moveUp(Player player){
y -= 0.5f;
if(new Rectangle((int) x,(int) y, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
player.y -= (player.y + player.height) - y;
player.velo = 0;
}
}
public void moveRight(Player player, Vector<Thing> things){
x += speed;
if(new Rectangle((int) x,(int) y - 1, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
player.moveRightWithThing(things, speed);
}
}
}
}
And here is the demo: http://prime.programming-designs.com/java/platformer_demo/platdemo.jnlp
The reason why you are having trouble finding the error is that the code is ghastly.
Vast amounts of duplication of code: moveLeft, moveRight, moveUp and moveDown are all very similar.
Logic for acceleration due to gravity appears in two places.
Collision logic repeated all over the place.
Desperately poor choice of names. velo for velocity downwards, but delta for velocity rightwards (mislabelled as "momentum"): why not vy and vx? Also maxSpeed for maximum horizontal velocity but maxFallSpeed for maximum vertical velocity.
Acceleration/deceleration called speed.
Wasteful allocation, e.g. calling new Rectangle every time you want to test whether something has hit something else.
Positions are floating-point, but collision is based on integers.
Duplication of code has the effect of introducing bugs, because when functionality is duplicated there's the possibility of getting it wrong in one of the places, and also of hiding bugs, because the volume of code makes it harder to spot them.
Anyway, the bouncing on platforms may be something to do with the fact that in Player:moveDown you are using a rectangle that's offset by half the player's height, whereas in Thing:moveRight you are colliding with a rectangle that's not offset. (But that's just a guess.)

Categories

Resources