Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: SuperSkunk/Box2DExperiments
base: 73379bd1aa
...
head fork: SuperSkunk/Box2DExperiments
compare: 2fa45ee433
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
15 Snowball/brunch/app/assets/index.html
@@ -13,6 +13,12 @@
<label for="world-debug">Mode DEBUG</label><br />
<input type="checkbox" id="world-debug" /><br />
+ <label for="world-iterations-velocity">Solver iterations velocity: <span id="world-iterations-velocity-value"></span></label><br />
+ <input type="range" id="world-iterations-velocity" /><br />
+
+ <label for="world-iterations-position">Solver iterations position: <span id="world-iterations-position-value"></span></label><br />
+ <input type="range" id="world-iterations-position" /><br />
+
<label for="world-gravity-x">World gravity horizontal: <span id="world-gravity-x-value"></span></label><br />
<input type="range" id="world-gravity-x" /><br />
@@ -39,6 +45,12 @@
<label for="slope-angle-maximum">Maximum slope angle: <span id="slope-angle-maximum-value"></span></label><br />
<input type="range" id="slope-angle-maximum" /><br />
+ <label for="slope-edging">Slope edging: <span id="slope-edging-value"></span></label><br />
+ <input type="range" id="slope-edging" /><br />
+
+ <label for="slope-radius">Slope radius: <span id="slope-radius-value"></span></label><br />
+ <input type="range" id="slope-radius" /><br />
+
<!-- slope physics -->
<label for="slope-friction">Slope friction: <span id="slope-friction-value"></span></label><br />
@@ -55,6 +67,9 @@
<label for="snowball-radius">Snowball radius: <span id="snowball-radius-value"></span></label><br />
<input type="range" id="snowball-radius" /><br />
+ <label for="snowball-bullet">Continuous collision detection (CCD - bullet)</label><br />
+ <input type="checkbox" id="snowball-bullet" /><br />
+
<!-- snowball physics -->
<label for="snowball-friction">Snowball friction: <span id="snowball-friction-value"></span></label><br />
View
4 Snowball/brunch/app/configs/mountain_config.coffee
@@ -6,6 +6,10 @@ MountainConfig =
angle:
minimum: -20
maximum: 5
+ # set to 0 to disable edging
+ edging: 0.01
+ radius: 0.4
+
physics:
density : 1
friction : .2 # how much the mountain is slidy
View
2  Snowball/brunch/app/configs/snowball_config.coffee
@@ -1,5 +1,7 @@
SnowballConfig =
radius: 1 # in meters
+ # enable continuous collision detection
+ bullet: yes
physics:
density : 1
friction : .1 # how much the can slide
View
19 Snowball/brunch/app/configs/world_config.coffee
@@ -7,6 +7,25 @@ WorldConfig =
minimum: 10
offsetBetweenScales: .001 # maximum step between scales
step: .5
+
+ # There are two phases in the constraint solver:
+ # a velocity phase and a position phase.
+ # In the velocity phase the solver computes the impulses necessary
+ # for the bodies to move correctly.
+ # In the position phase the solver adjusts the positions of
+ # the bodies to reduce overlap and joint detachment.
+ # Each phase has its own iteration count.
+ # In addition, the position phase may exit iterations early if the errors are small.
+ # The suggested iteration count for Box2D is 8 for velocity and 3 for position.
+ # You can tune this number to your liking, just keep in mind that
+ # this has a trade-off between speed and accuracy.
+ # Using fewer iterations increases performance but accuracy suffers.
+ # Likewise, using more iterations decreases performance but improves the quality of
+ # your simulation.
+ iterations:
+ velocity: 8
+ position: 3
+
physics:
gravity:
# normal earth gravity, 9.8 m/s/s straight down!
View
50 Snowball/brunch/app/entities/mountain.coffee
@@ -63,7 +63,8 @@ class Mountain
# generate curve
curvedSlope = @getCurve()
- @lastPoint ?= [-3,-2]
+
+ @lastPoint ?= [-3,curvedSlope[0][1]]
# add the curve in the world
for point, index in curvedSlope
@@ -127,19 +128,50 @@ class Mountain
addSegmentToWorld: (segment) ->
# create a polygon from the segment because box2d can't handle segment
- # respect the order
+
+ # get segment center
+ position = [(segment[1][0] + segment[0][0]) / 2, (segment[1][1] + segment[0][1]) / 2]
+
+ xs = segment[1][0] - segment[0][0]
+ ys = segment[1][1] - segment[0][1]
+
+ # get segment angle
+ xa = segment[0][0]
+ xb = segment[1][0]
+ ya = segment[0][1]
+ yb = segment[1][1]
+ angle = Math.atan2((yb - ya), (xb - xa))
+
+ # create the box
+ width = Math.sqrt(xs * xs + ys * ys)
+ halfWidth = width / 2
+ halfHeight = @config.slope.radius / 2
+
polygon = [
# bottom left
- [segment[0][0], segment[0][1] + 1]
+ [-halfWidth, halfHeight]
# top left
- segment[0],
+ [-halfWidth, -halfHeight]
# top right
- segment[1],
+ [halfWidth, -halfHeight]
# bottom right
- [segment[1][0], segment[1][1] + 1]
+ [halfWidth, halfHeight]
]
- @addPolygonToWorld polygon
+ # add edging
+ if @config.slope.edging > 0
+ edging = @config.slope.edging
+ # ______
+ # / \
+ # / \
+
+ # create top left
+ polygon.splice 1, 0, [-halfWidth, -halfHeight + edging]
+ polygon[2][0] += edging
+ polygon.splice 3, 0, [halfWidth - edging, -halfHeight]
+ polygon[4][1] += edging
+
+ @addPolygonToWorld2 polygon, position, angle
###
* move the moutain
@@ -191,6 +223,10 @@ class Mountain
body = Box2DHelper.addPolygon polygon, @config.physics, @world
@slices.push body
+ addPolygonToWorld2: (polygon, position, angle) ->
+ body = Box2DHelper.addPolygon2 polygon, position, angle, @config.physics, @world
+ @slices.push body
+
draw: (context) ->
# Set drawing style
context.lineWidth = 2
View
5 Snowball/brunch/app/entities/snowball.coffee
@@ -18,6 +18,9 @@ class SnowBall
updateRadius: ->
@body.UpdateRadius @config.radius
+ updateBulletMode: ->
+ @body.SetBullet @config.bullet
+
updatePhysics: ->
@body.UpdatePhysics @config.physics
@@ -43,6 +46,8 @@ class SnowBall
@world
)
+ @body.SetBullet @config.buller
+
###
* get the position of the very bottom of the ball
###
View
10 Snowball/brunch/app/entities/world.coffee
@@ -9,7 +9,6 @@ class World
allowSleepingBodies: yes
updatePhysics: ->
-
currentGravity = @world.GetGravity()
gravity = @config.physics.gravity
if currentGravity.x isnt gravity.x or currentGravity.y isnt gravity.y
@@ -25,9 +24,12 @@ class World
update: (callback) ->
frameRate = 1 / 60
- velocityIterations = 10
- positionIterations = 10
- @world.Step(frameRate, velocityIterations, positionIterations)
+ @world.Step(
+ frameRate,
+ @config.iterations.velocity,
+ @config.iterations.position
+ )
+ # clear applied force to bodies
@world.ClearForces()
callback()
View
3  Snowball/brunch/app/game.coffee
@@ -72,8 +72,11 @@ class Game
update: ->
# update physics
@world.updatePhysics()
+
@snowball.updatePhysics()
@snowball.updateRadius()
+ @snowball.updateBulletMode()
+
@mountain.updatePhysics()
# handle camera; we need to be always at the same position from the snowball
View
28 Snowball/brunch/app/game_debug.coffee
@@ -10,6 +10,18 @@ class GameDebug
name: 'debug'
object: WorldConfig
checkbox: yes
+ 'world-iterations-velocity':
+ name: 'velocity'
+ object: WorldConfig.iterations
+ minimum: 0
+ maximum: 100
+ step: 1
+ 'world-iterations-position':
+ name: 'position'
+ object: WorldConfig.iterations
+ minimum: 0
+ maximum: 100
+ step: 1
'world-gravity-x':
name: 'x'
object: WorldConfig.physics.gravity
@@ -59,6 +71,18 @@ class GameDebug
minimum: -90
maximum: 90
step: .1
+ 'slope-edging':
+ name: 'edging'
+ object: MountainConfig.slope
+ minimum: 0
+ maximum: 0.2
+ step: .01
+ 'slope-radius':
+ name: 'radius'
+ object: MountainConfig.slope
+ minimum: 0.1
+ maximum: 2
+ step: .1
# slope physics
'slope-friction':
name: 'friction'
@@ -85,6 +109,10 @@ class GameDebug
minimum: 1
maximum: 20
step: 1
+ 'snowball-bullet':
+ name: 'bullet'
+ object: SnowballConfig
+ checkbox: yes
# snowball physics
'snowball-friction':
name: 'friction'
View
11 Snowball/brunch/app/helpers/box2d_helper.coffee
@@ -36,18 +36,15 @@ class Box2DHelper
@setPolygonToVectors: (polygon) ->
@newPoint point for point in polygon
- @addPolygon: (polygon, physics, world) ->
+ @addPolygon: (polygon, position, angle, physics, world) ->
bodyDef = new b2BodyDef
bodyDef.type = b2BodyDef.b2_staticBody
+ bodyDef.position.Set position...
+ bodyDef.angle = angle
vectorsPolygon = @setPolygonToVectors polygon
- centroid = PolygonHelper.getCentroidOf vectorsPolygon
- bodyDef.position.Set centroid.x, centroid.y
- # set the points relative to the centroid
- @subtract vector, centroid for vector in vectorsPolygon
-
- shape = new b2PolygonShape vectorsPolygon
+ shape = new b2PolygonShape
shape.SetAsVector vectorsPolygon, vectorsPolygon.length
fixDef = new b2FixtureDef

No commit comments for this range

Something went wrong with that request. Please try again.