rngine 0.7.0 → 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.
@@ -27,10 +27,14 @@ interface Renderable {
27
27
  progress?: number;
28
28
  }
29
29
  interface Kinematic {
30
- /** Velocity on the X axis in game units per second. */
30
+ /** Horizontal velocity in game units per second. */
31
31
  vx?: number;
32
- /** Velocity on the Y axis in game units per second. */
32
+ /** Vertical velocity in game units per second. */
33
33
  vy?: number;
34
+ /** Horizontal acceleration in game units per second squared. */
35
+ ax?: number;
36
+ /** Vertical acceleration in game units per second squared. */
37
+ ay?: number;
34
38
  }
35
39
  export interface Screen extends Rect, Renderable {
36
40
  }
@@ -55,6 +59,10 @@ export interface Collision {
55
59
  b: string;
56
60
  /** Penetration depth of the collision. */
57
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;
58
66
  }
59
67
  export interface System {
60
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,uEAAu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E,MAAM,CAAC;IACV,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,qIAAqI;IACrI,EAAE,EAAE,MAAM,CAAC;IACX,qIAAqI;IACrI,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,MAAM;IACrB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;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
  };
@@ -55,10 +55,12 @@ namespace margelo::nitro::rngine {
55
55
  std::optional<double> progress SWIFT_PRIVATE;
56
56
  std::optional<double> vx SWIFT_PRIVATE;
57
57
  std::optional<double> vy SWIFT_PRIVATE;
58
+ std::optional<double> ax SWIFT_PRIVATE;
59
+ std::optional<double> ay SWIFT_PRIVATE;
58
60
 
59
61
  public:
60
62
  Entity() = default;
61
- explicit Entity(std::string id, double px, double py, std::variant<Rect, Circle> shape, std::optional<std::string> color, std::optional<double> asset, std::optional<double> progress, std::optional<double> vx, std::optional<double> vy): id(id), px(px), py(py), shape(shape), color(color), asset(asset), progress(progress), vx(vx), vy(vy) {}
63
+ explicit Entity(std::string id, double px, double py, std::variant<Rect, Circle> shape, std::optional<std::string> color, std::optional<double> asset, std::optional<double> progress, std::optional<double> vx, std::optional<double> vy, std::optional<double> ax, std::optional<double> ay): id(id), px(px), py(py), shape(shape), color(color), asset(asset), progress(progress), vx(vx), vy(vy), ax(ax), ay(ay) {}
62
64
 
63
65
  public:
64
66
  friend bool operator==(const Entity& lhs, const Entity& rhs) = default;
@@ -82,7 +84,9 @@ namespace margelo::nitro {
82
84
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "asset"))),
83
85
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "progress"))),
84
86
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vx"))),
85
- JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy")))
87
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy"))),
88
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ax"))),
89
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ay")))
86
90
  );
87
91
  }
88
92
  static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::Entity& arg) {
@@ -96,6 +100,8 @@ namespace margelo::nitro {
96
100
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "progress"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.progress));
97
101
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "vx"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.vx));
98
102
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "vy"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.vy));
103
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "ax"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.ax));
104
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "ay"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.ay));
99
105
  return obj;
100
106
  }
101
107
  static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
@@ -115,6 +121,8 @@ namespace margelo::nitro {
115
121
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "progress")))) return false;
116
122
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vx")))) return false;
117
123
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy")))) return false;
124
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ax")))) return false;
125
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ay")))) return false;
118
126
  return true;
119
127
  }
120
128
  };
@@ -55,10 +55,12 @@ namespace margelo::nitro::rngine {
55
55
  std::optional<double> progress SWIFT_PRIVATE;
56
56
  std::optional<double> vx SWIFT_PRIVATE;
57
57
  std::optional<double> vy SWIFT_PRIVATE;
58
+ std::optional<double> ax SWIFT_PRIVATE;
59
+ std::optional<double> ay SWIFT_PRIVATE;
58
60
 
59
61
  public:
60
62
  EntityUpdate() = default;
61
- explicit EntityUpdate(std::string id, std::optional<double> px, std::optional<double> py, std::optional<std::variant<Rect, Circle>> shape, std::optional<std::string> color, std::optional<double> asset, std::optional<double> progress, std::optional<double> vx, std::optional<double> vy): id(id), px(px), py(py), shape(shape), color(color), asset(asset), progress(progress), vx(vx), vy(vy) {}
63
+ explicit EntityUpdate(std::string id, std::optional<double> px, std::optional<double> py, std::optional<std::variant<Rect, Circle>> shape, std::optional<std::string> color, std::optional<double> asset, std::optional<double> progress, std::optional<double> vx, std::optional<double> vy, std::optional<double> ax, std::optional<double> ay): id(id), px(px), py(py), shape(shape), color(color), asset(asset), progress(progress), vx(vx), vy(vy), ax(ax), ay(ay) {}
62
64
 
63
65
  public:
64
66
  friend bool operator==(const EntityUpdate& lhs, const EntityUpdate& rhs) = default;
@@ -82,7 +84,9 @@ namespace margelo::nitro {
82
84
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "asset"))),
83
85
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "progress"))),
84
86
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vx"))),
85
- JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy")))
87
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy"))),
88
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ax"))),
89
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ay")))
86
90
  );
87
91
  }
88
92
  static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::EntityUpdate& arg) {
@@ -96,6 +100,8 @@ namespace margelo::nitro {
96
100
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "progress"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.progress));
97
101
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "vx"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.vx));
98
102
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "vy"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.vy));
103
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "ax"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.ax));
104
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "ay"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.ay));
99
105
  return obj;
100
106
  }
101
107
  static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
@@ -115,6 +121,8 @@ namespace margelo::nitro {
115
121
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "progress")))) return false;
116
122
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vx")))) return false;
117
123
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "vy")))) return false;
124
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ax")))) return false;
125
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ay")))) return false;
118
126
  return true;
119
127
  }
120
128
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rngine",
3
- "version": "0.7.0",
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
  }
@@ -192,16 +192,23 @@ void GameLoop::updateEntities(double deltaTime) {
192
192
  std::lock_guard<std::mutex> lock(_mutex);
193
193
 
194
194
  for (auto &[id, entity] : _entities) {
195
- if (entity.vx) {
196
- entity.px += *entity.vx * deltaTime;
195
+
196
+ if (entity.ax.has_value()) {
197
+ entity.vx = entity.vx.value_or(0.0) + entity.ax.value() * deltaTime;
198
+ }
199
+ if (entity.ay.has_value()) {
200
+ entity.vy = entity.vy.value_or(0.0) + entity.ay.value() * deltaTime;
197
201
  }
198
202
 
199
- if (entity.vy) {
200
- entity.py += *entity.vy * deltaTime;
203
+ if (entity.vx.has_value()) {
204
+ entity.px += entity.vx.value() * deltaTime;
205
+ }
206
+ if (entity.vy.has_value()) {
207
+ entity.py += entity.vy.value() * deltaTime;
201
208
  }
202
209
 
203
- if (entity.asset && entity.asset < 0) {
204
- auto it = _lottieDurations.find(*entity.asset);
210
+ if (entity.asset.has_value() && entity.asset.value() < 0) {
211
+ auto it = _lottieDurations.find(entity.asset.value());
205
212
  if (it != _lottieDurations.end() && it->second > 0.0) {
206
213
  entity.progress =
207
214
  fmod(entity.progress.value_or(0.0) + deltaTime / it->second, 1.0);
@@ -145,6 +145,12 @@ void GameMethods::update(const std::vector<EntityUpdate> &updates) {
145
145
  if (update.vy.has_value()) {
146
146
  entity->vy = update.vy.value();
147
147
  }
148
+ if (update.ax.has_value()) {
149
+ entity->ax = update.ax.value();
150
+ }
151
+ if (update.ay.has_value()) {
152
+ entity->ay = update.ay.value();
153
+ }
148
154
  }
149
155
 
150
156
  __android_log_print(ANDROID_LOG_INFO, "GameMethods",
@@ -32,10 +32,14 @@ interface Renderable {
32
32
  }
33
33
 
34
34
  interface Kinematic {
35
- /** Velocity on the X axis in game units per second. */
35
+ /** Horizontal velocity in game units per second. */
36
36
  vx?: number;
37
- /** Velocity on the Y axis in game units per second. */
37
+ /** Vertical velocity in game units per second. */
38
38
  vy?: number;
39
+ /** Horizontal acceleration in game units per second squared. */
40
+ ax?: number;
41
+ /** Vertical acceleration in game units per second squared. */
42
+ ay?: number;
39
43
  }
40
44
 
41
45
  export interface Screen extends Rect, Renderable {}
@@ -66,6 +70,10 @@ export interface Collision {
66
70
  b: string;
67
71
  /** Penetration depth of the collision. */
68
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;
69
77
  }
70
78
 
71
79
  export interface System {
Binary file