Skip to content

Commit daf2fc8

Browse files
committed
Add circle shape
1 parent c271062 commit daf2fc8

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

cpp/JSIBox2dApi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "JSIBox2dWorld.h"
1111
#include "JSIBox2dBodyDef.h"
1212
#include "JSIBox2dPolygonShape.h"
13+
#include "JSIBox2dCircleShape.h"
1314
#include "JSIBox2dFixtureDef.h"
1415

1516
namespace Box2d {
@@ -22,6 +23,7 @@ namespace Box2d {
2223
installFunction("b2World", JSIBox2dWorld::createCtor());
2324
installFunction("b2BodyDef", JSIBox2dBodyDef::createCtor());
2425
installFunction("b2PolygonShape", JSIBox2dPolygonShape::createCtor());
26+
installFunction("b2CircleShape", JSIBox2dCircleShape::createCtor());
2527
installFunction("b2FixtureDef", JSIBox2dFixtureDef::createCtor());
2628
}
2729
};

cpp/JSIBox2dCircleShape.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Created by Tomek Zawadzki on 26.02.23.
3+
//
4+
5+
#pragma once
6+
7+
#include "box2d/b2_circle_shape.h"
8+
9+
namespace Box2d {
10+
using namespace facebook;
11+
12+
class JSIBox2dCircleShape : public JsiWrappingSharedPtrHostObject<b2CircleShape> {
13+
public:
14+
15+
JSI_HOST_FUNCTION(SetRadius) {
16+
getObject()->m_radius = arguments[0].asNumber();
17+
return jsi::Value::undefined();
18+
}
19+
20+
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JSIBox2dCircleShape, SetRadius))
21+
22+
/**
23+
* Constructor
24+
*/
25+
JSIBox2dCircleShape(const b2CircleShape &shape)
26+
: JsiWrappingSharedPtrHostObject<b2CircleShape>(
27+
std::make_shared<b2CircleShape>(std::move(shape))) {}
28+
29+
/**
30+
* Returns the underlying object from a host object of this type
31+
*/
32+
static std::shared_ptr<b2CircleShape> fromValue(jsi::Runtime &runtime,
33+
const jsi::Value &obj) {
34+
return obj.asObject(runtime)
35+
.asHostObject<JSIBox2dCircleShape>(runtime)
36+
->getObject();
37+
}
38+
39+
static const jsi::HostFunctionType
40+
createCtor() {
41+
return JSI_HOST_FUNCTION_LAMBDA {
42+
b2CircleShape shape;
43+
44+
return jsi::Object::createFromHostObject(
45+
runtime,
46+
std::make_shared<JSIBox2dCircleShape>(
47+
std::move(shape)
48+
)
49+
);
50+
};
51+
};
52+
};
53+
}

cpp/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "box2d/b2_shape.h"
88
#include "JSIBox2dShape.h"
99
#include "JSIBox2dPolygonShape.h"
10+
#include "JSIBox2dCircleShape.h"
1011

1112
namespace Box2d {
1213
class Utils {
@@ -17,6 +18,9 @@ namespace Box2d {
1718
if (obj.isHostObject<JSIBox2dPolygonShape>(runtime)) {
1819
return JSIBox2dPolygonShape::fromValue(runtime, value).get();
1920
}
21+
if (obj.isHostObject<JSIBox2dCircleShape>(runtime)) {
22+
return JSIBox2dCircleShape::fromValue(runtime, value).get();
23+
}
2024
}
2125
return nullptr;
2226
}

src/Box2d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NativeModules, Platform } from 'react-native';
22
import type {
33
b2BodyDef,
4+
b2CircleShape,
45
b2FixtureDef,
56
b2PolygonShape,
67
b2Vec2,
@@ -18,6 +19,7 @@ declare global {
1819
b2World: (vec: b2Vec2) => b2World;
1920
b2BodyDef: () => b2BodyDef;
2021
b2PolygonShape: () => b2PolygonShape;
22+
b2CircleShape: () => b2CircleShape;
2123
b2FixtureDef: () => b2FixtureDef;
2224
};
2325
}

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export interface b2PolygonShape extends b2Shape {
3535
SetAsBox(hx: number, hy: number): void;
3636
}
3737

38+
export interface b2CircleShape extends b2Shape {
39+
/**
40+
* Set the radius of the circle.
41+
* @param r Radius of the circle.
42+
* @return Circle shape.
43+
**/
44+
SetRadius(r: number): void;
45+
}
46+
3847
export interface b2FixtureDef {
3948
/**
4049
* The density, usually in kg/m^2.

0 commit comments

Comments
 (0)