Skip to content

Commit 426d940

Browse files
authored
Merge pull request #4 from tomekzaw/polygon-points
Add polygon shape from points
2 parents bcdcc9f + 2cb5233 commit 426d940

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

cpp/JSIBox2dPolygonShape.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ namespace Box2d {
2020
return jsi::Value::undefined();
2121
}
2222

23-
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JSIBox2dPolygonShape, SetAsBox))
23+
JSI_HOST_FUNCTION(Set) {
24+
auto array = arguments[0].asObject(runtime).asArray(runtime);
25+
auto n = static_cast<int32>(array.length(runtime));
26+
b2Vec2 points[n];
27+
for (int32 i = 0; i < n; ++i) {
28+
auto point = JSIBox2dVec2::fromValue(runtime, array.getValueAtIndex(runtime, i));
29+
points[i].x = point->x;
30+
points[i].y = point->y;
31+
}
32+
getObject()->Set(points, n);
33+
return jsi::Value::undefined();
34+
}
35+
36+
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JSIBox2dPolygonShape, SetAsBox),
37+
JSI_EXPORT_FUNC(JSIBox2dPolygonShape, Set))
2438

2539
/**
2640
* Constructor

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export interface b2PolygonShape extends b2Shape {
3333
* @return Box polygon shape.
3434
**/
3535
SetAsBox(hx: number, hy: number): void;
36+
37+
/**
38+
* Create a convex hull from the given array of local points. The count must be in the range [3, b2_maxPolygonVertices].
39+
* @param points The array of local points.
40+
* @return Polygon shape.
41+
**/
42+
Set(points: b2Vec2[]): void;
3643
}
3744

3845
export interface b2CircleShape extends b2Shape {

0 commit comments

Comments
 (0)