Rope: Add Pull functions

rope
Armin Burgmeier 2012-05-06 13:59:47 +02:00
parent 27d72b12ce
commit f54b471115
3 changed files with 41 additions and 2 deletions

View File

@ -328,7 +328,7 @@ bool C4RopeElement::SetForceRedirectionByLookAround(const C4Rope* rope, int ox,
C4Rope::C4Rope(C4PropList* Prototype, C4Object* first_obj, C4Object* second_obj, C4Real segment_length, C4DefGraphics* graphics):
C4PropListNumbered(Prototype), Width(5.0f), Graphics(graphics), SegmentCount(itofix(ObjectDistance(first_obj, second_obj))/segment_length),
l(segment_length), k(Fix1*3), mu(Fix1*3), eta(Fix1*3), NumIterations(10),
FrontAutoSegmentation(Fix0), BackAutoSegmentation(Fix0)
FrontAutoSegmentation(Fix0), BackAutoSegmentation(Fix0), FrontPull(Fix0), BackPull(Fix0)
{
if(!PathFree(first_obj->GetX(), first_obj->GetY(), second_obj->GetX(), second_obj->GetY()))
throw C4RopeError("Path between objects is blocked");
@ -577,10 +577,29 @@ void C4Rope::Execute()
DoAutoSegmentation(Front, Front->Next, FrontAutoSegmentation);
DoAutoSegmentation(Back, Back->Prev, BackAutoSegmentation);
// Compute forces
// Compute inter-rope forces
for(C4RopeElement* cur = Front; cur->Next != NULL; cur = cur->Next)
Solve(cur, cur->Next);
// Front/BackPull
if(FrontPull != Fix0)
{
const C4Real dx = Front->Next->GetX() - Front->GetX();
const C4Real dy = Front->Next->GetY() - Front->GetY();
const C4Real d = ftofix(sqrt(fixtof(dx*dx+dy*dy)));
if(d != Fix0)
Front->Next->AddForce(-dx/d * FrontPull, -dy/d * FrontPull);
}
if(BackPull != Fix0)
{
const C4Real dx = Back->Prev->GetX() - Back->GetX();
const C4Real dy = Back->Prev->GetY() - Back->GetY();
const C4Real d = ftofix(sqrt(fixtof(dx*dx+dy*dy)));
if(d != Fix0)
Back->Prev->AddForce(-dx/d * BackPull, -dy/d * BackPull);
}
// Apply forces
for(C4RopeElement* cur = Front; cur != NULL; cur = cur->Next)
cur->Execute(this, dt);

View File

@ -91,6 +91,9 @@ public:
bool GetBackFixed() const { return Back->Fixed; }
void SetFrontFixed(bool fixed) { Front->Fixed = fixed; }
void SetBackFixed(bool fixed) { Back->Fixed = fixed; }
void PullFront(C4Real f) { FrontPull = f; }
void PullBack(C4Real f) { BackPull = f; }
private:
C4Real GetL(const C4RopeElement* prev, const C4RopeElement* next) const;
@ -116,6 +119,9 @@ private:
C4Real FrontAutoSegmentation;
C4Real BackAutoSegmentation;
C4Real FrontPull;
C4Real BackPull;
};
class C4RopeAul: public C4AulScript

View File

@ -71,6 +71,18 @@ static C4Void FnSetBackFixed(C4AulContext* Context, bool fixed)
return C4Void();
}
static C4Void FnPullFront(C4AulContext* Context, int force)
{
static_cast<C4Rope*>(Context->Def)->PullFront(itofix(force));
return C4Void();
}
static C4Void FnPullBack(C4AulContext* Context, int force)
{
static_cast<C4Rope*>(Context->Def)->PullBack(itofix(force));
return C4Void();
}
C4RopeAul::C4RopeAul():
RopeDef(NULL)
{
@ -98,5 +110,7 @@ void C4RopeAul::InitFunctionMap(C4AulScriptEngine* pEngine)
::AddFunc(this, "SetBackAutoSegmentation", FnSetBackAutoSegmentation);
::AddFunc(this, "SetFrontFixed", FnSetFrontFixed);
::AddFunc(this, "SetBackFixed", FnSetBackFixed);
::AddFunc(this, "PullFront", FnPullFront);
::AddFunc(this, "PullBack", FnPullBack);
RopeDef->Freeze();
}