void CAnimatedTexture::SetFrame(unsigned int newframe) { if (num_frames == 0) return; curr_frame = newframe % num_frames; // Make sure frames wrap around UpdateCoords(); }
void CAnimatedTexture::Think() { static bool first = true; // float last_time ; if (!animating) // If we arn't animating don't do anything return; /* if (num_frames == 0) // If we havn't calculated the number of frames yet // Calculate number of frames in animation // Assumes frame_width and frame_height have been set { int frames_in_row = GetWidth() / frame_width; int frames_in_column = GetHeight() / frame_height; num_frames = frames_in_row * frames_in_column; } */ // Calulcate time difference float tdiff = (float)(currentTime - last_time) / (float)CLOCKS_PER_SEC; float test_time = 1/float(fps); changed = false; // Only update frame according to sprite FPS setting if (!first) if (tdiff < test_time) return; first = false; last_time = currentTime; // Record current time curr_frame++; // Next frame curr_frame %= num_frames; // Make sure frames wrap around UpdateCoords(); }
////////////////////////////////////////////////////////////////////////////////////////// // Detects all collisions of various types with each object. void PerformCollisionDetection(GameRoom* room, GamePlayer* player, double dt, float xScale, float yScale, float zNear, float zFar){ dt/=1000; vector<GameObject*> Objects = room->GetGameObjects(); PSystems* ps;// = Render::gameState->GetParticleSystems(); list<Projectile*>* projs = ps->GetBullets(); ///////////////////////////////////////////////////////////////////////////// // PARTICLE COLLISION DETECTION ///////////////////////////////////////////////////////////////////////////// for(unsigned int i = 0; i<Objects.size(); i++){ GameObject* o = Objects[i]; if(o->CollisionTierNum<1) continue; vector<Vec3f> aBox = o->boundingBox; Vector3f pos = o->GetPosition(); Vec3f p(pos.x(), pos.y(), pos.z()); Vec4f r = o->GetRotation(); Vec4f wv = o->angularVelocity; Vec3f v = o->velocity; UpdateCoords(aBox, p, v, r, wv, 0, 1.f, true, 1.0, xScale, yScale, zNear, zFar); for(list<Projectile*>::iterator it = projs->begin(); it != projs->end(); ++it){ vector<Vec3f> bBox = (*(*it)).boundingBox; Vector3f pos2 = (*(*it)).getPosition(); Vec3f p2(pos.x(), pos.y(), pos.z()); Vec4f r2(0,0,1,0); Vec4f wv2(1,0,0,0); Vector3f vel2 = (*(*it)).getVelocity(); Vec3f v2(vel2.x(), vel2.y(), vel2.z()); UpdateCoords(bBox, p2, v2, r2, wv2, 0, 10.f, true, 1.0, xScale, yScale, zNear, zFar); Simplex P; Vec3f V; V = GJKDistance(aBox, bBox, P); if(P.GetSize() > 3 || V.norm() < tolerance){ //We have a collision Vec3f contact = ResolveContact(P); o->collidedProjectiles.push_back((*it)); CollisionData o1D, o2D; o1D.pointOfContact = contact; o1D.contactNormal = V.normalized(); o->projectileCollisionData[(*it)]=o1D; (*it)->drawCollision = true; } } } //////////////////////////////////////////////////////////////////////////// // SPECIAL DATA FOR MAIN CHARACTER ONLY //////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////// // Tier 0 collision ////////////////////////////////////////////////// for(unsigned int a = 0; a<Objects.size()-1; a++){ GameObject* o1 = Objects[a]; if(o1->CollisionTierNum < 1 )continue; vector<Vec3f> aBox = o1->boundingBox; Vector3f pos = o1->GetPosition(); Vec3f position = Vec3f(pos.x(), pos.y(), pos.z()); Vec4f rotation = o1->GetRotation(); Vec3f vel = o1->velocity; Vec4f wvel = o1->angularVelocity; UpdateCoords(aBox, position, vel, rotation, wvel ,dt, o1->outSideCollisionScale, true, 1.f, xScale, yScale, zNear, zFar); vector<Vec3f> oldABox = o1->boundingBox; UpdateCoords(oldABox, position, vel, rotation, wvel,0, o1->outSideCollisionScale, true, 1.f, xScale, yScale, zNear, zFar); for(unsigned int i = 0; i <oldABox.size(); i++){ aBox.push_back(oldABox[i]); } for(unsigned int b = a+1; b<Objects.size(); b++){ GameObject* o2 = Objects[b]; if(o1->objType == WORLD_OBJECT_TYPE && o2->objType == WORLD_OBJECT_TYPE || o2->CollisionTierNum < 1){ continue; } vector<Vec3f> bBox = o2->boundingBox; pos= o2->GetPosition(); position = Vec3f(pos.x(), pos.y(), pos.z()); vel = o2->velocity; rotation = o2->GetRotation(); wvel = o2->angularVelocity; UpdateCoords(bBox, position, vel, rotation, wvel,dt, o2->outSideCollisionScale, true, 1.f, xScale, yScale, zNear, zFar); vector<Vec3f> oldBBox = o2->boundingBox; UpdateCoords(oldBBox, position, vel, rotation, wvel,0, o2->outSideCollisionScale,true, 1.f, xScale, yScale, zNear, zFar); for(unsigned int i = 0; i <oldBBox.size(); i++){ bBox.push_back(oldBBox[i]); } Simplex P; Vec3f V; V = GJKDistance(aBox, bBox, P); if(V.norm() < tolerance){ //We have a collision if(!AlreadyIn(o1, o2, 0, room)){ Vec3f contact = ResolveContact(P); room->collisionTier0List[o1].push_back(o2); room->collisionTier0List[o2].push_back(o1); CollisionData o1D, o2D; o1D.pointOfContact = contact; o2D.pointOfContact = contact; o1D.contactNormal = V.normalized(); o2D.contactNormal = -V.normalized(); o1->tier0CollisionData[o2] = o1D; o2->tier0CollisionData[o1] = o2D; } } } } //////////////////////////////////////////////// // Tier 1 collision /////////////////////////////////////////////// map<GameObject*, vector<GameObject*> >::iterator it = room->collisionTier0List.begin(); while(it!=room->collisionTier0List.end()){ GameObject* o1 = it->first; if(o1->CollisionTierNum<2) continue; Vector3f pos = o1->GetPosition(); Vec3f position(pos.x(), pos.y(), pos.z()); Vec4f rotation = o1->GetRotation(); Vec3f vel = o1->velocity; Vec4f wvel = o1->angularVelocity; vector<Vec3f> aBox = o1->boundingBox; UpdateCoords(aBox,position, vel, rotation, wvel ,0, 1.0,true, 1.f, xScale, yScale, zNear, zFar); // vector<Vec3f> oldABox = o1->boundingBox; // UpdateCoords(aBox,position, vel, rotation, wvel ,0, 1.0,true, 1.f, xScale, yScale, zNear, zFar); // for(unsigned int i = 0; i <oldABox.size(); i++){ // aBox.push_back(oldABox[i]); // } vector<GameObject*> Bs = it->second; it++; for(unsigned int b = 0; b<Bs.size(); b++){ GameObject* o2 = Bs[b]; if(o2->CollisionTierNum<2) continue; vector<Vec3f> bBox = o2->boundingBox; //pos = o2->GetPosition(); position = Vec3f(pos.x(), pos.y(), pos.z()); vel = o2->velocity; wvel = o2->angularVelocity; rotation = o2->GetRotation(); UpdateCoords(bBox, position, vel, rotation, wvel ,0,1.0, true, 1.f, xScale, yScale, zNear, zFar); vector<Vec3f> oldBBox = o2->boundingBox; //UpdateCoords(oldBBox,position, vel, rotation, wvel ,0, 1.0, true, 1.f, xScale, yScale, zNear, zFar); //for(unsigned int i = 0; i <oldBBox.size(); i++){ // bBox.push_back(oldBBox[i]); //} Simplex P; Vec3f V; V = GJKDistance(aBox, bBox, P); if(V.norm() < tolerance){ //We have a collision. if(!AlreadyIn(o1, o2, 1, room)) { if(o1->objType == ACTIVE_OBJECT_TYPE && o2->objType == ACTIVE_OBJECT_TYPE){ o1->drawCollision = true; o2->drawCollision = true; } Vec3f contact = ResolveContact(P); room->collisionTier1List[o1].push_back(o2); room->collisionTier1List[o2].push_back(o1); CollisionData o1D, o2D; o1D.pointOfContact = contact; o2D.pointOfContact = contact; o1D.contactNormal = V.normalized(); o2D.contactNormal = -V.normalized(); o1->tier1CollisionData[o2] = o1D; o2->tier1CollisionData[o1] = o2D; } } } } ////////////////////////////////////////////////////////////////////////////// // // Mesh Level Detection // ///////////////////////////////////////////////////////////////////////////// }