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
Diff: GameEngine/engine.cpp
- Revision:
- 9:dc13042b09f5
- Parent:
- 8:b3738229ba85
- Child:
- 10:b939edd9b87c
--- a/GameEngine/engine.cpp Sat May 04 22:46:31 2019 +0000
+++ b/GameEngine/engine.cpp Sun May 05 23:31:07 2019 +0000
@@ -14,7 +14,35 @@
setSpeed(speed);
_screen_height = screenHeight;
- _screen_width = screenWidth;
+ _screen_width = screenWidth;
+ _car_positon = 0.0f;
+ _distance = 0.0f;
+ _curvature = 0.0f;
+
+ MapSegment seg1;
+ seg1.curvature = 0.0f;
+ seg1.meters = 100.0f;
+ track.push_back(seg1);
+
+ MapSegment seg2;
+ seg2.curvature = -1.0f;
+ seg2.meters = 200.0f;
+ track.push_back(seg2);
+
+ MapSegment seg3;
+ seg3.curvature = 1.0f;
+ seg3.meters = 200.0f;
+ track.push_back(seg3);
+
+ MapSegment seg4;
+ seg4.curvature = -1.0f;
+ seg4.meters = 200.0f;
+ track.push_back(seg4);
+
+ MapSegment seg5;
+ seg5.curvature = 1.0f;
+ seg5.meters = 200.0f;
+ track.push_back(seg5);
}
void Engine::setSpeed(int speed) {
@@ -23,15 +51,50 @@
_speed = speed;
}
-void Engine::update(Gamepad &pad, N5110 &lcd) {
+void Engine::update(Gamepad &pad, N5110 &lcd, float elapsedTime) {
clearScreen(lcd);
+ if(pad.get_direction() == N) {
+ _speed += 2.0f * elapsedTime;
+ }
+ else
+ _speed -= 1.0f * elapsedTime;
+
+ // clamp speed
+ if ( _speed < 0.0f) _speed = 0.0f;
+ if ( _speed > 1.0f) _speed = 1.0f;
+
+ _distance += (_speed * 110.0f) * elapsedTime;
+
+ // get point on track
+ float foffset = 0;
+ int ntrackSection = 0;
+
+ // find position on track
+ while( ntrackSection < track.size() && foffset < _distance){
+
+ foffset += track[ntrackSection].meters;
+ ntrackSection++;
+ }
+
+ float targetCurvature = track[ntrackSection -1].curvature;
+
+ float curvDiff = (targetCurvature - _curvature) * elapsedTime;
+ _curvature += curvDiff;
+
for (int y = 0; y < _screen_height / 2; y++) {
+
+ //_distance += n;
+
+ float fPerspective = (float)y / (_screen_height / 2.0f);
+
for (int x = 0; x < _screen_width; x++) {
- float fPointCentre = 0.5f;
- float fRoadSpace = 0.6f;
+
+
+ float fPointCentre = 0.5f + _curvature * powf((1.0f - fPerspective), 3);
+ float fRoadSpace = 0.1f + fPerspective * 0.8f;
float fSideSpace = fRoadSpace * 0.15f;
// for easier calculating the sides
@@ -44,26 +107,40 @@
int row = _screen_height / 2 + y;
+ float GrassColour = sinf(20.0f * powf(1.0f - fPerspective, 3) + _distance * 0.1f);
+ if (GrassColour > 0.0f) GrassColour = 1.0f;
+ else GrassColour = 0.0f;
+
+ float SideColour = sinf(40.0f * powf(1.0f - fPerspective, 3) + _distance * 0.1f);
+ if (SideColour > 0.0f) SideColour = 1.0f;
+ else SideColour = 0.0f;
+
if ( x >= 0 && x < leftGrass) {
- if ( y % 2 == 0 )
+ if ( GrassColour == 0.0f )
lcd.setPixel(x, row, true);
}
if ( x >= leftGrass && x < leftSide) {
- if ( x % 2 == 0 )
- lcd.setPixel(x, row, true);
+ if ( x % 2 ==0)
+ if ( SideColour == 0.0f )
+ lcd.setPixel(x, row, true);
}
if ( x >= rightSide && x < rightGrass ) {
- if ( x % 2 == 0 )
- lcd.setPixel(x, row, true);
+ if ( x % 2 ==0)
+ if ( SideColour == 0.0f )
+ lcd.setPixel(x, row, true);
}
if ( x >= rightGrass && x < _screen_width) {
- if ( y % 2 == 0 )
+ if ( GrassColour == 0.0f )
lcd.setPixel(x, row, true);
}
}
}
+ // draw car
+ int nCarPosition = _screen_width / 2 + ((int)_screen_width * _car_positon / 2.0f) - 9;
+
+ lcd.drawSprite(nCarPosition, 36, 6, 9, car1);
lcd.refresh();
- wait(200);
+
}
int Engine::car1[6*9] = {