Simple gravity is not working... OOP exercise
in
Programming Questions
•
5 months ago
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 ? )
- RectGravity myRectGravity;
- void setup(){
- size (200,200);
- smooth ();
- myRectGravity = new RectGravity();
- }
- void draw(){
- myRectGravity.display();
- }
- class RectGravity{
- //Data
- float x;
- float y ;
- float speed ;
- float gravity ;
- //Constructor
- RectGravity(){
- x = 100;
- y = 0;
- speed = 0;
- gravity = 0.1;
- }
- // Function
- void display(){
- fill (0);
- noStroke ();
- rectMode (CENTER);
- rect (x,y,10,10);
- y = y + speed ;
- speed = speed + gravity;
- if ( y < height){
- speed = speed * -0.95;}
- }
- }
1