Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on angular forces and collisions
- Loading branch information
Showing
13 changed files
with
701 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
## Vector math | ||
|
||
### Dot product and perp dot product | ||
|
||
Dot product: v1.mag * v2.mag * cos(theta) | ||
v1.x*v2.x + v1.y*v2.y | ||
Perp dot product: v1.mag * v2.mag * sin(theta) | ||
-v1.y*v2.x + v1.x*v2.y | ||
|
||
## Resources consulted for this project (in order of viewing) | ||
|
||
* http://gafferongames.com/game-physics/fix-your-timestep | ||
* http://gafferongames.com/game-physics/integration-basics | ||
* http://gafferongames.com/game-physics/spring-physics/ | ||
* http://chrishecker.com/Rigid_Body_Dynamics | ||
* http://www.youtube.com/watch?v=LbKKzMag5Rc | ||
* http://gamedev.stackexchange.com/questions/25838/when-to-detect-collisions-in-game-loop | ||
* http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/ | ||
* http://www.wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/ | ||
* http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/ | ||
* http://www.codezealot.org/archives/88 | ||
* http://www.codezealot.org/archives/153 | ||
* http://research.ncl.ac.uk/game/mastersdegree/gametechnologies/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
'use strict'; | ||
|
||
(function() { | ||
var Projectile, canvas, canvasObjects | ||
|
||
Projectile = P(CanvasObject, function (proto, uber, klass) { | ||
klass.maxSpeed = 5 | ||
return { | ||
setOptions: function (opts) { | ||
uber.setOptions.apply(this, arguments) | ||
this.vel = Vec2(3,0) | ||
this.pos = Vec2(200,200) | ||
}, | ||
|
||
calculateForces: function (nextState, t) { | ||
var values = {}, | ||
linearForce, | ||
angularForce, | ||
forceAmount = 0.3, | ||
forceDampening = 0.1, | ||
torqueAmount = 0.3, | ||
torqueDampening = 0.1 | ||
|
||
linearForce = Vec2() | ||
if (keyboard.keyIsPressed('UP')) { | ||
linearForce += forceAmount | ||
} | ||
// apply viscous damper force (from F = kx - cv) | ||
// XXX: don't we want to damp velocity and not force? | ||
linearForce -= Vec2.mul(nextState.velocity, forceDampening) | ||
if (linearForce < 0.001) { | ||
linearForce = 0 | ||
} | ||
|
||
angularForce = 0 | ||
if (keyboard.keyIsPressed('LEFT')) { | ||
angularForce += torqueAmount | ||
} else if (keyboard.keyIsPressed('RIGHT')) { | ||
angularForce -= torqueAmount | ||
} | ||
// apply viscous damper force (from F = kx - cv) | ||
// XXX: don't we want to damp velocity and not force? | ||
angularForce -= Vec2.mul(nextState.angularVelocity, torqueDampening) | ||
if (angularForce < 0.001) { | ||
angularForce = 0 | ||
} | ||
|
||
values.torque = angularForce | ||
values.force = Vec2.rotate(Vec2(linearForce, 0), angularForce) | ||
|
||
return values | ||
}, | ||
|
||
draw: function () { | ||
var pos, ori, color | ||
pos = this.interpState.position | ||
ori = this.interpState.orientation | ||
color = (this.index == 0) ? "red" : "black" | ||
this.canvas.triangle(pos[0], pos[1], this.width, this.height, { | ||
rotate: ori, | ||
fill: color | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
//--- | ||
|
||
canvas = Canvas("#wrapper", { | ||
width: 1000, | ||
height: 400 | ||
}) | ||
canvasObjects = canvas.buildObjectCollection(CanvasObjectCollection) | ||
canvasObjects.addObject(Projectile, { | ||
width: 8, | ||
height: 6, | ||
position: Vec2(canvas.height - 6), | ||
orientation: (Math.PI / 2), | ||
gravity: false | ||
}) | ||
|
||
window.canvas = canvas | ||
})() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.