void Body::Update(float dt) { //------------------------------------------------------- // Integrate position (verlet integration) //------------------------------------------------------- Point2f xAccel = netForce * invMass; pos += velocity * dt;// + 0.5f * xAccel * dt*dt; orientation += angVelocity * dt; assert(xAccel.Length() < 1e3); //------------------------------------------------------- // Integrate velocity (implicit linear velocity) //------------------------------------------------------- velocity += xAccel * dt; angVelocity += netTorque * (invInertia * dt); //------------------------------------------------------- // clear forces //------------------------------------------------------- netForce = Point2f(0.0f, 0.0f); netTorque = 0.0f; if (shape) shape->update(); }
void TargetIndicator::GraphicEntity::render(float gameTime, float deltaTime) { int frameTime = int(gameTime * 60); const Rectf viewerRange(-379, 379, -279, 279); // slightly smaller than windowViewRange const Point2f &windowSize = iRenderQueue::getSingleton().getWindowSize(); const Rectf windowRange(0, windowSize.x, 0, windowSize.y); const Rectf windowViewRange(windowRange.leftTop.x + 20, windowRange.rightBottom.x - 20, windowRange.leftTop.y + 20, windowRange.rightBottom.y - 20); const Point2f posOffset(windowRange.rightBottom / 2); const Point2f viewerPos = iRenderQueue::getSingleton().getViewerPos() + posOffset; Point2f pos; deltaTime; for (vector<iAttacheeEntity*>::const_iterator ie = getEntity().entities.begin(); ie != getEntity().entities.end(); ++ie) { if (((iTargetEntity*)*ie)->getEntity().getStatus(ESI_Position, &pos)) { Point2f dir = pos - viewerPos; if (viewerRange & dir) continue; if (dir.Length() > 500) continue; float direction = -atan2(dir.y, dir.x); if (fabs(dir.x) > 1e-5) { Point2f pos = dir * (viewerRange.rightBottom.x / fabs(dir.x)) + posOffset; if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); else { pos = dir * (viewerRange.rightBottom.y / fabs(dir.y)) + posOffset; //if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); } } else { assert(fabs(dir.y) > 1e-5); Point2f pos = dir * (viewerRange.rightBottom.y / fabs(dir.y)) + posOffset; if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); else { pos = dir * (viewerRange.rightBottom.x / fabs(dir.x)) + posOffset; //if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); } } } } }
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 &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); }