ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Revision:
14:529f798adae4
Parent:
13:10851784af9a
Child:
15:4efa04a6a376
--- a/Doodler/Doodler.cpp	Thu Apr 18 14:54:51 2019 +0000
+++ b/Doodler/Doodler.cpp	Sun Apr 21 16:10:19 2019 +0000
@@ -4,62 +4,66 @@
 Doodler::~Doodler(){
 }
 
-void Doodler::init(int radius){
+void Doodler::init(int radius, float position_x, float position_y, double velocity_y){  
 // initial position of doodler at centre 
     _radius = radius;
-    _pos_x = 35; 
-    _pos_y = 35;
-    _velocity_y = 1.1;
+    _velocity_y = (double)velocity_y;
+    _position_x = position_x;
+    _position_y = position_y;
     _gravity = 1.1; // moves down
     _up = -0.5; // elasticity
+    
+    if ((double)_velocity_y > 0.00){ // no jump 
+        _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating 
+    } else if ( (double)_velocity_y == 0.00){
+        _velocity_y = (double)_gravity; 
+    } else { 
+        _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
+        if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
+            _velocity_y = 0.00;
+        }
+    }
 }
 
 void Doodler::draw(N5110 &lcd){
-    lcd.drawCircle(_pos_x, _pos_y, _radius, FILL_BLACK);
+    lcd.drawCircle(_position_x, _position_y, _radius, FILL_BLACK);
 }
 
-void Doodler::update(Direction d, float mag){
+void Doodler::update(Direction d, float mag, double velocity_y){
 //////////////////////////// y - direction of doodler /////////////////////////////////////
-    _velocity_y = (double)get_velocity_y(); 
-    
+    _position_x = get_position_x(); 
+    _position_y = get_position_y(); 
+    _velocity_y = (double)velocity_y;
     if ((double)_velocity_y >= 0.00){ // no jump 
-        _pos_y += _velocity_y; 
-        _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating 
+        _position_y += _velocity_y; 
     } else { // jump has started until vel = 0 
-        _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
-        _pos_y += _velocity_y*5;
-        
-        if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
-            _velocity_y = _gravity;
-        }
+        _position_y += _velocity_y*5;
     }
     
 /////////////////////////// x - direction of doodler /////////////////////////////////////////
     _velocity_x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
     if (d == W){ // if direction is left
-        _pos_x -= _velocity_x;
+        _position_x -= _velocity_x;
     } else if (d == E){
-        _pos_x += _velocity_x;
+        _position_x += _velocity_x;
     }
-    
     // checking doodler does not leave screen:
-    if (_pos_x > WIDTH-4.00){ // right side
-        _pos_x = WIDTH-4.00;
+    if (_position_x > WIDTH-4.00){ // right side
+        _position_x = WIDTH-4.00;
     }
-    if (_pos_x < _radius){ // left side
-        _pos_x = _radius;
+    if (_position_x < _radius){ // left side
+        _position_x = _radius;
     }
-    set_position(_pos_x, _pos_y);
-    set_velocity(_velocity_x, (double)_velocity_y);
 }
 
+
 float Doodler::get_position_x(){
-    float p_x = _pos_x; 
+    float p_x = _position_x; 
     return p_x;
 }
 
 float Doodler::get_position_y(){
-    float p_y = _pos_y;
+    float p_y = _position_y;
     return p_y;
 }
 
@@ -73,12 +77,12 @@
     return (double)v_y;
 }
 
-void Doodler::set_velocity(float v_x, double v_y){
-    _velocity_x = v_x;
-    _velocity_y = (double)v_y;
+void Doodler::set_velocity(float vel_x, double vel_y){
+    _velocity_x = vel_x;
+    _velocity_y = (double)vel_y;
 }
 
-void Doodler::set_position(float p_x, float p_y){
-    _pos_x = p_x; 
-    _pos_y = p_y;
+void Doodler::set_position(float pos_x, float pos_y){
+    _position_x = pos_x; 
+    _position_y = pos_y;
 }
\ No newline at end of file