rngine 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,6 +59,10 @@ export interface Collision {
59
59
  b: string;
60
60
  /** Penetration depth of the collision. */
61
61
  depth: number;
62
+ /** X component of a normalised vector that represents the direction between the bodies providing the minimum separating distance. */
63
+ nx: number;
64
+ /** Y component of a normalised vector that represents the direction between the bodies providing the minimum separating distance. */
65
+ ny: number;
62
66
  }
63
67
  export interface System {
64
68
  /** Entity ids or prefixes this system subscribes to. */
@@ -1 +1 @@
1
- {"version":3,"file":"nativeTypes.d.ts","sourceRoot":"","sources":["../../../src/nativeTypes.ts"],"names":[],"mappings":"AAAA,UAAU,UAAU;IAClB,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,UAAU,IAAI;IACZ,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,MAAM;IACd,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,MAAM;IACd,2BAA2B;IAC3B,KAAK,EAAE,IAAI,GAAG,MAAM,CAAC;CACtB;AAED,UAAU,UAAU;IAClB,mDAAmD;IACnD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8FAA8F;IAC9F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,SAAS;IACjB,oDAAoD;IACpD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,kDAAkD;IAClD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,gEAAgE;IAChE,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,8DAA8D;IAC9D,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,W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;IACpB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,aAAa,EAAE,CAAC;IAC7B,mEAAmE;IACnE,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,MAAM,CAAC;CACjE"}
@@ -42,10 +42,12 @@ namespace margelo::nitro::rngine {
42
42
  std::string a SWIFT_PRIVATE;
43
43
  std::string b SWIFT_PRIVATE;
44
44
  double depth SWIFT_PRIVATE;
45
+ double nx SWIFT_PRIVATE;
46
+ double ny SWIFT_PRIVATE;
45
47
 
46
48
  public:
47
49
  Collision() = default;
48
- explicit Collision(std::string a, std::string b, double depth): a(a), b(b), depth(depth) {}
50
+ explicit Collision(std::string a, std::string b, double depth, double nx, double ny): a(a), b(b), depth(depth), nx(nx), ny(ny) {}
49
51
 
50
52
  public:
51
53
  friend bool operator==(const Collision& lhs, const Collision& rhs) = default;
@@ -63,7 +65,9 @@ namespace margelo::nitro {
63
65
  return margelo::nitro::rngine::Collision(
64
66
  JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "a"))),
65
67
  JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "b"))),
66
- JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "depth")))
68
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "depth"))),
69
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "nx"))),
70
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ny")))
67
71
  );
68
72
  }
69
73
  static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::Collision& arg) {
@@ -71,6 +75,8 @@ namespace margelo::nitro {
71
75
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "a"), JSIConverter<std::string>::toJSI(runtime, arg.a));
72
76
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "b"), JSIConverter<std::string>::toJSI(runtime, arg.b));
73
77
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "depth"), JSIConverter<double>::toJSI(runtime, arg.depth));
78
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "nx"), JSIConverter<double>::toJSI(runtime, arg.nx));
79
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "ny"), JSIConverter<double>::toJSI(runtime, arg.ny));
74
80
  return obj;
75
81
  }
76
82
  static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
@@ -84,6 +90,8 @@ namespace margelo::nitro {
84
90
  if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "a")))) return false;
85
91
  if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "b")))) return false;
86
92
  if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "depth")))) return false;
93
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "nx")))) return false;
94
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ny")))) return false;
87
95
  return true;
88
96
  }
89
97
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rngine",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "React Native 2D game engine powered by Nitro Modules",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -1,8 +1,10 @@
1
1
  #include "CollisionUtils.hpp"
2
+ #include "Collision.hpp"
3
+ #include <optional>
2
4
 
3
5
  namespace margelo::nitro::rngine::CollisionUtils {
4
- std::optional<double> overlap(const Rect &a, double ax, double ay,
5
- const Rect &b, double bx, double by) {
6
+ std::optional<Overlap> overlap(const Rect &a, double ax, double ay,
7
+ const Rect &b, double bx, double by) {
6
8
 
7
9
  double aLeft = ax - a.width / 2.0, aRight = ax + a.width / 2.0;
8
10
  double aTop = ay - a.height / 2.0, aBottom = ay + a.height / 2.0;
@@ -12,24 +14,35 @@ std::optional<double> overlap(const Rect &a, double ax, double ay,
12
14
  double overlapX = std::min(aRight, bRight) - std::max(aLeft, bLeft);
13
15
  double overlapY = std::min(aBottom, bBottom) - std::max(aTop, bTop);
14
16
 
15
- if (overlapX > 0 && overlapY > 0)
16
- return std::min(overlapX, overlapY);
17
- return std::nullopt;
17
+ if (overlapX <= 0 || overlapY <= 0)
18
+ return std::nullopt;
19
+
20
+ if (overlapX < overlapY) {
21
+ double nx = (bx >= ax) ? 1.0 : -1.0;
22
+ return Overlap{overlapX, nx, 0.0};
23
+ } else {
24
+ double ny = (by >= ay) ? 1.0 : -1.0;
25
+ return Overlap{overlapY, 0.0, ny};
26
+ }
18
27
  }
19
28
 
20
- std::optional<double> overlap(const Circle &a, double ax, double ay,
21
- const Circle &b, double bx, double by) {
29
+ std::optional<Overlap> overlap(const Circle &a, double ax, double ay,
30
+ const Circle &b, double bx, double by) {
22
31
  double dx = bx - ax, dy = by - ay;
23
32
  double dist = std::sqrt(dx * dx + dy * dy);
24
33
  double depth = (a.radius + b.radius) - dist;
25
34
 
26
- if (depth > 0)
27
- return depth;
28
- return std::nullopt;
35
+ if (depth <= 0)
36
+ return std::nullopt;
37
+
38
+ if (dist == 0.0)
39
+ return Overlap{depth, 0.0, -1.0};
40
+
41
+ return Overlap{depth, dx / dist, dy / dist};
29
42
  }
30
43
 
31
- std::optional<double> overlap(const Rect &a, double ax, double ay,
32
- const Circle &b, double bx, double by) {
44
+ std::optional<Overlap> overlap(const Rect &a, double ax, double ay,
45
+ const Circle &b, double bx, double by) {
33
46
  double left = ax - a.width / 2.0, right = ax + a.width / 2.0;
34
47
  double top = ay - a.height / 2.0, bottom = ay + a.height / 2.0;
35
48
 
@@ -42,26 +55,49 @@ std::optional<double> overlap(const Rect &a, double ax, double ay,
42
55
  if (distSq >= b.radius * b.radius)
43
56
  return std::nullopt;
44
57
 
58
+ if (distSq == 0.0) {
59
+ double cdx = bx - ax, cdy = by - ay;
60
+ double cdist = std::sqrt(cdx * cdx + cdy * cdy);
61
+ if (cdist == 0.0)
62
+ return Overlap{b.radius, 0.0, -1.0};
63
+ return Overlap{b.radius, cdx / cdist, cdy / cdist};
64
+ }
65
+
45
66
  double dist = std::sqrt(distSq);
46
- return b.radius - dist;
67
+ return Overlap{b.radius - dist, dx / dist, dy / dist};
47
68
  }
48
69
 
49
- std::optional<double> overlap(const Circle &a, double ax, double ay,
50
- const Rect &b, double bx, double by) {
51
- return overlap(b, bx, by, a, ax, ay);
70
+ std::optional<Overlap> overlap(const Circle &a, double ax, double ay,
71
+ const Rect &b, double bx, double by) {
72
+ auto result = overlap(b, bx, by, a, ax, ay);
73
+
74
+ if (!result.has_value()) {
75
+ return std::nullopt;
76
+ }
77
+
78
+ result.value().nx = -result.value().nx;
79
+ result.value().ny = -result.value().ny;
80
+
81
+ return result;
52
82
  }
53
83
 
54
- std::optional<double> shapeOverlap(const Entity &entityA,
55
- const Entity &entityB) {
56
- return std::visit(
57
- [&](const auto &shapeA) -> std::optional<double> {
84
+ std::optional<Collision> shapeOverlap(const Entity &entityA,
85
+ const Entity &entityB) {
86
+ auto result = std::visit(
87
+ [&](const auto &shapeA) -> std::optional<Overlap> {
58
88
  return std::visit(
59
- [&](const auto &shapeB) -> std::optional<double> {
89
+ [&](const auto &shapeB) -> std::optional<Overlap> {
60
90
  return overlap(shapeA, entityA.px, entityA.py, shapeB, entityB.px,
61
91
  entityB.py);
62
92
  },
63
93
  entityB.shape);
64
94
  },
65
95
  entityA.shape);
96
+ if (!result.has_value()) {
97
+ return nullopt;
98
+ }
99
+
100
+ return Collision{entityA.id, entityB.id, result->depth, result->nx,
101
+ result->ny};
66
102
  }
67
103
  } // namespace margelo::nitro::rngine::CollisionUtils
@@ -1,19 +1,26 @@
1
1
  #pragma once
2
2
 
3
3
  #include "Circle.hpp"
4
+ #include "Collision.hpp"
4
5
  #include "Entity.hpp"
5
6
  #include "Rect.hpp"
6
7
  #include <optional>
7
8
 
8
9
  namespace margelo::nitro::rngine::CollisionUtils {
9
- std::optional<double> overlap(const Rect &a, double ax, double ay,
10
- const Rect &b, double bx, double by);
11
- std::optional<double> overlap(const Circle &a, double ax, double ay,
12
- const Circle &b, double bx, double by);
13
- std::optional<double> overlap(const Rect &a, double ax, double ay,
14
- const Circle &b, double bx, double by);
15
- std::optional<double> overlap(const Circle &a, double ax, double ay,
16
- const Rect &b, double bx, double by);
17
- std::optional<double> shapeOverlap(const Entity &entityA,
18
- const Entity &entityB);
10
+ struct Overlap {
11
+ double depth;
12
+ double nx;
13
+ double ny;
14
+ };
15
+
16
+ std::optional<Overlap> overlap(const Rect &a, double ax, double ay,
17
+ const Rect &b, double bx, double by);
18
+ std::optional<Overlap> overlap(const Circle &a, double ax, double ay,
19
+ const Circle &b, double bx, double by);
20
+ std::optional<Overlap> overlap(const Rect &a, double ax, double ay,
21
+ const Circle &b, double bx, double by);
22
+ std::optional<Overlap> overlap(const Circle &a, double ax, double ay,
23
+ const Rect &b, double bx, double by);
24
+ std::optional<Collision> shapeOverlap(const Entity &entityA,
25
+ const Entity &entityB);
19
26
  } // namespace margelo::nitro::rngine::CollisionUtils
@@ -126,9 +126,9 @@ void GameLoop::runSystems() {
126
126
 
127
127
  uniqueEntityIdPairs.insert(uniqueEntityIdPairKey);
128
128
 
129
- if (auto ov = CollisionUtils::shapeOverlap(*entityA, *entityB)) {
130
- collisions.push_back(
131
- Collision(entityA->id, entityB->id, ov.value()));
129
+ if (auto collision =
130
+ CollisionUtils::shapeOverlap(*entityA, *entityB)) {
131
+ collisions.push_back(collision.value());
132
132
  }
133
133
  }
134
134
  }
@@ -70,6 +70,10 @@ export interface Collision {
70
70
  b: string;
71
71
  /** Penetration depth of the collision. */
72
72
  depth: number;
73
+ /** X component of a normalised vector that represents the direction between the bodies providing the minimum separating distance. */
74
+ nx: number;
75
+ /** Y component of a normalised vector that represents the direction between the bodies providing the minimum separating distance. */
76
+ ny: number;
73
77
  }
74
78
 
75
79
  export interface System {
Binary file