ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Revision:
10:e1d2289705ef
Parent:
9:5e53bca2a4c2
Child:
11:2041290b5a74
--- a/Doodler/Doodler.cpp	Tue Apr 16 19:51:12 2019 +0000
+++ b/Doodler/Doodler.cpp	Wed Apr 17 15:19:58 2019 +0000
@@ -7,65 +7,79 @@
 void Doodler::init(int radius){
 // initial position of doodler at centre 
     _radius = radius;
-    _pos.x = (70/2)+6; 
-    _pos.y = 24; 
-    _velocity.y = 1.5; // dropped down
-    _gravity = 1.5; // moves down
-    _up = -0.2;
+    _pos_x = 41; 
+    _pos_y = 24;
+    _velocity_y = 1.1;
+    _gravity = 1.1; // moves down
+    _up = -0.5; // elasticity
 }
 
 void Doodler::draw(N5110 &lcd){
-    lcd.drawCircle(_pos.x, _pos.y, _radius, FILL_BLACK);
+    lcd.drawCircle(_pos_x, _pos_y, _radius, FILL_BLACK);
 }
 
-void Doodler::update(Direction d, float mag, Vector2D current_vel){
+void Doodler::update(Direction d, float mag, float current_vel_x, double current_vel_y){
 //////////////////////////// y - direction of doodler /////////////////////////////////////
-    _current_vel = current_vel;
+    _velocity_x = current_vel_x;
+    _velocity_y = (double)current_vel_y;
     
-    if (_current_vel.y > 0 ){ // no jump 
-        _velocity.y *=  _gravity; // falls accelerating 
-    } else if(_current_vel.y < 0){ // jump has started and continues 
-        _velocity.y = -(_velocity.y*_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
-    } else if (_current_vel.y == 0) { // jumping has finished
-        _velocity.y = _gravity;
+    if ((double)_velocity_y >= 0.00){ // no jump 
+        _pos_y += _velocity_y; 
+        _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating 
+    } 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.005){ // decelerated completely
+            _velocity_y = _gravity;
+        }
     }
-    _pos.y += _velocity.y; 
     
 /////////////////////////// x - direction of doodler /////////////////////////////////////////
-    _velocity.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
+    _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;
+        _pos_x -= _velocity_x;
     } else if (d == E){
-        _pos.x += _velocity.x;
+        _pos_x += _velocity_x;
     }
     
     // checking doodler does not leave screen:
-    if (_pos.x > WIDTH-4){ // right side
-        _pos.x = WIDTH-4;
+    if (_pos_x > WIDTH-4.00){ // right side
+        _pos_x = WIDTH-4.00;
     }
-    if (_pos.x < _radius){ // left side
-        _pos.x = _radius;
+    if (_pos_x < _radius){ // left side
+        _pos_x = _radius;
     }
-    set_position(_pos);
-    set_velocity(_velocity);
+    set_position(_pos_x, _pos_y);
+    set_velocity(_velocity_x, (double)_velocity_y);
 }
 
-Vector2D Doodler::get_position(){
-    Vector2D p = {_pos.x, _pos.y}; 
-    return p;
+float Doodler::get_position_x(){
+    float p_x = _pos_x; 
+    return p_x;
+}
+
+float Doodler::get_position_y(){
+    float p_y = _pos_y;
+    return p_y;
 }
 
-Vector2D Doodler::get_velocity(){
-    Vector2D v = {_velocity.x, _velocity.y};
-    return v;
+float Doodler::get_velocity_x(){
+    float v_x = _velocity_x;
+    return v_x;
+}
+
+double Doodler::get_velocity_y(){
+    double v_y = (double)_velocity_y;
+    return (double)v_y;
 }
 
-void Doodler::set_velocity(Vector2D v){
-    _velocity.x = v.x;
-    _velocity.y = v.y;
+void Doodler::set_velocity(float v_x, double v_y){
+    _velocity_x = v_x;
+    _velocity_y = (double)v_y;
 }
 
-void Doodler::set_position(Vector2D p){
-    _pos.x = p.x; 
-    _pos.y = p.y;
+void Doodler::set_position(float p_x, float p_y){
+    _pos_x = p_x; 
+    _pos_y = p_y;
 }
\ No newline at end of file