Ironcheese Posted August 29, 2017 Share Posted August 29, 2017 Hello everyone, i am having a hard time implementing the physics for a race like game where you can drive over "boosting" and "slowing" zones. The player can control in which lane the vehicle drives (left, mid, right). On the track there are different zones/areas. Boosting Zones should accelerate the vehicle and "Slowing" zones should really punish the player with very slow speed and a massive drag. The game is played in portrait mode with the vehicle going up. The camera is fixed on the vehicle. The lane switching is working and right now I am using tweens for that. My problem is the "natural" feeling for the acceleration / deceleration. I tried tweening the amount of pixels the y coordinate gets manipulated, I tried working with acceleration and velocity. Nothing of that "feels" right. The Zones fire events when ever the vehicle enters and leaves them. I am stuck at the physics part. What should I manipulate? Acceleration? Velocity? Drag? Friction? Inside my State: /** * handle Vehicle entering a zone! * * @param obstacle */ handleVehicleZoneEnter(event) { if (event.zone instanceof SlowingZone) { this.vehicle.speedEffect = 'SLOWED'; } if (event.zone instanceof BoostingZone) { this.vehicle.speedEffect = 'BOOSTED'; } } /** * handle vehicle zone leave * @param event */ handleVehicleZoneLeave(event) { if (event.zone instanceof SlowingZone) { this.vehicle.speedEffect = 'NONE'; } if (event.zone instanceof BoostingZone) { this.vehicle.speedEffect = 'NONE'; } } Inside my vehicle Object: // This gets called on every frame as long as the vehicle hasnt reached the end of the track adjustSpeed() { let minSpeed = 30; let normalSpeed = 500; let maxSpeed = 3000; switch (this.speedEffect) { case 'SLOWED' : this.body.drag.y = 500; this.body.friction.y = 500; break; case 'BOOSTED' : this.body.acceleration.y = -500; break; default : this.body.drag.y = 100; this.body.friction.y = 100; break; } } Thanks. Link to comment Share on other sites More sharing options...
samme Posted August 29, 2017 Share Posted August 29, 2017 It will work with drag and acceleration (remove friction). I think in the default case you would set drag.y = 0 and apply a small acceleration up or down (if necessary) to return to normal velocity. Another approach is to tween the velocity when the car enters or leaves the zone: function boost () { this.game.add.tween(this.body.velocity) .to({y: -maxSpeed}, DURATION, EASE, true); } function slow () { this.game.add.tween(this.body.velocity) .to({y: -minSpeed}, DURATION, EASE, true); } function normal () { this.game.add.tween(this.body.velocity) .to({y: -normal}, DURATION, EASE, true); } It's precise and you can choose the duration and easing function. Ironcheese 1 Link to comment Share on other sites More sharing options...
Ironcheese Posted August 29, 2017 Author Share Posted August 29, 2017 Thanks samme, I will try this tomorrow. Link to comment Share on other sites More sharing options...
Recommended Posts