Skip to content

Commit

Permalink
box2d: disable physics and simulation rate blocks (#1190)
Browse files Browse the repository at this point in the history
Resolves #1171, #925
  • Loading branch information
CST1229 authored Dec 21, 2023
1 parent aefaa88 commit 11ca543
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/box2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ step simulation :: #0FBD8C

Move forward in time by one step. Run this in a loop to keep the physics going.


```scratch
set simulation rate to (30)/s :: #0FBD8C
```

Set how much simulation steps is considered one second. Usually this should be project's framerate, but can also be used to slow down or speed up time.

You can get the current simulation rate with the (simulation rate) reporter.

## Sprites

Manipulate individual sprites.
Expand All @@ -48,6 +57,14 @@ Precision mode will make the sprite work extra hard to make sure it doesn't over

---

```scratch
disable physics for this sprite :: #0FBD8C
```

Makes physics no longer apply to this sprite.

---

```scratch
go to x: [0] y: [0] [in world v] :: #0FBD8C
```
Expand Down
66 changes: 65 additions & 1 deletion extensions/box2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -12244,6 +12244,14 @@
return body;
};

const _removeBody = function (id) {
if (!bodies[id]) return;

world.DestroyBody(bodies[id]);
delete bodies[id];
delete prevPos[id];
};

const _applyForce = function (id, ftype, x, y, dir, pow) {
const body = bodies[id];
if (!body) {
Expand Down Expand Up @@ -12444,6 +12452,8 @@
}
};

let tickRate = 30;

const blockIconURI =
"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiDQoJIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbG5zOmE9Imh0dHA6Ly9ucy5hZG9iZS5jb20vQWRvYmVTVkdWaWV3ZXJFeHRlbnNpb25zLzMuMC8iDQoJIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iNDBweCIgaGVpZ2h0PSI0MHB4IiB2aWV3Qm94PSItMy43IC0zLjcgNDAgNDAiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgLTMuNyAtMy43IDQwIDQwIg0KCSB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxkZWZzPg0KPC9kZWZzPg0KPHJlY3QgeD0iOC45IiB5PSIxLjUiIGZpbGw9IiNGRkZGRkYiIHN0cm9rZT0iIzE2OUZCMCIgc3Ryb2tlLXdpZHRoPSIzIiB3aWR0aD0iMTQuOCIgaGVpZ2h0PSIxNC44Ii8+DQo8cmVjdCB4PSIxLjUiIHk9IjE2LjMiIGZpbGw9IiNGRkZGRkYiIHN0cm9rZT0iIzE2OUZCMCIgc3Ryb2tlLXdpZHRoPSIzIiB3aWR0aD0iMTQuOCIgaGVpZ2h0PSIxNC44Ii8+DQo8cmVjdCB4PSIxNi4zIiB5PSIxNi4zIiBmaWxsPSIjRkZGRkZGIiBzdHJva2U9IiMxNjlGQjAiIHN0cm9rZS13aWR0aD0iMyIgd2lkdGg9IjE0LjgiIGhlaWdodD0iMTQuOCIvPg0KPC9zdmc+";
const menuIconURI =
Expand Down Expand Up @@ -12487,6 +12497,7 @@
delete bodies[body];
delete prevPos[body];
}
tickRate = 30;
// todo: delete joins?
}

Expand Down Expand Up @@ -12593,6 +12604,17 @@
},
filter: [Scratch.TargetType.SPRITE],
},
{
opcode: "disablePhysics",
blockType: BlockType.COMMAND,
text: Scratch.translate({
id: "griffpatch.disablePhysics",
default: "disable physics for this sprite",
description: "Disable Physics for this Sprite",
}),
arguments: {},
filter: [Scratch.TargetType.SPRITE],
},
// {
// opcode: 'setPhysics',
// blockType: BlockType.COMMAND,
Expand Down Expand Up @@ -12630,6 +12652,32 @@
description: "Run a single tick of the physics simulation",
}),
},
{
opcode: "setTickRate",
blockType: BlockType.COMMAND,
text: Scratch.translate({
id: "griffpatch.setTickRate",
default: "set simulation rate to [rate]/s",
description:
"Set the number of physics simulation steps to run per second",
}),
arguments: {
rate: {
type: ArgumentType.NUMBER,
defaultValue: 30,
},
},
},
{
opcode: "getTickRate",
blockType: BlockType.REPORTER,
text: Scratch.translate({
id: "griffpatch.getTickRate",
default: "simulation rate",
description:
"Get the number of physics simulation steps to run per second",
}),
},

"---",

Expand Down Expand Up @@ -13219,7 +13267,7 @@
this._checkMoved();

// world.Step(1 / 30, 10, 10);
world.Step(1 / 30, 10, 10);
world.Step(1 / tickRate, 10, 10);
world.ClearForces();

for (const targetID in bodies) {
Expand Down Expand Up @@ -13248,6 +13296,18 @@
}
}

setTickRate(args) {
let rate = Scratch.Cast.toNumber(args.rate);
if (Number.isNaN(rate) || rate === Infinity) rate = 30;
rate = Math.max(rate, 0.01);

tickRate = rate;
}

getTickRate() {
return tickRate;
}

_checkMoved() {
for (const targetID in bodies) {
const body = bodies[targetID];
Expand Down Expand Up @@ -13408,6 +13468,10 @@
return body;
}

disablePhysics(args, util) {
_removeBody(util.target.id);
}

/**
*
* @param svg the svg element
Expand Down

0 comments on commit 11ca543

Please sign in to comment.