From bd5479438f787985e0c5080b5cff14af39c76132 Mon Sep 17 00:00:00 2001 From: wassname Date: Sun, 2 Dec 2018 21:59:23 +0800 Subject: [PATCH] throw balls --- src/js/floor.js | 4 ++-- src/js/renderer.js | 2 +- src/js/utils.js | 6 +++++- src/js/walker.js | 47 +++++++++++++++++++++++++++++----------------- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/js/floor.js b/src/js/floor.js index b70e8a0..29eda31 100644 --- a/src/js/floor.js +++ b/src/js/floor.js @@ -10,7 +10,7 @@ function createFloor(world, max_floor_tiles) { // start with flat floor var edges = [ - new b2.Vec2(-3.5, 4), // back wall + new b2.Vec2(-3.5, 18), // back wall new b2.Vec2(-3.5, -0.16), new b2.Vec2(2.5, -0.16) ]; @@ -23,7 +23,7 @@ function createFloor(world, max_floor_tiles) { edges[edges.length - 1].y + (ratio * Math.random() - ratio / 2))); // edges.push(new b2.Vec2(edges[edges.length-1].x + 1,-0.16)); } - edges.push(new b2.Vec2(edges[edges.length - 1].x, edges[edges.length - 1].y + 8)); // front wall + edges.push(new b2.Vec2(edges[edges.length - 1].x, edges[edges.length - 1].y + 18)); // front wall fix_def.shape.CreateChain(edges, edges.length); body.CreateFixture(fix_def); return body; diff --git a/src/js/renderer.js b/src/js/renderer.js index 6fd52fb..dc485ac 100644 --- a/src/js/renderer.js +++ b/src/js/renderer.js @@ -64,7 +64,7 @@ class Renderer { let radius = shape.m_radius // this.ctx.moveTo(p0.x, p0.y); this.ctx.arc(p0.x, p0.y, radius, 0, Math.PI*2) - this.ctx.fill(); + // this.ctx.fill(); this.ctx.stroke(); } diff --git a/src/js/utils.js b/src/js/utils.js index 5c7ffa9..4c5b3d4 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -5,6 +5,9 @@ function deg2rad(deg) { return deg / 180 * Math.PI } +function clamp(x, a, b) { + return Math.max(a, Math.min(b, x)) +} class MovingAverage { constructor(N) { @@ -37,5 +40,6 @@ module.exports = { randf, randi, MovingAverage, - mean + mean, + clamp } diff --git a/src/js/walker.js b/src/js/walker.js index 8eca356..d4a951d 100644 --- a/src/js/walker.js +++ b/src/js/walker.js @@ -3,7 +3,8 @@ const b2 = require('../vendor/jsbox2d') const { randf, - deg2rad + deg2rad, + clamp } = require('./utils.js') const { Renderer @@ -480,7 +481,7 @@ class Walker { this.joints.push(j); } - addBall(x, y, r) { + addBall(x, y, r) { var bodyDef = new b2.BodyDef() bodyDef.type = b2.Body.b2_dynamicBody bodyDef.position.x = x @@ -490,13 +491,13 @@ class Walker { // Create a circle shape and set its radius to 6 var circle = new b2.CircleShape(); // circle.setRadius(6) - circle.m_radius=r + circle.m_radius = r // Create a fixture definition to apply our shape to var fixtureDef = new b2.FixtureDef(); fixtureDef.shape = circle; - fixtureDef.density = 0.5; + fixtureDef.density = 100; fixtureDef.friction = 0.4; fixtureDef.restitution = 0.6; // Make it bounce a little bit @@ -588,8 +589,8 @@ class Walker { @action: (Integer) The action to take (can be null if no action) */ - if (Math.random() < 0.05) { - this.addBallOnWalker() + if (Math.random() < 0.005) { + this.addBallOnWalker() } // repeat actions @@ -685,7 +686,18 @@ class Walker { addBallOnWalker() { let p = this.torso.upper_torso.GetPosition() - this.addBall(p.x, randf(1, 4), randf(0.03,0.3)) + this.addBall(p.x + randf(-2, 2), randf(1, 4), randf(0.03, 0.3)) + } + + chuckBalls() { + for (const ball of this.balls) { + let pb = ball.GetPosition() + let dx = (this.torso.upper_torso.GetPosition().x-pb.x)*2 + let dy = (this.torso.upper_torso.GetPosition().y - pb.y + 1) * 3 + dx = clamp(dx, -6, 6) + dy = clamp(dy, -6, 6) + ball.SetLinearVelocity(new b2.Vec2(dx,dy)) + } } @@ -695,12 +707,11 @@ class Walker { if (this.bodies) this.destroy(); this.build(); this.initGrip() - + this.episodeSteps = 0 this.randomise(0.5) - this.addBallOnWalker() this.addBallOnWalker() this.addBallOnWalker() @@ -719,15 +730,17 @@ class Walker { // }) + this.chuckBalls() - // console.log('shuffle not implemented') - var b = this.torso.upper_torso - let angle = b.GetAngle() - let pos = b.GetPosition() - // pos.x += randf(-1, 1) - // pos.y += randf(0, 2) - angle += Math.PI - b.SetTransform(pos, angle) + + // // console.log('shuffle not implemented') + // var b = this.torso.upper_torso + // let angle = b.GetAngle() + // let pos = b.GetPosition() + // // pos.x += randf(-1, 1) + // // pos.y += randf(0, 2) + // angle += Math.PI + // b.SetTransform(pos, angle) // let dx = randf(-1, 1) // let dy = randf(-1, 1)