Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Doodler/Doodler.cpp@10:e1d2289705ef, 2019-04-17 (annotated)
- Committer:
- el17m2h
- Date:
- Wed Apr 17 15:19:58 2019 +0000
- Revision:
- 10:e1d2289705ef
- Parent:
- 9:5e53bca2a4c2
- Child:
- 11:2041290b5a74
Managed to fix the doodler's bounce by changing the velocity to a double instead of a int type. I also added a get_velocity_y() function to the function checking the floor's collisions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17m2h | 4:8ec314f806ae | 1 | #include "Doodler.h" |
el17m2h | 4:8ec314f806ae | 2 | Doodler::Doodler(){ |
el17m2h | 4:8ec314f806ae | 3 | } |
el17m2h | 4:8ec314f806ae | 4 | Doodler::~Doodler(){ |
el17m2h | 4:8ec314f806ae | 5 | } |
el17m2h | 4:8ec314f806ae | 6 | |
el17m2h | 5:8814d6de77d0 | 7 | void Doodler::init(int radius){ |
el17m2h | 7:0d9cee90ab0d | 8 | // initial position of doodler at centre |
el17m2h | 5:8814d6de77d0 | 9 | _radius = radius; |
el17m2h | 10:e1d2289705ef | 10 | _pos_x = 41; |
el17m2h | 10:e1d2289705ef | 11 | _pos_y = 24; |
el17m2h | 10:e1d2289705ef | 12 | _velocity_y = 1.1; |
el17m2h | 10:e1d2289705ef | 13 | _gravity = 1.1; // moves down |
el17m2h | 10:e1d2289705ef | 14 | _up = -0.5; // elasticity |
el17m2h | 4:8ec314f806ae | 15 | } |
el17m2h | 4:8ec314f806ae | 16 | |
el17m2h | 4:8ec314f806ae | 17 | void Doodler::draw(N5110 &lcd){ |
el17m2h | 10:e1d2289705ef | 18 | lcd.drawCircle(_pos_x, _pos_y, _radius, FILL_BLACK); |
el17m2h | 4:8ec314f806ae | 19 | } |
el17m2h | 4:8ec314f806ae | 20 | |
el17m2h | 10:e1d2289705ef | 21 | void Doodler::update(Direction d, float mag, float current_vel_x, double current_vel_y){ |
el17m2h | 9:5e53bca2a4c2 | 22 | //////////////////////////// y - direction of doodler ///////////////////////////////////// |
el17m2h | 10:e1d2289705ef | 23 | _velocity_x = current_vel_x; |
el17m2h | 10:e1d2289705ef | 24 | _velocity_y = (double)current_vel_y; |
el17m2h | 5:8814d6de77d0 | 25 | |
el17m2h | 10:e1d2289705ef | 26 | if ((double)_velocity_y >= 0.00){ // no jump |
el17m2h | 10:e1d2289705ef | 27 | _pos_y += _velocity_y; |
el17m2h | 10:e1d2289705ef | 28 | _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating |
el17m2h | 10:e1d2289705ef | 29 | } else { // jump has started until vel = 0 |
el17m2h | 10:e1d2289705ef | 30 | _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0) |
el17m2h | 10:e1d2289705ef | 31 | _pos_y += _velocity_y*5; |
el17m2h | 10:e1d2289705ef | 32 | |
el17m2h | 10:e1d2289705ef | 33 | if (fabs((double)_velocity_y) < 0.005){ // decelerated completely |
el17m2h | 10:e1d2289705ef | 34 | _velocity_y = _gravity; |
el17m2h | 10:e1d2289705ef | 35 | } |
el17m2h | 9:5e53bca2a4c2 | 36 | } |
el17m2h | 8:90e789413e0b | 37 | |
el17m2h | 9:5e53bca2a4c2 | 38 | /////////////////////////// x - direction of doodler ///////////////////////////////////////// |
el17m2h | 10:e1d2289705ef | 39 | _velocity_x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value) |
el17m2h | 5:8814d6de77d0 | 40 | if (d == W){ // if direction is left |
el17m2h | 10:e1d2289705ef | 41 | _pos_x -= _velocity_x; |
el17m2h | 5:8814d6de77d0 | 42 | } else if (d == E){ |
el17m2h | 10:e1d2289705ef | 43 | _pos_x += _velocity_x; |
el17m2h | 8:90e789413e0b | 44 | } |
el17m2h | 7:0d9cee90ab0d | 45 | |
el17m2h | 9:5e53bca2a4c2 | 46 | // checking doodler does not leave screen: |
el17m2h | 10:e1d2289705ef | 47 | if (_pos_x > WIDTH-4.00){ // right side |
el17m2h | 10:e1d2289705ef | 48 | _pos_x = WIDTH-4.00; |
el17m2h | 9:5e53bca2a4c2 | 49 | } |
el17m2h | 10:e1d2289705ef | 50 | if (_pos_x < _radius){ // left side |
el17m2h | 10:e1d2289705ef | 51 | _pos_x = _radius; |
el17m2h | 9:5e53bca2a4c2 | 52 | } |
el17m2h | 10:e1d2289705ef | 53 | set_position(_pos_x, _pos_y); |
el17m2h | 10:e1d2289705ef | 54 | set_velocity(_velocity_x, (double)_velocity_y); |
el17m2h | 4:8ec314f806ae | 55 | } |
el17m2h | 4:8ec314f806ae | 56 | |
el17m2h | 10:e1d2289705ef | 57 | float Doodler::get_position_x(){ |
el17m2h | 10:e1d2289705ef | 58 | float p_x = _pos_x; |
el17m2h | 10:e1d2289705ef | 59 | return p_x; |
el17m2h | 10:e1d2289705ef | 60 | } |
el17m2h | 10:e1d2289705ef | 61 | |
el17m2h | 10:e1d2289705ef | 62 | float Doodler::get_position_y(){ |
el17m2h | 10:e1d2289705ef | 63 | float p_y = _pos_y; |
el17m2h | 10:e1d2289705ef | 64 | return p_y; |
el17m2h | 4:8ec314f806ae | 65 | } |
el17m2h | 7:0d9cee90ab0d | 66 | |
el17m2h | 10:e1d2289705ef | 67 | float Doodler::get_velocity_x(){ |
el17m2h | 10:e1d2289705ef | 68 | float v_x = _velocity_x; |
el17m2h | 10:e1d2289705ef | 69 | return v_x; |
el17m2h | 10:e1d2289705ef | 70 | } |
el17m2h | 10:e1d2289705ef | 71 | |
el17m2h | 10:e1d2289705ef | 72 | double Doodler::get_velocity_y(){ |
el17m2h | 10:e1d2289705ef | 73 | double v_y = (double)_velocity_y; |
el17m2h | 10:e1d2289705ef | 74 | return (double)v_y; |
el17m2h | 7:0d9cee90ab0d | 75 | } |
el17m2h | 7:0d9cee90ab0d | 76 | |
el17m2h | 10:e1d2289705ef | 77 | void Doodler::set_velocity(float v_x, double v_y){ |
el17m2h | 10:e1d2289705ef | 78 | _velocity_x = v_x; |
el17m2h | 10:e1d2289705ef | 79 | _velocity_y = (double)v_y; |
el17m2h | 7:0d9cee90ab0d | 80 | } |
el17m2h | 7:0d9cee90ab0d | 81 | |
el17m2h | 10:e1d2289705ef | 82 | void Doodler::set_position(float p_x, float p_y){ |
el17m2h | 10:e1d2289705ef | 83 | _pos_x = p_x; |
el17m2h | 10:e1d2289705ef | 84 | _pos_y = p_y; |
el17m2h | 7:0d9cee90ab0d | 85 | } |