throw balls

This commit is contained in:
wassname
2018-12-02 21:59:23 +08:00
parent e2d2a30303
commit bd5479438f
4 changed files with 38 additions and 21 deletions
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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();
}
+5 -1
View File
@@ -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
}
+30 -17
View File
@@ -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)