Why wont my ball bouce in the cube :(

I have a problem. I need the ball to bounce inside the cube. but its not :(

Any ideas please?

int counter = 1;
PVector loc;
PVector vel;


void setup() {
  strokeWeight(20);
  size(displayWidth,displayHeight,P3D);
  loc = new PVector(0,0,0);
  vel = new PVector(2.5,5,5);
}http://forum.processing.org/two/post/discussion/7#

void draw() {

  background(198,106,106);

  translate(width/2,height/2);
  rotateX(0);
  smooth();
  rotateY(45);
  noFill();
  box(550,550,550);

  translate(0,0);
  loc.add(vel);

  if ((loc.x > 550) || (loc.x < 0)) {
    vel.x = vel.x * -1;
  }
  if ((loc.y > 550) || (loc.y < 0)) {
    vel.y = vel.y * -1;
  }

  if ((loc.z > 550) || (loc.z < 0)) {
    vel.z = vel.z * -1;
  }
  stroke(0);
  fill(175);
  translate(loc.x,loc.y,loc.z);
  sphere(16);

}
Tagged:

Answers

  • Answer ✓

    the centre of your cube is at 0, 0, 0 (world coords after the width / 2, height / 2 translate)

    the ball bounces in a cube where the corner is 0, 0, 0

    try taking 550/2 off each coordinate in line 39. or, neater, change the boundaries in the bounce tests.

    (and use a constant for BOXSIZE rather than having 550 throughout the code so that when you decide the box needs to be smaller then you only have to change number, not 6 (or 9 as it will be after the above change))

  • cheers mate. it works :D

Sign In or Register to comment.