Loading...
Logo
Processing Forum
Hi there, im doing a basic rectangle that falls and is affected by gravity, is an exercise to create it in an OO manner.

but something is wrong... the rect stays fixed on the middle upper part of the sdcreen...

any ideas whats wrong ? thanks!

here is in openprocesing:  ( by the way , whats the best service to share code ? )

 
Copy code
  1. RectGravity myRectGravity;

  2. void setup(){
  3. size (200,200);
  4. smooth ();
  5. myRectGravity = new RectGravity(); 
  6. }

  7. void draw(){
  8.     myRectGravity.display();

  9. }

  10. class RectGravity{
  11.                      //Data
  12.          float x;
  13.         float y ;
  14.     float speed ;
  15.   float gravity ;
  16.                      //Constructor
  17.    RectGravity(){ 
  18.          x = 100;
  19.            y = 0;
  20.        speed = 0;
  21.    gravity = 0.1; 
  22.   }
  23.                      // Function

  24. void display(){
  25. fill (0);
  26. noStroke ();
  27. rectMode (CENTER);
  28. rect (x,y,10,10);

  29. y = y + speed ;
  30. speed = speed + gravity;

  31. if ( y < height){
  32. speed = speed * -0.95;}
  33. }
  34.   
  35. }

Replies(3)


here


Copy code
  1. RectGravity myRectGravity;
  2. void setup() {
  3.   size (200, 200);
  4.   smooth ();
  5.   background (111);
  6.   myRectGravity = new RectGravity();
  7. }
  8. void draw() {
  9.   background (111);
  10.   myRectGravity.display();
  11. }
  12. class RectGravity {
  13.   //Data
  14.   float x;
  15.   float y ;
  16.   float speed ;
  17.   float gravity ;
  18.   //Constructor
  19.   RectGravity() {
  20.     x = 100;
  21.     y = 0;
  22.     speed = 0;
  23.     gravity = 0.1;
  24.   }
  25.   // Function
  26.   void display() {
  27.     fill (0);
  28.     noStroke ();
  29.     rectMode (CENTER);
  30.     rect (x, y, 10, 10);
  31.     y = y + speed ;
  32.     speed = speed + gravity;
  33.     if ( y > height) {
  34.       speed = speed * -0.95;
  35.     }
  36.   }
  37. }