void BulletEntity::Update(const float& deltaTime) { D3DXVECTOR2 oldPosition(mPosition); //更新子弹位置 this->mPosition.x += float(mVelocityVector.x) * deltaTime; this->mPosition.y += float(mVelocityVector.y) * deltaTime; this->m_pGame->pCellSpacePartition->CalculateNeighbors(this->Pos(), this->m_Boundary); for(GameEntity* pCurNeighbor = m_pGame->pCellSpacePartition->begin(); !(m_pGame->pCellSpacePartition->end()); pCurNeighbor = m_pGame->pCellSpacePartition->next()) { //不允许子弹撞子弹 if((pCurNeighbor != this) && (pCurNeighbor->m_EntityType != yellowshot) && (pCurNeighbor->m_Boundary + this->m_Boundary > (pCurNeighbor->Pos() - this->Pos()).Length())) { m_pGame->RecycleAAliveEntity(pCurNeighbor, pCurNeighbor->m_EntityType); this->mPosition = oldPosition; //正好跨越空间划分格击中时,必须找到以前位置并删除 m_pGame->RecycleAAliveEntity(this, this->m_EntityType); return; //保证一个子弹只可以击中一个目标 } } //if(this->isAlive) //检测子弹是否可用 //{ bool _onEdge = false; //是否越界 if(mPosition.y - m_Boundary < this->m_pGame->GetBoundary().TopLeft.y) _onEdge = true; else if(mPosition.y + m_Boundary > this->m_pGame->GetBoundary().BottomRight.y) _onEdge = true; else if(mPosition.x - m_Boundary < this->m_pGame->GetBoundary().TopLeft.x) _onEdge = true; else if(mPosition.x + m_Boundary > this->m_pGame->GetBoundary().BottomRight.x) _onEdge = true; if(_onEdge == true) { this->mPosition = oldPosition; //已经跨越空间划分,必须找到以前位置并删除 m_pGame->RecycleAAliveEntity(this, this->m_EntityType); } /*}*/ }
bool Particle::update(float deltaTime) { const Model* model = group->getModel(); data->age += deltaTime; if (!model->immortal) { // computes the ratio between the life of the particle and its lifetime float ratio = std::min(1.0f,deltaTime / data->life); data->life -= deltaTime; // updates mutable parameters for (size_t i = 0; i < model->nbMutableParams; ++i) { size_t index = model->mutableParams[i]; size_t enableIndex = model->particleEnableIndices[index]; currentParams[enableIndex] += (extendedParams[i] - currentParams[enableIndex]) * ratio; } } // updates interpolated parameters interpolateParameters(); // updates position oldPosition() = position(); position() += velocity() * deltaTime; // updates velocity velocity() += group->getGravity() * deltaTime; std::vector<Modifier*>::const_iterator end = group->activeModifiers.end(); for (std::vector<Modifier*>::const_iterator it = group->activeModifiers.begin(); it != end; ++it) (*it)->process(*this,deltaTime); if (group->getFriction() != 0.0f) velocity() *= 1.0f - std::min(1.0f,group->getFriction() * deltaTime / getParamCurrentValue(PARAM_MASS)); return data->life <= 0.0f; }
void ofxSVGParticle::update() { static int frame = 0; positionSteps.clear(); rotationSteps.clear(); scaleSteps.clear(); // Get spring-like attraction force ofVec3f a = (positionTarget - position) * springTension; if(a.lengthSquared() < 0.1 && mode == 1) { randomize(); update(); return; } // Get perlin noise force float noiseValue = (ofNoise((center.x + position.x) / 1000., (center.y + position.y) / 1000., frame / 1000.) * 2 - 1) * 15; ofVec3f fNoise(cos(noiseValue) * noiseScale, sin(noiseValue) * noiseScale); a += fNoise; positionVelocity += a; // Accelerate positionVelocity *= 0.9; // Drag ofVec3f oldPosition(position); position += positionVelocity * 0.1; float rotationDiff = rotationTarget - rotation; while(rotationDiff > M_PI) rotationDiff -= M_PI*2; while(rotationDiff < -M_PI) rotationDiff += M_PI*2; float rA = rotationDiff * 0.1; rA += noiseValue * noiseScale / 10000.0f; rotationVelocity += rA; rotationVelocity *= 0.8; float oldRotation = rotation; rotation += rotationVelocity; if(blur) { // Linear interpolation for motion blur steps // First, figure out how many steps are needed based on translation and rotation distance ofVec3f dP = position - oldPosition; float dR = rotation - oldRotation; blurSteps = (int)max((int)dP.length() / 2, (int)abs((ofRadToDeg(dR) / 4))); for(int i=blurSteps-1; i>=0; i--) { positionSteps.push_back(oldPosition + (dP * (1 + i/(float)blurSteps)) / 2); // 180 degree shutter angle //positionSteps.push_back(oldPosition + (dP * i/(float)blurSteps)); // 360 degree shutter angle rotationSteps.push_back(oldRotation + (dR * (1 + i/(float)blurSteps)) / 2); // 180 degree shutter angle //rotationSteps.push_back(oldRotation + (dR * i/(float)blurSteps)); // 360 degree shutter angle } } frame++; }
void DrawRoundedTriangle::execute(DrawStackArgList argList) { // set up geometry Vector2 position = *((Vector2*) argList["position"]); float rotation = *((float*) argList["rotation"]); Vector2 size = *((Vector2*) argList["size"]); float triangleHeight = cos(asin(size.x * gameGraphics->aspectRatio * 0.5f / size.y)) * size.y; float cutOutHeight = tan(asin(triangleHeight / size.y) - radians(45.0f)) * (size.x * gameGraphics->aspectRatio * 0.5f); std::vector<VertexEntry> triangleVertices; VertexEntry entry; entry.highlight = false; entry.concave = false; entry.curveOriginCoord = Vector2(0.0f, 0.0f); entry.position = Vector2(-size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); entry.primCoord = Vector2( cos(atan(triangleHeight / (size.x * (float) gameGraphics->aspectRatio / 2.0f)) + radians(90.0f)) * 2.0f, sin(atan(triangleHeight / (size.x * (float) gameGraphics->aspectRatio / 2.0f)) + radians(90.0f)) * 2.0f ); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, size.y / 2.0f); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f + (size.y - (triangleHeight - cutOutHeight))); entry.primCoord = Vector2( entry.primCoord.x / 2.0f * (2.0f - sin(atan(size.x * (float) gameGraphics->aspectRatio / 2.0f / triangleHeight)) * (triangleHeight - cutOutHeight) / size.y * 2.0f), entry.primCoord.y / 2.0f * (2.0f - sin(atan(size.x * (float) gameGraphics->aspectRatio / 2.0f / triangleHeight)) * (triangleHeight - cutOutHeight) / size.y * 2.0f) ); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, size.y / 2.0f); entry.primCoord = Vector2( -cos(atan(triangleHeight / (size.x * (float) gameGraphics->aspectRatio / 2.0f)) + radians(90.0f)) * 2.0f, sin(atan(triangleHeight / (size.x * (float) gameGraphics->aspectRatio / 2.0f)) + radians(90.0f)) * 2.0f ); triangleVertices.push_back(entry); entry.position = Vector2(size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f + (size.y - (triangleHeight - cutOutHeight))); entry.primCoord = Vector2( entry.primCoord.x / 2.0f * (2.0f - sin(atan(size.x * (float) gameGraphics->aspectRatio / 2.0f / triangleHeight)) * (triangleHeight - cutOutHeight) / size.y * 2.0f), entry.primCoord.y / 2.0f * (2.0f - sin(atan(size.x * (float) gameGraphics->aspectRatio / 2.0f / triangleHeight)) * (triangleHeight - cutOutHeight) / size.y * 2.0f) ); triangleVertices.push_back(entry); entry.curveOriginCoord = Vector2(0.0f, 1.0f); entry.position = Vector2(-size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); entry.primCoord = Vector2(-2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, 1.0f - 2.0f * cos(asin(size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y))); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f + (size.y - (triangleHeight - cutOutHeight))); entry.primCoord = Vector2(0.0f, 1.0f - 2.0f * (triangleHeight - cutOutHeight) / size.y); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f); entry.primCoord = Vector2(0.0f, -1.0f); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f); entry.primCoord = Vector2(0.0f, -1.0f); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f + (size.y - (triangleHeight - cutOutHeight))); entry.primCoord = Vector2(0.0f, 1.0f - 2.0f * (triangleHeight - cutOutHeight) / size.y); triangleVertices.push_back(entry); entry.position = Vector2(size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); entry.primCoord = Vector2(2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, 1.0f - 2.0f * cos(asin(size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y))); triangleVertices.push_back(entry); entry.position = Vector2(-size.x / 2.0f, -size.y / 2.0f); entry.primCoord = Vector2(-2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, -1.0f); triangleVertices.push_back(entry); entry.position = Vector2(-size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); entry.primCoord = Vector2(-2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, 1.0f - 2.0f * cos(asin(size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y))); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f); entry.primCoord = Vector2(0.0f, -1.0f); triangleVertices.push_back(entry); entry.position = Vector2(size.x / 2.0f, -size.y / 2.0f + (size.y - triangleHeight)); entry.primCoord = Vector2(2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, 1.0f - 2.0f * cos(asin(size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y))); triangleVertices.push_back(entry); entry.position = Vector2(size.x / 2.0f, -size.y / 2.0f); entry.primCoord = Vector2(2.0f * size.x * (float) gameGraphics->aspectRatio / 2.0f / size.y, -1.0f); triangleVertices.push_back(entry); entry.position = Vector2(0.0f, -size.y / 2.0f); entry.primCoord = Vector2(0.0f, -1.0f); triangleVertices.push_back(entry); // apply rotation Matrix4 rotationMatrix; rotationMatrix.identity(); scaleMatrix(gameGraphics->aspectRatio, 1.0f, 1.0f, rotationMatrix); rotateMatrix(Vector3(0.0f, 0.0f, 1.0f), radians(rotation), rotationMatrix); scaleMatrix(1.0f / gameGraphics->aspectRatio, 1.0f, 1.0f, rotationMatrix); for(int i = 0; i < triangleVertices.size(); ++i) { Vector4 oldPosition(triangleVertices[i].position.x, triangleVertices[i].position.y, 0.0f, 0.0f); oldPosition = oldPosition * rotationMatrix; triangleVertices[i].position = Vector2(oldPosition.x, oldPosition.y); } // update vertex buffers GLuint* triangleElementBufferArray = new GLuint[triangleVertices.size()]; for(size_t i = 0; i < triangleVertices.size(); ++i) triangleElementBufferArray[i] = i; size_t triangleVertexBufferArraySize = triangleVertices.size() * 8; GLfloat* triangleVertexBufferArray = new GLfloat[triangleVertexBufferArraySize]; for(size_t i = 0; i < triangleVertices.size(); ++i) { triangleVertexBufferArray[i * 8 + 0] = triangleVertices[i].position.x + position.x; triangleVertexBufferArray[i * 8 + 1] = triangleVertices[i].position.y + position.y; triangleVertexBufferArray[i * 8 + 2] = triangleVertices[i].primCoord.x; triangleVertexBufferArray[i * 8 + 3] = triangleVertices[i].primCoord.y; triangleVertexBufferArray[i * 8 + 4] = triangleVertices[i].curveOriginCoord.x; triangleVertexBufferArray[i * 8 + 5] = triangleVertices[i].curveOriginCoord.y; triangleVertexBufferArray[i * 8 + 6] = 2.0f - *((float*) argList["border"]) * 2.0f / (size.y / 2.0f * (float) gameGraphics->resolutionY); triangleVertexBufferArray[i * 8 + 7] = 2.0f; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBuffers["elements"]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangleVertices.size() * sizeof(GLuint), triangleElementBufferArray, GL_STREAM_DRAW); delete[] triangleElementBufferArray; glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers["vertices"]); glBufferData(GL_ARRAY_BUFFER, triangleVertexBufferArraySize * sizeof(GLfloat), triangleVertexBufferArray, GL_STREAM_DRAW); delete[] triangleVertexBufferArray; // state glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); if(gameGraphics->supportsMultisampling) glDisable(GL_MULTISAMPLE); glDisable(GL_SCISSOR_TEST); glDisable(GL_TEXTURE_2D); // enable shader glUseProgram(gameGraphics->getProgramID("hudContainer")); // set uniforms Vector4 insideColor = *((Vector4*) argList["insideColor"]); Vector4 borderColor = *((Vector4*) argList["borderColor"]); Vector4 outsideColor = *((Vector4*) argList["outsideColor"]); glUniform4f(glGetUniformLocation(gameGraphics->getProgramID("hudContainer"), "insideColor"), insideColor.x, insideColor.y, insideColor.z, insideColor.w); glUniform4f(glGetUniformLocation(gameGraphics->getProgramID("hudContainer"), "borderColor"), borderColor.x, borderColor.y, borderColor.z, borderColor.w); glUniform4f(glGetUniformLocation(gameGraphics->getProgramID("hudContainer"), "outsideColor"), outsideColor.x, outsideColor.y, outsideColor.z, outsideColor.w); glUniform1f(glGetUniformLocation(gameGraphics->getProgramID("hudContainer"), "softEdge"), *((float*) argList["softEdge"]) * 2.0f / (size.y / 2.0f * (float) gameGraphics->resolutionY)); // draw the data stored in GPU memory glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers["vertices"]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBuffers["elements"]); glVertexAttribPointer(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "position"), 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) 0); glVertexAttribPointer(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "primCoord"), 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) (2 * sizeof(GLfloat))); glVertexAttribPointer(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "curveOriginCoord"), 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) (4 * sizeof(GLfloat))); glVertexAttribPointer(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border1Dist"), 1, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) (6 * sizeof(GLfloat))); glVertexAttribPointer(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border2Dist"), 1, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) (7 * sizeof(GLfloat))); glEnableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "position")); glEnableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "primCoord")); glEnableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "curveOriginCoord")); glEnableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border1Dist")); glEnableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border2Dist")); glDrawElements(GL_TRIANGLES, triangleVertices.size(), GL_UNSIGNED_INT, NULL); glDisableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "position")); glDisableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "primCoord")); glDisableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "curveOriginCoord")); glDisableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border1Dist")); glDisableVertexAttribArray(glGetAttribLocation(gameGraphics->getProgramID("hudContainer"), "border2Dist")); }
/** * Rotates the a SimObjects in the list around the origin with the given orientation-vector. * @param simObjects The list of objects to be rotated. * @param orientationOffset The angles (in degree) about which each object is to be rotated. */ void Physics::rotateSimObjects(QList<SimObject*> simObjects, Vector3D orientationOffset) { Quaternion orientation; // while(orientationOffset.getX() > 180.0) { // orientationOffset.setX(orientationOffset.getX() - 360.0); // } // while(orientationOffset.getX() < -180.0) { // orientationOffset.setX(orientationOffset.getX() + 360.0); // } // while(orientationOffset.getY() > 180.0) { // orientationOffset.setY(orientationOffset.getY() - 360.0); // } // while(orientationOffset.getY() < -180.0) { // orientationOffset.setY(orientationOffset.getY() + 360.0); // } // while(orientationOffset.getZ() > 180.0) { // orientationOffset.setZ(orientationOffset.getZ() - 360.0); // } // while(orientationOffset.getZ() < -180.0) { // orientationOffset.setZ(orientationOffset.getZ() + 360.0); // } // cerr << "Got: " << orientationOffset.getX() << " " << orientationOffset.getY() << " " << orientationOffset.getZ() << endl; orientation.setFromAngles(orientationOffset.getX(), orientationOffset.getY(), orientationOffset.getZ()); Quaternion inverse = orientation.getInverse(); for(int i = 0; i < simObjects.size(); i++) { if(dynamic_cast<SimBody*>(simObjects.at(i)) != 0) { SimBody *obj = dynamic_cast<SimBody*>(simObjects.at(i)); Vector3DValue *bodyPosition = dynamic_cast<SimBody*>(simObjects.at(i))->getPositionValue(); Quaternion oldPosition(0, 0, 0, 0); oldPosition.set(0, bodyPosition->getX(), bodyPosition->getY(), bodyPosition->getZ()); Quaternion newPointQ = orientation * oldPosition * inverse; bodyPosition->set(newPointQ.getX(), newPointQ.getY(), newPointQ.getZ()); //Vector3DValue *bodyOrientation = obj->getOrientationValue(); QuaternionValue *currentOrientation = obj->getQuaternionOrientationValue(); Quaternion newOrientation = orientation * currentOrientation->get(); // Vector3D ori1 = newOrientation.toAngles(); // // cerr << "Got2: " << currentOrientation->getX() << " " << currentOrientation->getY() << " " << currentOrientation->getZ() << " " << currentOrientation->getW() << endl; // // cerr << "Got3: " << newOrientation.getX() << " " << newOrientation.getY() << " " << newOrientation.getZ() << " " << newOrientation.getW() << endl; // // cerr << "Got4: " << ori1.getX() << " " << ori1.getY() << " " << ori1.getZ() << endl; // Vector3D rot = orientationOffset; // Quaternion newOrientation = currentOrientation->get(); // bool tooLong = false; // while(rot.getZ() != 0) { // double x = 0; // double y = 0; // double z = 0; // if(rot.getZ() > 90.0) { // rot.setZ(rot.getZ() - 90.0); // z = 90.0; // tooLong = true; // } // else if(rot.getZ() < -90.0) { // rot.setZ(rot.getZ() + 90.0); // z = -90.0; // tooLong = true; // } // else { // z = rot.getZ(); // rot.setZ(0); // if(tooLong) { // z = -z; // } // } // Quaternion ori; // ori.setFromAngles(x, y, z); // // newOrientation = ori * newOrientation; // // } obj->getQuaternionOrientationValue()->set(newOrientation); continue; } else if(dynamic_cast<SimJoint*>(simObjects.at(i)) != 0) { SimJoint *joint = dynamic_cast<SimJoint*>(simObjects.at(i)); for(int j = 0; j < joint->getAxisPoints().size(); j++) { Vector3DValue *axisPointPosition = joint->getAxisPoints().at(j); Quaternion oldPosition(0, 0, 0, 0); oldPosition.set(0, axisPointPosition->getX(), axisPointPosition->getY(), axisPointPosition->getZ()); Quaternion newPointQ = orientation * oldPosition * inverse; axisPointPosition->set(newPointQ.getX(), newPointQ.getY(), newPointQ.getZ()); } continue; } else { continue; } } }