Skip to content

Commit

Permalink
Scene is working!
Browse files Browse the repository at this point in the history
  • Loading branch information
avardy committed Jun 14, 2023
1 parent 341f35a commit b809aa7
Show file tree
Hide file tree
Showing 16 changed files with 556 additions and 253 deletions.
6 changes: 3 additions & 3 deletions python_scripts/paint_scalar_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

#width = 800
#height = 600
width = 600
height = 400
width = 1024
height = 540

cell_radius = 0#1#10
cell_width = cell_radius * 2 + 1
Expand Down Expand Up @@ -103,7 +103,7 @@ def application_function_probability(value_0_to_1):
#paint(image, value_function_top, application_function_direct)
#paint(image, value_function_top, application_function_probability)
#paint(image, value_function_centre, application_function_probability)
paint(image, value_function_centre, application_function_direct)
#paint(image, value_function_centre, application_function_direct)
#paint(image, value_function_segment, application_function_direct)

print("Writing {}".format(filename))
Expand Down
Binary file removed python_scripts/scalar_field.png
Binary file not shown.
Binary file removed src/client/dist/scalar_field.png
Binary file not shown.
13 changes: 7 additions & 6 deletions src/common/puck/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ export default class Puck {
// Create Matter.js body and attach it to world
this.body = Bodies.circle(position.x, position.y, this.radius);
// this.body.friction = 0;
this.body.frictionAir = 1;
this.body.frictionAir = 0.5;
// this.body.frictionStatic = 0;
// this.body.restitution = 0;
//Body.setDensity(this.body, 0.001);

this.body.collisionFilter = {
group: 0,
category: 0x0002,
mask: 0x0001 | 0x0002 | 0x0004,
};
// this.body.collisionFilter = {
// group: 0,
// category: 0x0002,
// mask: 0x0001 | 0x0002 | 0x0004,
// };
World.add(this.world, this.body);

// Initialize velocity according to movement goal
Expand Down
79 changes: 14 additions & 65 deletions src/common/robot/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,33 @@ export default class Robot {

// Create Matter.js body and attach it to world
let parts = [ Bodies.circle(position.x, position.y, this.radius) ]
const compoundBody = Body.create({
parts: parts
});

this.body = compoundBody;

// We'll set the main body to category 1.
this.body.collisionFilter.group = 0;
this.body.collisionFilter.category = 0x0001;
this.body.collisionFilter.mask = 0x0001 | 0x0002;

if (this.tail) {
this.tailLength = 2.7*this.radius;
this.tailLength = 1.0*this.radius;

this.tailBody = Bodies.trapezoid(
position.x - this.radius - this.tailLength/2.0,
position.y,
this.tailLength,
position.x,
position.y - this.radius - this.tailLength/2.0,
this.radius / 5,
0
this.tailLength,
-this.tailSlope
);
//Body.setAngle(this.tailBody, 0);

var revoluteConstraint = Constraint.create({
bodyA: this.body,
bodyB: this.tailBody,
length: 0,
stiffness: 0.9,
damping: 1.0,
pointA: {x:-this.radius, y:0},
pointB: {x:this.tailLength/2.0, y:0}
});

Composite.add(this.world, [this.body, this.tailBody, revoluteConstraint]);

// Tails belong to category 4, which should only collide with pucks,
// category 2. Everything else is in category 1, which collides with each
// other and with pucks.
this.tailBody.collisionFilter.group = 0; // Group is set to 0 so that we use category/mask for collision filtering.
this.tailBody.collisionFilter.category = 0x0004;
this.tailBody.collisionFilter.mask = 0x0002;
Body.rotate(this.tailBody, -Math.PI/2.0, {x: position.x, y: position.y} );
Body.setDensity(this.tailBody, 0.00001);
parts.push(this.tailBody)
}

const compoundBody = Body.create({
parts: parts
});
this.body = compoundBody;

this.body.friction = 0;
this.body.frictionAir = 0;
this.body.frictionStatic = 0;
this.body.restitution = 0;

//Body.setAngle(this.body, Math.random() * 2 * Math.PI); // Randomize orientations
Body.setAngle(this.body, 0);
Body.setAngle(this.body, Math.random() * 2 * Math.PI); // Randomize orientations

World.add(this.world, this.body);
Body.setAngularVelocity(this.body, 1);
Expand Down Expand Up @@ -253,34 +230,6 @@ export default class Robot {
if (this.actuate && typeof this.actuate === 'function') {
this.actuate(this.sensors, this.actuators, this.goal, newWaypoint);
}

/* AN ACTIVE TAIL STRAIGHTENING FORCE. THIS WORKS BUT SEEMS TO YIELD
UNSTABLE BEHAVIOUR. FALLING OUT OF LOVE WITH Matter.js.
if (this.tailBody) {
// Parameters:
const TAIL_CORRECTION_FACTOR = 0.001;
const MAX_TAIL_CORRECTION_FORCE = 0.01;
let deltaAngle = getSmallestSignedAngularDifference(this.body.angle, this.tailBody.angle);
let magnitude = TAIL_CORRECTION_FACTOR * deltaAngle;
if (magnitude > MAX_TAIL_CORRECTION_FORCE) {
magnitude = MAX_TAIL_CORRECTION_FORCE;
} else if (magnitude < -MAX_TAIL_CORRECTION_FORCE) {
magnitude = -MAX_TAIL_CORRECTION_FORCE;
}
let forceVector = {
x: magnitude * Math.cos(this.tailBody.angle + Math.PI/2),
y: magnitude * Math.sin(this.tailBody.angle + Math.PI/2)
};
let position = {
x: this.tailBody.position.x + 0.5 * this.tailLength * Math.cos(this.tailBody.angle),
y: this.tailBody.position.y + 0.5 * this.tailLength * Math.sin(this.tailBody.angle)
};
Body.applyForce(this.tailBody, position, forceVector);
}
*/
}

setVelocities({ linearVel, angularVel }) {
Expand Down
10 changes: 5 additions & 5 deletions src/common/staticObjects/staticCircle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export default class StaticCircle {
}

this.body = Bodies.circle(this.center.x, this.center.y, this.radius, { isStatic: true });
this.body.collisionFilter = {
group: 0,
category: 0x0001,
mask: 0x0001 | 0x0002,
};
// this.body.collisionFilter = {
// group: 0,
// category: 0x0001,
// mask: 0x0001 | 0x0002,
// };

if (shouldAddToWorld) {
this.addToWorld();
Expand Down
10 changes: 5 additions & 5 deletions src/common/staticObjects/staticRectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export default class StaticRectangle {
{ isStatic: true }
);

this.body.collisionFilter = {
group: 0,
category: 0x0001,
mask: 0x0001 | 0x0002,
};
// this.body.collisionFilter = {
// group: 0,
// category: 0x0001,
// mask: 0x0001 | 0x0002,
// };

if (shouldAddToWorld) {
this.addToWorld();
Expand Down
22 changes: 22 additions & 0 deletions src/common/utils/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ export function normalizeAngle(angle) {
return angle % (2 * Math.PI);
}

export function normalizeAnglePlusMinusPi(a) {
// BAD: These loops should be replaced.
while (a > Math.PI) {
a -= 2 * Math.PI;
}
while (a <= -Math.PI) {
a += 2 * Math.PI;
}
return a;
}

export function getAngularDifference(angleA, angleB) {
angleA = normalizeAnglePlusMinusPi(angleA);
angleB = normalizeAnglePlusMinusPi(angleB);
let error = Math.abs(angleA - angleB);
if (error > Math.PI) {
error -= Math.PI * 2;
error = Math.abs(error);
}
return error;
}

export function getSmallestSignedAngularDifference(a, b) {
/* Return angle between the two given angles with the smallest absolute
value. Meanwhile, the value returned will have a sign. */
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/Labyrinth/benchmarking/puckFieldValueTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PuckFieldValueTracker extends Tracker {

scene.pucks.forEach((p) => {
let point = {x: Math.round(p.position.x), y: Math.round(p.position.y)};
const fieldValue = sampleFieldAtPoint(scene.fields.heatMap.src, point)[0] / 255.0;
const fieldValue = sampleFieldAtPoint(scene.fields.travelTime.src, point)[0] / 255.0;
ssd += (fieldValue - ideal_tau) ** 2;
});

Expand Down
Binary file added src/scenes/Labyrinth/black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b809aa7

Please sign in to comment.