ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Sun Apr 21 16:10:19 2019 +0000
Revision:
14:529f798adae4
Parent:
13:10851784af9a
Child:
15:4efa04a6a376
Changed the doodler's update function so that it only affects the position of the doodler and the init function so that it also changes the velocity of the doodler. I also included the init function in the check wall collisions.

Who changed what in which revision?

UserRevisionLine numberNew 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 14:529f798adae4 7 void Doodler::init(int radius, float position_x, float position_y, double velocity_y){
el17m2h 7:0d9cee90ab0d 8 // initial position of doodler at centre
el17m2h 5:8814d6de77d0 9 _radius = radius;
el17m2h 14:529f798adae4 10 _velocity_y = (double)velocity_y;
el17m2h 14:529f798adae4 11 _position_x = position_x;
el17m2h 14:529f798adae4 12 _position_y = position_y;
el17m2h 10:e1d2289705ef 13 _gravity = 1.1; // moves down
el17m2h 10:e1d2289705ef 14 _up = -0.5; // elasticity
el17m2h 14:529f798adae4 15
el17m2h 14:529f798adae4 16 if ((double)_velocity_y > 0.00){ // no jump
el17m2h 14:529f798adae4 17 _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating
el17m2h 14:529f798adae4 18 } else if ( (double)_velocity_y == 0.00){
el17m2h 14:529f798adae4 19 _velocity_y = (double)_gravity;
el17m2h 14:529f798adae4 20 } else {
el17m2h 14:529f798adae4 21 _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
el17m2h 14:529f798adae4 22 if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
el17m2h 14:529f798adae4 23 _velocity_y = 0.00;
el17m2h 14:529f798adae4 24 }
el17m2h 14:529f798adae4 25 }
el17m2h 4:8ec314f806ae 26 }
el17m2h 4:8ec314f806ae 27
el17m2h 4:8ec314f806ae 28 void Doodler::draw(N5110 &lcd){
el17m2h 14:529f798adae4 29 lcd.drawCircle(_position_x, _position_y, _radius, FILL_BLACK);
el17m2h 4:8ec314f806ae 30 }
el17m2h 4:8ec314f806ae 31
el17m2h 14:529f798adae4 32 void Doodler::update(Direction d, float mag, double velocity_y){
el17m2h 9:5e53bca2a4c2 33 //////////////////////////// y - direction of doodler /////////////////////////////////////
el17m2h 14:529f798adae4 34 _position_x = get_position_x();
el17m2h 14:529f798adae4 35 _position_y = get_position_y();
el17m2h 14:529f798adae4 36 _velocity_y = (double)velocity_y;
el17m2h 10:e1d2289705ef 37 if ((double)_velocity_y >= 0.00){ // no jump
el17m2h 14:529f798adae4 38 _position_y += _velocity_y;
el17m2h 10:e1d2289705ef 39 } else { // jump has started until vel = 0
el17m2h 14:529f798adae4 40 _position_y += _velocity_y*5;
el17m2h 9:5e53bca2a4c2 41 }
el17m2h 8:90e789413e0b 42
el17m2h 9:5e53bca2a4c2 43 /////////////////////////// x - direction of doodler /////////////////////////////////////////
el17m2h 10:e1d2289705ef 44 _velocity_x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
el17m2h 5:8814d6de77d0 45 if (d == W){ // if direction is left
el17m2h 14:529f798adae4 46 _position_x -= _velocity_x;
el17m2h 5:8814d6de77d0 47 } else if (d == E){
el17m2h 14:529f798adae4 48 _position_x += _velocity_x;
el17m2h 8:90e789413e0b 49 }
el17m2h 9:5e53bca2a4c2 50 // checking doodler does not leave screen:
el17m2h 14:529f798adae4 51 if (_position_x > WIDTH-4.00){ // right side
el17m2h 14:529f798adae4 52 _position_x = WIDTH-4.00;
el17m2h 9:5e53bca2a4c2 53 }
el17m2h 14:529f798adae4 54 if (_position_x < _radius){ // left side
el17m2h 14:529f798adae4 55 _position_x = _radius;
el17m2h 9:5e53bca2a4c2 56 }
el17m2h 4:8ec314f806ae 57 }
el17m2h 4:8ec314f806ae 58
el17m2h 14:529f798adae4 59
el17m2h 10:e1d2289705ef 60 float Doodler::get_position_x(){
el17m2h 14:529f798adae4 61 float p_x = _position_x;
el17m2h 10:e1d2289705ef 62 return p_x;
el17m2h 10:e1d2289705ef 63 }
el17m2h 10:e1d2289705ef 64
el17m2h 10:e1d2289705ef 65 float Doodler::get_position_y(){
el17m2h 14:529f798adae4 66 float p_y = _position_y;
el17m2h 10:e1d2289705ef 67 return p_y;
el17m2h 4:8ec314f806ae 68 }
el17m2h 7:0d9cee90ab0d 69
el17m2h 10:e1d2289705ef 70 float Doodler::get_velocity_x(){
el17m2h 10:e1d2289705ef 71 float v_x = _velocity_x;
el17m2h 10:e1d2289705ef 72 return v_x;
el17m2h 10:e1d2289705ef 73 }
el17m2h 10:e1d2289705ef 74
el17m2h 10:e1d2289705ef 75 double Doodler::get_velocity_y(){
el17m2h 10:e1d2289705ef 76 double v_y = (double)_velocity_y;
el17m2h 10:e1d2289705ef 77 return (double)v_y;
el17m2h 7:0d9cee90ab0d 78 }
el17m2h 7:0d9cee90ab0d 79
el17m2h 14:529f798adae4 80 void Doodler::set_velocity(float vel_x, double vel_y){
el17m2h 14:529f798adae4 81 _velocity_x = vel_x;
el17m2h 14:529f798adae4 82 _velocity_y = (double)vel_y;
el17m2h 7:0d9cee90ab0d 83 }
el17m2h 7:0d9cee90ab0d 84
el17m2h 14:529f798adae4 85 void Doodler::set_position(float pos_x, float pos_y){
el17m2h 14:529f798adae4 86 _position_x = pos_x;
el17m2h 14:529f798adae4 87 _position_y = pos_y;
el17m2h 7:0d9cee90ab0d 88 }