//----------------------------------------------------------------------------------------------------------------------------------- bool GameObject::CheckCollisionWithObjects(const std::list<GameObject*>& collisionObjects) { if (!GetCollider() || m_noCollisions) { return false; } for (GameObject* gameObject : collisionObjects) { if (gameObject == this) { continue; } // Check distance away from object before doing proper collision detection if (Vector2::DistanceSquared(GetWorldPosition(), gameObject->GetWorldPosition()) >= (GetSize() * 0.5f).LengthSquared() + (gameObject->GetSize() * 0.5f).LengthSquared()) { continue; } if (GetCollider()->CheckCollisionWith(gameObject->GetCollider())) { OnCollision(); return true; } } return false; }
/// 毎フレーム呼ぶ関数 更新が必要な関数 void CPlayerAttack::Update() { PushKey(); Create(); OnCollision(); }
void EffectSystem::Update(float _dt) { /* If currentTime is >= then maxTime, reset to 0, else add _dt */ m_currentTime = m_currentTime >= m_maxTime ? 0 : m_currentTime + _dt; // Go through all entites for (auto entityPair : m_entityMap) { Entity* e = entityPair.second; m_flags = e->GetComponent<EffectComponent>()->m_effects; // OnEveryFrame OnEveryFrame(e, _dt); // OneEverySecond if (m_currentTime >= m_maxTime) OnEverySecond(e, _dt); // OnCollision auto collide = e->GetComponent<CollisionComponent>(); if (collide) { if (collide->GetCollisions().size() > 0) OnCollision(e, _dt); } } UpdateEffects(_dt); }
void Entity::PhysicsUpdate(Octree* octree, double deltaTime) { auto& instancesToCheck = octree->nearbyObjects(this); for (auto obj : instancesToCheck) { if (obj == this || frameCollisions.find(this->GetID()) != frameCollisions.end()) continue; if (this->collisionTest != ObjectType::Default && this->collisionTest != obj->GetType()) continue; checkCount++; std::shared_ptr<Collision> collision = obj->GetCollider()->IsColliding(this->collider); if (collision != nullptr) { collision->collider1 = obj; collision->collider2 = this; } AddFrameCollision(obj->GetID(), collision); obj->AddFrameCollision(GetID(), collision); if (collision != nullptr && collision->colliding) { OnCollision(collision->GetEvent(this)); obj->OnCollision(collision->GetEvent(obj)); } } frameCollisions.clear(); velocity += acceleration * static_cast<float>(deltaTime); lastPosition = GetPosition(); Translate(velocity * static_cast<float>(deltaTime)); acceleration = vector3(0.0f, 0.0f, 0.0f); }
void CEnemyTurtleFly::Update(float deltaTime) { MoveUpdate(deltaTime); SetFrame(deltaTime); ChangeFrame(deltaTime); OnCollision(deltaTime); }
void CTatteredId::Update() { if (state != State::None) { anime->Update(); move->Update(); task->GetComponent<CTatteredIdAttack>(CEnemyManager::TatteredIdAttack, 0)->Update(); } OnCollision(); }
/// 更新 void CPatroller::Update() { if (state != State::None) { anime->Update(); move->Update(); OnCollision(); task->GetComponent<CPatrollerAttack>(CEnemyManager::PatrollerAttack,0)->Update(); } }
void CBoidObject::OnEntityEvent( SEntityEvent &event ) { switch (event.event) { case ENTITY_EVENT_DONE: m_entity = 0; m_object = 0; m_pPhysics = 0; break; case ENTITY_EVENT_COLLISION: OnCollision(event); break; } }
void CEnemyTurtle::Update(float deltaTime) { ChangeFrame(deltaTime); SetFrame(deltaTime); MoveUpdate(deltaTime); OnCollision(deltaTime); if (this->m_isLife) { if (CCollision::GetInstance()->Collision(CMarioObject::GetInstance(), this)) { CMarioObject::GetInstance()->m_status = STATUS::DIE; CMarioObject::GetInstance()->m_vy = 120; } } }
void ModulePhysics::BeginContact(b2Contact* contact) { LOG("Collision"); PhysBody* pb1 = (PhysBody*)contact->GetFixtureA()->GetBody()->GetUserData(); PhysBody* pb2 = (PhysBody*)contact->GetFixtureB()->GetBody()->GetUserData(); LOG("PB1: %s", pb1/*->GetPosition()*/); LOG("PB2: %s", pb2); // if (pb1 != NULL && pb2 != NULL) { OnCollision(pb1, pb2); } }
void GameWorld::BeginContact(b2Contact *contact) { b2Body* body_a = contact->GetFixtureA()->GetBody(); b2Body* body_b = contact->GetFixtureB()->GetBody(); // only need to observe collisions that involve GameObjects if(body_a->GetUserData() == NULL || body_b->GetUserData() == NULL) { return; } // identify type of the objects involved in the collision EGameObjectType game_object_a_type = ((GameObject*)body_a->GetUserData())->GetType(); EGameObjectType game_object_b_type = ((GameObject*)body_b->GetUserData())->GetType(); // if a collision did not involve the clown, it will be ignored if(game_object_a_type != E_GAME_OBJECT_CLOWN && game_object_b_type != E_GAME_OBJECT_CLOWN) return; // separate the clown and the other object GameObject* other_object = (game_object_a_type != E_GAME_OBJECT_CLOWN) ? (GameObject*)body_a->GetUserData() : (GameObject*)body_b->GetUserData(); // based on type, call appropriate collision response function switch(other_object->GetType()) { case E_GAME_OBJECT_PLATFORM: if(other_object == base_platform_) { DoBasePlatformCollision(); } else { OnCollision(contact->GetManifold()->localNormal); } break; case E_GAME_OBJECT_COLLECTIBLE: case E_GAME_OBJECT_ROCKET: case E_GAME_OBJECT_BALLOON: ((Collectible*)other_object)->OnCollision(); break; } }
void SgObjectManager::DetectCollisions() { for(int i=0; i<m_dwMaxObjects; i++) { if(m_ppObject[i]!=NULL) { for(int j=i+1; j<m_dwMaxObjects; j++) { if( NULL == m_ppObject[j] )continue; //here overloaded function should do //appropriate action depending on type //of collision. switch(DetectCollision(m_ppObject[i], m_ppObject[j])) { case CT_STDCLSN: OnCollision( m_ppObject[i] , m_ppObject[j] ); break; default: break; } } } } }
//! checks collision with another component void RigidBodyComponent::CheckCollisions(CollisionComponent* pOther) { if(RigidBodyComponent* pOtherRigidBody = DYNAMIC_CAST(pOther, RigidBodyComponent)) { std::vector<CollisionShape::CollisionInfo> infos; Entity3D* pParent = static_cast<Entity3D*>(GetParent()); Entity3D* pOtherParent = static_cast<Entity3D*>(pOther->GetParent()); CollisionShapeTransform component1Transform(pParent->GetTransformationMatrix(), pParent->GetAbsoluteScale()); CollisionShapeTransform component2Transform(pOtherParent->GetTransformationMatrix(), pOtherParent->GetAbsoluteScale()); for(u32 i=0; i<GetShapeCount(); ++i) { if(CollisionShape* pShape1 = GetShape(i)) { for(u32 j=0; j<pOtherRigidBody->GetShapeCount(); ++j) { if(CollisionShape* pShape2 = pOtherRigidBody->GetShape(j)) { CollisionShape::CollisionInfo info; if(pShape1->CollidesWith(pShape2, component1Transform, component2Transform, info)) { infos.push_back(info); } } } } } if(!infos.empty()) { OnCollision(pOther, infos); pOther->OnCollision(this, infos); } } }
static void hitCallback(CollisionContact *c) { //printf("OUCH! %x (depth %f)\n", SDL_GetTicks(), c->depth); Object *po1 = static_cast<Object*>(c->userData1); Object *po2 = static_cast<Object*>(c->userData2); const bool po1_isDynBody = po1->IsType(Object::DYNAMICBODY); const bool po2_isDynBody = po2->IsType(Object::DYNAMICBODY); // collision response assert(po1_isDynBody || po2_isDynBody); if (po1_isDynBody && po2_isDynBody) { DynamicBody *b1 = static_cast<DynamicBody*>(po1); DynamicBody *b2 = static_cast<DynamicBody*>(po2); const vector3d linVel1 = b1->GetVelocity(); const vector3d linVel2 = b2->GetVelocity(); const vector3d angVel1 = b1->GetAngVelocity(); const vector3d angVel2 = b2->GetAngVelocity(); const double coeff_rest = 0.5; // step back // mover->UndoTimestep(); const double invMass1 = 1.0 / b1->GetMass(); const double invMass2 = 1.0 / b2->GetMass(); const vector3d hitPos1 = c->pos - b1->GetPosition(); const vector3d hitPos2 = c->pos - b2->GetPosition(); const vector3d hitVel1 = linVel1 + angVel1.Cross(hitPos1); const vector3d hitVel2 = linVel2 + angVel2.Cross(hitPos2); const double relVel = (hitVel1 - hitVel2).Dot(c->normal); // moving away so no collision if (relVel > 0) return; if (!OnCollision(po1, po2, c, -relVel)) return; const double invAngInert1 = 1.0 / b1->GetAngularInertia(); const double invAngInert2 = 1.0 / b2->GetAngularInertia(); const double numerator = -(1.0 + coeff_rest) * relVel; const double term1 = invMass1; const double term2 = invMass2; const double term3 = c->normal.Dot((hitPos1.Cross(c->normal)*invAngInert1).Cross(hitPos1)); const double term4 = c->normal.Dot((hitPos2.Cross(c->normal)*invAngInert2).Cross(hitPos2)); const double j = numerator / (term1 + term2 + term3 + term4); const vector3d force = j * c->normal; b1->SetVelocity(linVel1 + force*invMass1); b1->SetAngVelocity(angVel1 + hitPos1.Cross(force)*invAngInert1); b2->SetVelocity(linVel2 - force*invMass2); b2->SetAngVelocity(angVel2 - hitPos2.Cross(force)*invAngInert2); } else { // one body is static vector3d hitNormal; DynamicBody *mover; if (po1_isDynBody) { mover = static_cast<DynamicBody*>(po1); hitNormal = c->normal; } else { mover = static_cast<DynamicBody*>(po2); hitNormal = -c->normal; } const double coeff_rest = 0.5; const vector3d linVel1 = mover->GetVelocity(); const vector3d angVel1 = mover->GetAngVelocity(); // step back // mover->UndoTimestep(); const double invMass1 = 1.0 / mover->GetMass(); const vector3d hitPos1 = c->pos - mover->GetPosition(); const vector3d hitVel1 = linVel1 + angVel1.Cross(hitPos1); const double relVel = hitVel1.Dot(c->normal); // moving away so no collision if (relVel > 0 && !c->geomFlag) return; if (!OnCollision(po1, po2, c, -relVel)) return; const double invAngInert = 1.0 / mover->GetAngularInertia(); const double numerator = -(1.0 + coeff_rest) * relVel; const double term1 = invMass1; const double term3 = c->normal.Dot((hitPos1.Cross(c->normal)*invAngInert).Cross(hitPos1)); const double j = numerator / (term1 + term3); const vector3d force = j * c->normal; mover->SetVelocity(linVel1 + force*invMass1); mover->SetAngVelocity(angVel1 + hitPos1.Cross(force)*invAngInert); } }
void CGoblin::Update() { //move->Update(); OnCollision(); }