Apply gravity to rope segments

rope
Armin Burgmeier 2012-04-28 00:06:32 +02:00
parent b83cc3c6d2
commit 745ab91665
2 changed files with 10 additions and 6 deletions

View File

@ -27,6 +27,9 @@ namespace
C4Real dy = second->fix_y - first->fix_y;
return ftofix(sqrt(fixtof(dx*dx + dy*dy))); // TODO: Replace by integer sqrt
}
C4Object* GetObject(C4RopeSegment* segment) { return NULL; }
C4Object* GetObject(C4RopeEnd* end) { return end->GetObject(); }
}
C4RopeSegment::C4RopeSegment(C4Real x, C4Real y, C4Real m):
@ -162,12 +165,9 @@ void C4Rope::Solve(TRopeType1* prev, TRopeType2* next) //C4RopeSegment* prev, C4
fx += -(prev->GetVx() - next->GetVx()) * rho;
fy += -(prev->GetVy() - next->GetVy()) * rho;
// TODO: Don't apply gravity to objects, it is applied automatically.
fy += ::Landscape.Gravity;
// Apply forces to masses
prev->AddForce(-fx, -fy);
next->AddForce(fx, fy);
// Apply forces to masses. Don't apply gravity to objects since it's applied already in C4Object execution.
prev->AddForce(-fx, -fy + GetObject(prev) ? prev->GetMass() * ::Landscape.Gravity : Fix0);
next->AddForce(fx, fy + GetObject(next) ? next->GetMass() * ::Landscape.Gravity : Fix0);
}
void C4Rope::Execute()

View File

@ -39,6 +39,7 @@ public:
C4Real GetY() const { return y; }
C4Real GetVx() const { return vx; }
C4Real GetVy() const { return vy; }
C4Real GetMass() const { return m; }
void AddForce(C4Real x, C4Real y);
@ -63,9 +64,12 @@ public:
C4Real GetY() const { return has_object ? obj->fix_y : end.y; }
C4Real GetVx() const { return has_object ? obj->xdir : end.vx; }
C4Real GetVy() const { return has_object ? obj->ydir : end.vy; }
C4Real GetMass() const { return has_object ? itofix(obj->Mass) : end.m; }
C4Object* GetObject() const { return has_object ? obj : NULL; }
void AddForce(C4Real fx, C4Real fy);
void Execute(C4Real dt);
private:
C4RopeSegment* segment;