📄 main.cpp
字号:
double f;
if(pPattern[i].LimitHeadingChange)
f = 1 - PatternTracking.dHeading / pPattern[i].dHeadingLimit;
else
f = 1;
if(f < 0.05) f = 0.05;
Craft2.SetThrusters(pPattern[i].PThrusterActive, pPattern[i].SThrusterActive, f);
return true;
}
void DrawCraft(RigidBody2D craft, COLORREF clr)
{
Vector vList[5];
double wd, lg;
int i;
Vector v1;
wd = craft.fWidth;
lg = craft.fLength;
vList[0].y = lg/2; vList[0].x = wd/2;
vList[1].y = -lg/2; vList[1].x = wd/2;
vList[2].y = -lg/2; vList[2].x = -wd/2;
vList[3].y = lg/2; vList[3].x = -wd/2;
vList[4].y = lg/2*1.5; vList[4].x = 0;
for(i=0; i<5; i++)
{
v1 = VRotate2D(craft.fOrientation, vList[i]);
vList[i] = v1 + craft.vPosition;
}
DrawLine(vList[0].x, vList[0].y, vList[1].x, vList[1].y, 2, clr);
DrawLine(vList[1].x, vList[1].y, vList[2].x, vList[2].y, 2, clr);
DrawLine(vList[2].x, vList[2].y, vList[3].x, vList[3].y, 2, clr);
DrawLine(vList[3].x, vList[3].y, vList[4].x, vList[4].y, 2, clr);
DrawLine(vList[4].x, vList[4].y, vList[0].x, vList[0].y, 2, clr);
if(ShowVectors)
{
Vector v, u;
double f = 5;
// Show velocity vectors in green
DrawLine(craft.vPosition.x, craft.vPosition.y, craft.vPosition.x+craft.vVelocity.x, craft.vPosition.y+craft.vVelocity.y, 3, RGB(0,255,0));
// Show force vectors in black
// thrust vector
v.x = 0;
v.y = craft.ThrustForce*f;
v = VRotate2D(craft.fOrientation, v);
u.x = craft.CT.x;
u.y = craft.CT.y;
u = VRotate2D(craft.fOrientation, u);
DrawLine(craft.vPosition.x+u.x, craft.vPosition.y+u.y, craft.vPosition.x + u.x + v.x, craft.vPosition.y + u.y + v.y, 1, RGB(0,0,0));
// port steering force
v.x = craft.PThrust.x*f;
v.y = craft.PThrust.y*f;
v = VRotate2D(craft.fOrientation, v);
u.x = craft.CPT.x;
u.y = craft.CPT.y;
u = VRotate2D(craft.fOrientation, u);
DrawLine(craft.vPosition.x+u.x, craft.vPosition.y+u.y, craft.vPosition.x + u.x + v.x, craft.vPosition.y + u.y + v.y, 1, RGB(0,0,0));
// stbd steering force
v.x = craft.SThrust.x*f;
v.y = craft.SThrust.y*f;
v = VRotate2D(craft.fOrientation, v);
u.x = craft.CST.x;
u.y = craft.CST.y;
u = VRotate2D(craft.fOrientation, u);
DrawLine(craft.vPosition.x+u.x, craft.vPosition.y+u.y, craft.vPosition.x + u.x + v.x, craft.vPosition.y + u.y + v.y, 1, RGB(0,0,0));
// applied force
v.x = craft.Fa.x*f;
v.y = craft.Fa.y*f;
v = VRotate2D(craft.fOrientation, v);
u.x = craft.Pa.x;
u.y = craft.Pa.y;
u = VRotate2D(craft.fOrientation, u);
DrawLine(craft.vPosition.x+u.x, craft.vPosition.y+u.y, craft.vPosition.x + u.x + v.x, craft.vPosition.y + u.y + v.y, 1, RGB(0,0,0));
}
}
void DoCraft2Chase(void)
{
Vector u, v;
bool p = false;
bool s = false;
u = VRotate2D(-Craft2.fOrientation, (Craft1.vPosition - Craft2.vPosition));
u.Normalize();
Target = Craft1.vPosition;
if(u.x < -_TOL)
p = true;
else if(u.x > _TOL)
s = true;
Craft2.SetThrusters(p,s, 1);
}
void DoCraft2Evade(void)
{
Vector u, v;
bool p = false;
bool s = false;
u = VRotate2D(-Craft2.fOrientation, (Craft1.vPosition - Craft2.vPosition));
u.Normalize();
if(u.x > 0)
p = true;
else if(u.x < 0)
s = true;
Craft2.SetThrusters(p,s, 1);
Target = Craft2.vPosition;
}
void DoCraft2Intercept(void)
{
Vector u1, u2, u;
Vector s1, s2;
Vector Vr;
double t1, t2;
Vector s1unit, s2unit;
bool p = false;
bool s = false;
Vr = Craft1.vVelocity - Craft2.vVelocity;
s2 = GetVelocityIntersection() - Craft2.vPosition;
s2unit = s2;
s2unit.Normalize();
u2 = VRotate2D(-Craft2.fOrientation, s2);
t2 = s2.Magnitude()/(Vr * s2unit);
s1 = Craft1.vPosition - Craft2.vPosition;
s1unit = s1;
s1unit.Normalize();
u1 = VRotate2D(-Craft2.fOrientation, s1);
t1 = s1.Magnitude()/(Vr * s1unit);
if(t1 < 0.0)
{
u = u2;
Target = s2 + Craft2.vPosition;
} else if(t2 < 0.0) {
u = u1;
Target = s1 + Craft2.vPosition;
} else if(t2 < t1)
{
u = u2;
Target = s2 + Craft2.vPosition;
} else {
u = u1;
Target = s1 + Craft2.vPosition;
}
u.Normalize();
if(u.x < -_TOL)
p = true;
else if(u.x > _TOL)
s = true;
Craft2.SetThrusters(p,s, 1);
}
/*Old version
void DoCraft2Intercept(void)
{
Vector u1, u2, u3, u;
Vector s1, s2, s3;
Vector Vr1, Vr2, Vr3;
double t1, t2, t3;
bool p = false;
bool s = false;
// todo: change variable subscripts to be consistent with craft #...
s3 = Craft1.vPosition - Craft2.vPosition;
u3 = VRotate2D(-Craft2.fOrientation, s3);
if(u3.y < -_TOL)
{
if(GetRandomNumber(0, 10, true) < 5)
p = true;
else
s = true;
Craft2.SetThrusters(p,s);
Target = Craft2.vPosition;
return;
}
Vr1 = Craft1.vVelocity - Craft2.vVelocity;
Vr2 = Craft1.vVelocity - Craft2.vVelocity;
s1 = GetVelocityIntersection() - Craft2.vPosition;
u1 = VRotate2D(-Craft2.fOrientation, s1);
t1 = s1.Magnitude()/(Vr1 * s1);
s2 = Craft1.vPosition - Craft2.vPosition;
u2 = VRotate2D(-Craft2.fOrientation, s2);
t2 = s2.Magnitude()/(Vr2 * s2);
if(t1 < t2)
{
u = u1;
Target = s1 + Craft2.vPosition;
} else {
u = u2;
Target = s2 + Craft2.vPosition;
}
u.Normalize();
if(u.x < -_TOL)
p = true;
else if(u.x > _TOL)
s = true;
else if(u.x == _TOL) {
if(GetRandomNumber(0, 10, true) < 5)
p = true;
else
s = true;
}
Craft2.SetThrusters(p,s);
}*/
void DoCraft2InterceptAlt(void)
{
Vector u;
Vector s1, s2, s12;
bool p = false;
bool s = false;
double tClose;
Vector Vr12;
double vr;
// turn around if we get ahead of the prey...
s12 = Craft1.vPosition - Craft2.vPosition;
u = VRotate2D(-Craft2.fOrientation, s12);
if(u.y < -_TOL)
{
//if(GetRandomNumber(0, 10, true) < 5)
p = true;
//else
// s = true;
Craft2.SetThrusters(p,s, 1);
Target = Craft2.vPosition;
return;
}
Vr12 = Craft1.vVelocity-Craft2.vVelocity; // closing velocity
s12 = Craft1.vPosition - Craft2.vPosition; // range to close
tClose = s12.Magnitude() / Vr12.Magnitude(); // time to close
s1 = Craft1.vPosition + (Craft1.vVelocity * tClose);
Target = s1;
s2 = s1 - Craft2.vPosition;
u = VRotate2D(-Craft2.fOrientation, s2);
u.Normalize();
if(u.x < -_TOL)
p = true;
else if(u.x > _TOL)
s = true;
Craft2.SetThrusters(p,s, 1);
}
void DoAttractCraft2(void)
{
// Apply Leonard-Jones potential force to Craft2
// todo: make sure rigidbody calcloads function handles it
Vector r = Craft2.vPosition - Craft1.vPosition;
Vector u = r;
u.Normalize();
double k1 = 0.5e2; // repel
double k2 = 1.0e2; // attract
Craft2.Fa = VRotate2D( -Craft2.fOrientation, ((k1*pow(Craft2.fLength*5/r.Magnitude(), 4) - k2*pow(Craft2.fLength*3/r.Magnitude(), 2)) ) * u);
Craft2.Pa.x = 0;
Craft2.Pa.y = Craft2.fLength / 2;
Target = Craft1.vPosition;
}
Vector GetVelocityIntersection(void)
{
double s, t, num, denom;
Vector a,b,c,d;
a = Craft1.vPosition;
b = a+Craft1.vVelocity;
c = Craft2.vPosition;
d = c+Craft2.vVelocity;
denom = a.x * (d.y-c.y) +
b.x * (c.y-d.y) +
d.x * (b.y-a.y) +
c.x * (a.y-b.y);
if(denom == 0)
return Vector(a.x, a.y, 0);
num = a.x * (d.y-c.y) +
c.x * (a.y-d.y) +
d.x * (c.y-a.y);
s = num/denom;
num = -( a.x * (c.y-b.y) +
b.x * (a.y-c.y) +
c.x * (b.y-a.y) );
t = num/denom;
if( (s >= 0) && (t >= 0) )
return Vector(a.x+s*(b.x-a.x), a.y+s*(b.y-a.y),0);
else
return Vector(a.x, a.y, 0);
}
int GetRandomNumber(int min, int max, bool seed)
{
int number;
if(seed)
srand( (unsigned)time( NULL ) );
number = (((abs(rand())%(max-min+1))+min));
if(number>max)
number = max;
if(number<min)
number = min;
return number;
}
void DoCraft2ModulateThrust(void)
{
Vector r = Craft1.vPosition - Craft2.vPosition;
double dmax = Craft2.fLength * 10;
if((Craft2.PThrust.Magnitude() > 0) || (Craft2.SThrust.Magnitude() > 0)) // turning
{
if(r.Magnitude() > dmax)
Craft2.ThrustForce = _MAXTHRUST;
else
Craft2.ThrustForce = r.Magnitude() / dmax * _MAXTHRUST;
} else {
// todo: check how close we are to target and adjust speed to stay with it
Craft2.ThrustForce = _MAXTHRUST;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -