https://github.com/chipbell4/car-physics
A fun car physics engine for Node/Browsers
https://github.com/chipbell4/car-physics
Last synced: 3 months ago
JSON representation
A fun car physics engine for Node/Browsers
- Host: GitHub
- URL: https://github.com/chipbell4/car-physics
- Owner: chipbell4
- Created: 2016-06-21T20:08:45.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-21T20:37:23.000Z (almost 9 years ago)
- Last Synced: 2024-12-26T08:42:06.806Z (4 months ago)
- Language: JavaScript
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Car Physics
A fun little car physics engine! Here's an example usage:
```javascript
var options = {
top_speed: 100, // top speed
acceleration: 200, // how fast the car accelerates
handling: 3, // how fast the car can turn
traction: 1, // how quickly the car stops drifting (tire stickiness)
friction: 0.5 // the friction of the surface, i.e. how much percentage speed is lost per second
};
var physics = new CarPhysics(options);// dt is the delta time (in seconds) between frames. Hopefully around 1 / 60...
var doFrame = function(dt) {
// update car state, based on keyboard. Assume some booleans exist...
physics.turnDirection = 0;
if(leftArrowKeyPressed) {
physics.turnDirection = -1;
} else if(rightArrowKeyPressed) {
physics.turnDirection = 1;
}physics.thrusterOn = false;
if(spaceBarPressed) {
physics.thrusterOn = true;
}// tell the physics engine to update position
physics.update(dt);// now move some sprite carSprite (I assume it exists)
carSprite.position.x = physics.x;
carSprite.position.y = physics.y;// now, set the direction of the car
directionAngle = Math.atan2(physics.vy, physics.vx);
carSprite.rotation = directionAngle;
};
```