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@9:5e53bca2a4c2, 2019-04-16 (annotated)
- Committer:
- el17m2h
- Date:
- Tue Apr 16 19:51:12 2019 +0000
- Revision:
- 9:5e53bca2a4c2
- Parent:
- 8:90e789413e0b
- Child:
- 10:e1d2289705ef
Changed the values of the positions and velocities to vectors so that they are easier to manipulate. I made use of the float absolute function (fabs) function to make the ball decelerate to a zero velocity while going upwards.
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 | 9:5e53bca2a4c2 | 10 | _pos.x = (70/2)+6; |
el17m2h | 9:5e53bca2a4c2 | 11 | _pos.y = 24; |
el17m2h | 8:90e789413e0b | 12 | _velocity.y = 1.5; // dropped down |
el17m2h | 8:90e789413e0b | 13 | _gravity = 1.5; // moves down |
el17m2h | 9:5e53bca2a4c2 | 14 | _up = -0.2; |
el17m2h | 4:8ec314f806ae | 15 | } |
el17m2h | 4:8ec314f806ae | 16 | |
el17m2h | 4:8ec314f806ae | 17 | void Doodler::draw(N5110 &lcd){ |
el17m2h | 9:5e53bca2a4c2 | 18 | lcd.drawCircle(_pos.x, _pos.y, _radius, FILL_BLACK); |
el17m2h | 4:8ec314f806ae | 19 | } |
el17m2h | 4:8ec314f806ae | 20 | |
el17m2h | 9:5e53bca2a4c2 | 21 | void Doodler::update(Direction d, float mag, Vector2D current_vel){ |
el17m2h | 9:5e53bca2a4c2 | 22 | //////////////////////////// y - direction of doodler ///////////////////////////////////// |
el17m2h | 8:90e789413e0b | 23 | _current_vel = current_vel; |
el17m2h | 5:8814d6de77d0 | 24 | |
el17m2h | 9:5e53bca2a4c2 | 25 | if (_current_vel.y > 0 ){ // no jump |
el17m2h | 9:5e53bca2a4c2 | 26 | _velocity.y *= _gravity; // falls accelerating |
el17m2h | 8:90e789413e0b | 27 | } else if(_current_vel.y < 0){ // jump has started and continues |
el17m2h | 9:5e53bca2a4c2 | 28 | _velocity.y = -(_velocity.y*_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0) |
el17m2h | 9:5e53bca2a4c2 | 29 | } else if (_current_vel.y == 0) { // jumping has finished |
el17m2h | 9:5e53bca2a4c2 | 30 | _velocity.y = _gravity; |
el17m2h | 9:5e53bca2a4c2 | 31 | } |
el17m2h | 9:5e53bca2a4c2 | 32 | _pos.y += _velocity.y; |
el17m2h | 8:90e789413e0b | 33 | |
el17m2h | 9:5e53bca2a4c2 | 34 | /////////////////////////// x - direction of doodler ///////////////////////////////////////// |
el17m2h | 9:5e53bca2a4c2 | 35 | _velocity.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value) |
el17m2h | 5:8814d6de77d0 | 36 | if (d == W){ // if direction is left |
el17m2h | 9:5e53bca2a4c2 | 37 | _pos.x -= _velocity.x; |
el17m2h | 5:8814d6de77d0 | 38 | } else if (d == E){ |
el17m2h | 9:5e53bca2a4c2 | 39 | _pos.x += _velocity.x; |
el17m2h | 8:90e789413e0b | 40 | } |
el17m2h | 7:0d9cee90ab0d | 41 | |
el17m2h | 9:5e53bca2a4c2 | 42 | // checking doodler does not leave screen: |
el17m2h | 9:5e53bca2a4c2 | 43 | if (_pos.x > WIDTH-4){ // right side |
el17m2h | 9:5e53bca2a4c2 | 44 | _pos.x = WIDTH-4; |
el17m2h | 9:5e53bca2a4c2 | 45 | } |
el17m2h | 9:5e53bca2a4c2 | 46 | if (_pos.x < _radius){ // left side |
el17m2h | 9:5e53bca2a4c2 | 47 | _pos.x = _radius; |
el17m2h | 9:5e53bca2a4c2 | 48 | } |
el17m2h | 9:5e53bca2a4c2 | 49 | set_position(_pos); |
el17m2h | 9:5e53bca2a4c2 | 50 | set_velocity(_velocity); |
el17m2h | 4:8ec314f806ae | 51 | } |
el17m2h | 4:8ec314f806ae | 52 | |
el17m2h | 7:0d9cee90ab0d | 53 | Vector2D Doodler::get_position(){ |
el17m2h | 9:5e53bca2a4c2 | 54 | Vector2D p = {_pos.x, _pos.y}; |
el17m2h | 4:8ec314f806ae | 55 | return p; |
el17m2h | 4:8ec314f806ae | 56 | } |
el17m2h | 7:0d9cee90ab0d | 57 | |
el17m2h | 7:0d9cee90ab0d | 58 | Vector2D Doodler::get_velocity(){ |
el17m2h | 7:0d9cee90ab0d | 59 | Vector2D v = {_velocity.x, _velocity.y}; |
el17m2h | 7:0d9cee90ab0d | 60 | return v; |
el17m2h | 7:0d9cee90ab0d | 61 | } |
el17m2h | 7:0d9cee90ab0d | 62 | |
el17m2h | 7:0d9cee90ab0d | 63 | void Doodler::set_velocity(Vector2D v){ |
el17m2h | 7:0d9cee90ab0d | 64 | _velocity.x = v.x; |
el17m2h | 7:0d9cee90ab0d | 65 | _velocity.y = v.y; |
el17m2h | 7:0d9cee90ab0d | 66 | } |
el17m2h | 7:0d9cee90ab0d | 67 | |
el17m2h | 7:0d9cee90ab0d | 68 | void Doodler::set_position(Vector2D p){ |
el17m2h | 9:5e53bca2a4c2 | 69 | _pos.x = p.x; |
el17m2h | 9:5e53bca2a4c2 | 70 | _pos.y = p.y; |
el17m2h | 7:0d9cee90ab0d | 71 | } |