Skip to content

Commit

Permalink
[cpp] Port physics translation and rotation methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Jan 24, 2024
1 parent 73cb252 commit 922ac2b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spine-cpp/spine-cpp/include/spine/PhysicsConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ namespace spine {

virtual void update(Physics physics);

void translate(float x, float y);

void rotate(float x, float y, float degrees);

private:
PhysicsConstraintData& _data;
Bone* _bone;
Expand Down
7 changes: 7 additions & 0 deletions spine-cpp/spine-cpp/include/spine/Skeleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ namespace spine {

void update(float delta);

/// Rotates the physics constraint so next {@link #update(Physics)} forces are applied as if the bone rotated around the
/// specified point in world space.
void physicsTranslate(float x, float y);

/// Calls {@link PhysicsConstraint#rotate(float, float, float)} for each physics constraint. */
void physicsRotate(float x, float y, float degrees);

private:
SkeletonData *_data;
Vector<Bone *> _bones;
Expand Down
16 changes: 16 additions & 0 deletions spine-cpp/spine-cpp/src/spine/PhysicsConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,19 @@ void PhysicsConstraint::update(Physics physics) {
}
bone->updateAppliedTransform();
}

void PhysicsConstraint::rotate(float x, float y, float degrees) {
float r = degrees * MathUtil::Deg_Rad, cos = MathUtil::cos(r), sin = MathUtil::sin(r);
r = _tx * cos - _ty * sin;
_ty = _tx * sin + _ty * cos;
_tx = r;
float dx = _cx - x, dy = _cy - y;
translate(dx * cos - dy * sin - dx, dx * sin + dy * cos - dy);
}

void PhysicsConstraint::translate(float x, float y) {
_ux -= x;
_uy -= y;
_cx -= x;
_cy -= y;
}
12 changes: 12 additions & 0 deletions spine-cpp/spine-cpp/src/spine/Skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,15 @@ void Skeleton::setTime(float time) {
void Skeleton::update(float delta) {
_time += delta;
}

void Skeleton::physicsTranslate(float x, float y) {
for (int i = 0; i < (int)_physicsConstraints.size(); i++) {
_physicsConstraints[i]->translate(x, y);
}
}

void Skeleton::physicsRotate(float x, float y, float degrees) {
for (int i = 0; i < (int)_physicsConstraints.size(); i++) {
_physicsConstraints[i]->rotate(x, y, degrees);
}
}

0 comments on commit 922ac2b

Please sign in to comment.