void Bullet::step(float gameTime, float deltaTime) { Point2f delta = dest_pos - pos; float len; if (!bExplode) { len = delta.DotProduct(); if (len < 1) { bExplode = false; explodePos = pos; Explode(gameTime); return; } } else { bExplode = false; Explode(gameTime); return; } Point2f vel = delta.Normalize(min(sqrtf(len) / deltaTime, speed));// 可能引发爆发事件 collisionEntity.getBody().setVelocity(vel); collisionEntity.getBody().Update(deltaTime); }
void Player::step(float gameTime, float deltaTime) { gameTime, deltaTime; Point2f dir = Point2f(400, 300) - pos; float speed = 1 * MainGameState::getSingleton().getTimeScale(); dir.Normalize(speed); pos += dir; }
std::vector<int> ptsubsetBetween2Line( std::vector<Point2f> points2d, Point2f p0, Point2f p1, Point2f t0){ t0.Normalize() ; std::vector<int> res ; for( int i=0; i<points2d.size(); ++i ){ Point2f p0p1 = p1-p0 ; if( p0p1 * (points2d[i]-p1) <= 0 && !ReconstructorUtility::vectorIntersect(p0-t0*100, p0+t0*100, points2d[i], p1 ) ) res.push_back( i ) ; } return res ; }
void AiTankController::stepMoveTo() { if (!isAttached()) { bCheckMoveTo = false; return; } Point2f posTank; float orientionTank; Entity &e = entity->getEntity(); if (e.getStatus(Entity::ESI_Position, &posTank) && e.getStatus(Entity::ESI_Orientation, &orientionTank)) { //#ifndef NDEBUG iRenderQueue::getSingleton().render(moveToPos, posTank, ARGB(255, 0, 0, 0)); //#endif Point2f dir = moveToPos - posTank; float len = dir.Length(); float len2 = (nextMoveToPos - posTank).Length(); if (len < 10 || len > len2) { bCheckMoveTo = false; bForward = false; sendCommand(Tank::TCI_Forward, (void *)bForward); return; } dir.Normalize(); if (fabs(dir ^ Point2f(cos(orientionTank), sin(orientionTank))) < 0.3f) { if (!bForward) { bForward = true; sendCommand(Tank::TCI_Forward, (void *)bForward); } } else { float lerpValue = (float)rand() / RAND_MAX; lerpValue *= lerpValue; turnTo(moveToPos);//lerp(moveToPos, nextMoveToPos, lerpValue)); if (bForward) { bForward = false; sendCommand(Tank::TCI_Forward, (void *)bForward); } } } else { bCheckMoveTo = false; } }
size_t collide(CircleShape &circle, BoxShape &box, float dt, Contact *pxContacts, size_t numMaxContacts) { dt, pxContacts, numMaxContacts; const Point2f &p1 = circle.body.getPosition(); const Point2f &p2 = box.body.getPosition(); // work in the box's coordinate system const Point2f kDiff = p1 - p2; // compute squared distance and closest point on box float fSqrDistance = 0.0f, fDelta; Point2f kClosest(kDiff * box.GetDir(0), kDiff * box.GetDir(1)); const Point2f &extents = box.GetExtents(); if (kClosest.x < -extents.x) { fDelta = kClosest.x + extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = -extents.x; } else if (kClosest.x > extents.x) { fDelta = kClosest.x - extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = extents.x; } if (kClosest.y < -extents.y) { fDelta = kClosest.y + extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = -extents.y; } else if (kClosest.y > extents.y) { fDelta = kClosest.y - extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = extents.y; } if (fSqrDistance > circle.GetRadius() * circle.GetRadius()) { return 0; } Point2f d = p2 + kClosest - p1; d.Normalize(); pxContacts[0] = Contact(p1 + d * circle.GetRadius(), p2 + kClosest, &circle.body, &box.body); return 1; }
void Bullet::Explode(float gameTime) { Explosive *e = new Explosive(graphicEntity.animResManager); Point2f delta = dest_pos - pos; float len = delta.DotProduct(); if (len > 1) { delta = delta.Normalize(speed); } e->orientation = orientation; e->init("data/tank/bulletexplode.xml", gameTime, gun->explosive); e->setPostion(explodePos, explodePos + delta); EntityManager::getSingleton().attach(e); active = false; }
size_t collide(CircleShape &circle1, CircleShape &circle2, float dt, Contact *pxContacts, size_t numMaxContacts) { dt, numMaxContacts; float rR = circle1.GetRadius() + circle2.GetRadius(); const Point2f &p1 = circle1.body.getPosition(); const Point2f &p2 = circle2.body.getPosition(); Point2f d = p2 - p1; float len = d.Length(); if (len > rR) { return 0; } d.Normalize(); pxContacts[0] = Contact(p1 + d * circle1.GetRadius(), p1 + d * (len - circle1.GetRadius()), &circle1.body, &circle2.body); return 1; }
void Player::Attack() { Enemy *pEnemy = MainGameState::getSingleton().getNearestEnemy(pos); if (!pEnemy) return; Point2f dir = pEnemy->pos - pos; float len = dir.Length(); if (len > attackRange) { MainGameState::getSingleton().pauseGame(0.5f, 2.0f); return; } //bAttack = true; dir.Normalize(); pos = pEnemy->pos - dir * 20 * MainGameState::getSingleton().getTimeScale(); pEnemy->hit(pos); MainGameState::getSingleton().pauseGame(0.5f, 0.1f); }
void Contact::ResolveCollision() { if (!m_pxBodies[0] || !m_pxBodies[1]) return; //------------------------------------------------------------------------------------------------------ // parameters //------------------------------------------------------------------------------------------------------ Point2f C0 = m_xContacts[0]; Point2f C1 = m_xContacts[1]; Point2f Ncoll = C1 - C0; // assert(Ncoll.x != 0 || Ncoll.y != 0); Ncoll.Normalize(); float m0 = m_pxBodies[0]->getInvMass(); float m1 = m_pxBodies[1]->getInvMass(); float i0 = m_pxBodies[0]->getInvInertia(); float i1 = m_pxBodies[1]->getInvInertia(); const Point2f& P0 = m_pxBodies[0]->getPosition(); const Point2f& P1 = m_pxBodies[1]->getPosition(); const Point2f& V0 = m_pxBodies[0]->getVelocity(); const Point2f& V1 = m_pxBodies[1]->getVelocity(); float w0 = m_pxBodies[0]->getAngVelocity(); float w1 = m_pxBodies[1]->getAngVelocity(); //------------------------------------------------------------------------------------------------------ // pre-computations //------------------------------------------------------------------------------------------------------ Point2f R0 = C0 - P0; Point2f R1 = C1 - P1; Point2f T0 = Point2f(-R0.y, R0.x); Point2f T1 = Point2f(-R1.y, R1.x); Point2f VP0 = V0 + T0 * w0; // point velocity Point2f VP1 = V1 + T1 * w1; // point velocity //------------------------------------------------------------------------------------------------------ // impact velocity //------------------------------------------------------------------------------------------------------ Point2f Vcoll = VP0 - VP1; float vn = Vcoll * Ncoll; Point2f Vn = Ncoll * vn; Point2f Vt = Vcoll - Vn; if (vn > 0.0f) // separation return; if (Vt * Vt < 0.0001f) Vt = Point2f(0.0f, 0.0f); // float vt = Vt.Length(); Vt.Normalize(); //------------------------------------------------------------------------------------------------------ // compute impulse (frction and restitution). // ------------------------------------------ // // -(1+Cor)(Vel.norm) // j = ------------------------------------------------------------ // [1/Ma + 1/Mb] + [Ia' * (ra x norm)²] + [Ib' * (rb x norm)²] //------------------------------------------------------------------------------------------------------ Point2f J; Point2f Jt(0.0f, 0.0f); Point2f Jn(0.0f, 0.0f); float fCoR = s_xContactMaterial.GetRestitution(); float fCoF = s_xContactMaterial.GetFriction(); float t0 = (R0 ^ Ncoll) * (R0 ^ Ncoll) * i0; float t1 = (R1 ^ Ncoll) * (R1 ^ Ncoll) * i1; float m = m0 + m1; float denom = m + t0 + t1; float jn = vn / denom; Jn = Ncoll * (-(1.0f + fCoR) * jn); //if (dbg_UseFriction) Jt = Vt * (fCoF * jn); J = Jn + Jt; //------------------------------------------------------------------------------------------------------ // changes in momentum //------------------------------------------------------------------------------------------------------ Point2f dV0 = J * m0; Point2f dV1 =-J * m1; float dw0 = (R0 ^ J) * i0; float dw1 =-(R1 ^ J) * i1; //------------------------------------------------------------------------------------------------------ // apply changes in momentum //------------------------------------------------------------------------------------------------------ if (!m_pxBodies[0]->isStaticBody()) { if (m0 > 0.0f) { m_pxBodies[0]->setVelocity(V0 + dV0); } if (m0 > 0.0f) { m_pxBodies[0]->setAngVelocity(w0 + dw0); } } if (!m_pxBodies[1]->isStaticBody()) { if (m1 > 0.0f) { m_pxBodies[1]->setVelocity(V1 + dV1); } if (m1 > 0.0f) { m_pxBodies[1]->setAngVelocity(w1 + dw1); } } // Render(); return; /* //------------------------------------------------------------------------------------------------------ // Check for static frcition //------------------------------------------------------------------------------------------------------ float fRestingContactVelocity = 1.0f; if (-vn < fRestingContactVelocity) { if (vt < fRestingContactVelocity * fCoF) { //------------------------------------------------------------------------------------------------------ // Cancel tangential velocity on the two bodies, so they stick //------------------------------------------------------------------------------------------------------ Point2f dV = V1 - V0; dV -= Ncoll * (dV * Ncoll); if (m0 > 0.0f) V0 += dV * (m0 / m + 0.01f); if (m1 > 0.0f) V1 -= dV * (m1 / m + 0.01f); } } */ }