Skip to content
This repository was archived by the owner on Mar 5, 2021. It is now read-only.

Commit 0ee4dbb

Browse files
authored
Merge pull request #140 from Ikalou/p2js-box-angle
p2js: Add angle body setting so we don't only rely on the actor orientation
2 parents dc3bd33 + b5068ed commit 0ee4dbb

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

plugins/extra/p2js/componentConfigs/P2BodyConfig.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface P2BodyConfigPub {
88
shape: string;
99
width: number;
1010
height: number;
11+
angle: number;
1112
radius: number;
1213
length: number;
1314
}
@@ -24,6 +25,7 @@ export default class P2BodyConfig extends SupCore.Data.Base.ComponentConfig {
2425
shape: { type: "enum", items: [ "box", "circle" ], mutable: true },
2526
width: { type: "number", min: 0, mutable: true },
2627
height: { type: "number", min: 0, mutable: true },
28+
angle: { type: "number", min: -360, max: 360, mutable: true },
2729
radius: { type: "number", min: 0, mutable: true }
2830
};
2931

@@ -38,13 +40,14 @@ export default class P2BodyConfig extends SupCore.Data.Base.ComponentConfig {
3840
shape: "box",
3941
width: 1,
4042
height: 1,
43+
angle: 0,
4144
radius: 1,
4245
length: 1
4346
};
4447
return emptyConfig;
4548
}
4649

47-
static currentFormatVersion = 1;
50+
static currentFormatVersion = 2;
4851
static migrate(pub: P2BodyConfigPub) {
4952
if (pub.formatVersion === P2BodyConfig.currentFormatVersion) return false;
5053

@@ -55,6 +58,12 @@ export default class P2BodyConfig extends SupCore.Data.Base.ComponentConfig {
5558
if (pub.shape === "rectangle") pub.shape = "box";
5659
}
5760

61+
if (pub.formatVersion === 1) {
62+
pub.formatVersion = 2;
63+
64+
pub.angle = 0;
65+
}
66+
5867
return true;
5968
}
6069

plugins/extra/p2js/componentEditors/P2BodyEditor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class P2BodyEditor {
77

88
sizeRow: SupClient.table.RowParts;
99
radiusRow: SupClient.table.RowParts;
10+
angleRow: SupClient.table.RowParts;
1011

1112
constructor(tbody: HTMLTableSectionElement, config: any, projectClient: SupClient.ProjectClient, editConfig: any) {
1213
this.tbody = tbody;
@@ -61,6 +62,12 @@ export default class P2BodyEditor {
6162
this.editConfig("setProperty", "height", parseFloat(event.target.value));
6263
});
6364

65+
this.angleRow = SupClient.table.appendRow(this.tbody, SupClient.i18n.t("componentEditors:P2Body.angle"));
66+
this.fields["angle"] = SupClient.table.appendNumberField(this.angleRow.valueCell, config.angle, { min: -360, max: 360 });
67+
this.fields["angle"].addEventListener("change", (event: any) => {
68+
this.editConfig("setProperty", "angle", parseFloat(event.target.value));
69+
});
70+
6471
// Circle
6572
this.radiusRow = SupClient.table.appendRow(this.tbody, SupClient.i18n.t("componentEditors:P2Body.radius"));
6673
this.fields["radius"] = SupClient.table.appendNumberField(this.radiusRow.valueCell, config.radius, { min: 0 });
@@ -76,11 +83,13 @@ export default class P2BodyEditor {
7683
case "box": {
7784
this.sizeRow.row.hidden = false;
7885
this.radiusRow.row.hidden = true;
86+
this.angleRow.row.hidden = false;
7987
} break;
8088

8189
case "circle": {
8290
this.sizeRow.row.hidden = true;
8391
this.radiusRow.row.hidden = false;
92+
this.angleRow.row.hidden = true;
8493
} break;
8594
}
8695
}

plugins/extra/p2js/components/P2Body.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class P2Body extends SupEngine.ActorComponent {
1111

1212
width: number;
1313
height: number;
14+
angle: number;
1415
radius: number;
1516

1617
actorPosition = new THREE.Vector3();
@@ -45,24 +46,26 @@ export default class P2Body extends SupEngine.ActorComponent {
4546
case "box": {
4647
this.width = (config.width != null) ? config.width : 0.5;
4748
this.height = (config.height != null) ? config.height : 0.5;
49+
this.angle = (config.angle != null) ? config.angle * (Math.PI / 180) : 0;
4850
this.body.addShape(new (<any>window).p2.Box({ width: this.width, height: this.height }));
4951
} break;
5052
case "circle": {
5153
this.radius = (config.radius != null) ? config.radius : 1;
54+
this.angle = 0;
5255
this.body.addShape(new (<any>window).p2.Circle({ radius: this.radius }));
5356
} break;
5457
}
5558
this.body.position = [ this.actorPosition.x, this.actorPosition.y ];
5659
this.body.shapes[0].position = [ this.offsetX, this.offsetY ];
57-
this.body.angle = this.actorAngles.z;
60+
this.body.angle = this.actorAngles.z + this.angle;
5861
}
5962

6063
update() {
6164
this.actorPosition.x = this.body.position[0];
6265
this.actorPosition.y = this.body.position[1];
6366
this.actor.setGlobalPosition(this.actorPosition);
6467

65-
this.actorAngles.z = this.body.angle;
68+
this.actorAngles.z = this.body.angle - this.angle;
6669
this.actor.setGlobalEulerAngles(this.actorAngles);
6770
}
6871

plugins/extra/p2js/components/P2BodyMarker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default class P2BodyMarker extends SupEngine.ActorComponent {
88

99
mesh: THREE.Line|THREE.Mesh;
1010
offset = new THREE.Vector3(0, 0, 0);
11+
angle = 0;
1112

1213
constructor(actor: SupEngine.Actor) {
1314
super(actor, "P2BodyMarker");
@@ -32,6 +33,7 @@ export default class P2BodyMarker extends SupEngine.ActorComponent {
3233
this.mesh = new THREE.Line(geometry, material);
3334
this.actor.threeObject.add(this.mesh);
3435
this.mesh.position.copy(this.offset);
36+
this.mesh.rotation.z = this.angle;
3537
this.mesh.updateMatrixWorld(false);
3638
}
3739

@@ -52,6 +54,12 @@ export default class P2BodyMarker extends SupEngine.ActorComponent {
5254
this.mesh.updateMatrixWorld(false);
5355
}
5456

57+
setAngle(angle: number) {
58+
this.angle = angle * (Math.PI / 180);
59+
this.mesh.rotation.z = this.angle;
60+
this.mesh.updateMatrixWorld(false);
61+
}
62+
5563
_clearRenderer() {
5664
this.actor.threeObject.remove(this.mesh);
5765
this.mesh.geometry.dispose();

plugins/extra/p2js/components/P2BodyMarkerUpdater.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class P2BodyMarkerUpdater {
1414
case "circle": { this.bodyRenderer.setCircle(this.config.radius); } break;
1515
}
1616
this.bodyRenderer.setOffset(this.config.offsetX, this.config.offsetY);
17+
this.bodyRenderer.setAngle(this.config.angle);
1718
}
1819

1920
destroy() { /* Ignore */ }
@@ -30,5 +31,9 @@ export default class P2BodyMarkerUpdater {
3031
if (path === "offsetX" || path === "offsetY") {
3132
this.bodyRenderer.setOffset(this.config.offsetX, this.config.offsetY);
3233
}
34+
35+
if (path === "angle") {
36+
this.bodyRenderer.setAngle(this.config.angle);
37+
}
3338
}
3439
}

plugins/extra/p2js/public/locales/en/componentEditors.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"mass": "Mass",
55
"fixedRotation": "Fixed rotation",
66
"offset": "Offset",
7+
"angle": "Angle",
78
"shape": "Shape",
89
"shapeOptions": {
910
"box": "Box",

0 commit comments

Comments
 (0)