void Ball::Update() { // store our last position Vector2 lastPosition = position; // update our current position using velocity position += velocity * Monocle::deltaTime; // check collisions against the paddles Collider* collider = Collide("Paddle"); if (collider) { Debug::Log("Ball hit an entity tagged with 'Paddle'"); position = lastPosition; Vector2 diff = position - collider->GetEntity()->position; diff.Normalize(); diff *= velocity.GetMagnitude(); velocity = diff; // Calculate panning float pan = ((collider->GetEntity()->position.x / Graphics::GetVirtualWidth()) - 0.5) * 2.0; if (sfxWall) sfxWall->Play(1,1.0,pan); // Play it with panning! (STEREO, baby :D) } // if we hit the top or bottom of the screen if (position.y < 0 || position.y > 600) { position = lastPosition; velocity.y *= -1; // Calculate panning float pan = ((position.x / Graphics::GetVirtualWidth()) - 0.5) * 2.0; if (sfxWall) sfxWall->Play(1,1.0,pan,2.0); // Play it higher, you won't even notice it's the same ;D } // if we go off the left side of the screen if (position.x < 0) { SendNoteToScene("BallOffLeft"); } // if we go off the right side of the screen if (position.x > 800) { SendNoteToScene("BallOffRight"); } }
//------------------------------------------------------------------------------ Collider * PhysicsSystem::colliderAt(const sf::FloatRect & rect, const Collider * except) const { for(auto it = m_colliders.cbegin(); it != m_colliders.cend(); ++it) { Collider * c = *it; if(c != except && c->enabled() && c->entity().activeInHierarchy()) { if(c->collides(rect)) return c; } } return nullptr; }
void Bullet::init() { this->setInt("power", 3); this->n = 0; Collider* c = this->addComponent<Collider>(); c->setRigid(false); c->setActive(false); c->setSize(10); Network* n = this->addComponent<Network>(); n->currentFrame = 0; n->frameRate = 10; this->_renderer = this->addComponent<Renderer>(); this->_renderer->setName("pew"); }
bool Map::isFlyable(Collider const& collider) const { for(int i(-1); i<=1; ++i) { for(int j(-1); j<=1; ++j) { int index(mapCoordToIndex(collider.getPosition() + Vec2d(i,j)*(collider.getRadius()))); if(index >= 0 && index < int(tiles_.size())) { if(tiles_[index].tileType() == TileType::ROCK || tiles_[index].tileType() == TileType::FOREST) return false; } } } return true; }
void Camera::UpdateVisibility(const Collider& Collider) { using std::sort; using std::unique; for (auto&& buf : m_Buffers) { buf.clear(); } const Vec2i LeftUpper = -m_CurTranslation - Vec2f(1.f, 1.f); const Vec2i RightLower = -m_CurTranslation + m_ViewHalfSize * 2.f + Vec2f(1.f, 1.f); m_CollisionsEnd = Collider.CopyColliding( LeftUpper, RightLower, m_CollisionsBuffer.begin()); sort(m_CollisionsBuffer.begin(), m_CollisionsEnd); m_CollisionsEnd = unique(m_CollisionsBuffer.begin(), m_CollisionsEnd); for (auto CollisionsIter = m_CollisionsBuffer.begin(); CollisionsIter != m_CollisionsEnd; ++CollisionsIter) { const int TextureDescr = (*CollisionsIter)->GetTextureDescriptor(); if (TextureDescr != -1) { m_Buffers[TextureDescr].push_back(*CollisionsIter); } } }
void Ship::calcBase() { Collider* c = m_body->getCollider(); float min = FLT_MAX; while (c) { if (lengthSq(c->getTransform().p - m_body->getLocalCenter()) < min) { min = lengthSq(c->getTransform().p - m_body->getLocalCenter()); m_base = (Brick*)c->getUserData(); } c = c->getNext(); } }
void Graph::Attach(int from, EdgeRef to) { Collider* col = std::get<2>(to); int toIndex = std::get<0>(to); Plane* face1 = nodes[from].GetPlane(); Plane* face2 = nodes[toIndex].GetPlane(); glm::vec3 face1orig = face1->getPosition(), face2orig = face2->getPosition(), axis = glm::abs(glm::cross(face1orig, face2orig)); auto angle = glm::orientedAngle(face1orig, face2orig,axis); face2->addChild(col); col->ApplySpace(glm::inverse(face2->getWorldMatrix())); col->addChild(face1); face1->ApplySpace(glm::inverse(col->getWorldMatrix())); auto face1pos = face1->getModelPosition(); col->JointRotateBy(angle); }
void EnemyBullet::init() { if (this->_world->server) this->mine = true; this->scale.x = 1.8; this->scale.y = 1.8; this->n = 0; Collider* c = this->addComponent<Collider>(); c->setRigid(false);//false blabla c->setActive(false); c->setSize(15); Network* n = this->addComponent<Network>(); n->currentFrame = 0; n->frameRate = 10; this->_renderer = this->addComponent<Renderer>(); this->_renderer->setName("enemypew"); }
void Mob::init() { this->setInt("life", 12); this->setFloat("speed", 10); Collider* c = this->addComponent<Collider>(); c->setRigid(false); c->setActive(true); c->setSize(40); Network* n = this->addComponent<Network>(); n->currentFrame = 0; n->frameRate = 20; this->scale.x = 0.7; this->scale.y = 0.7; this->_renderer = this->addComponent<Renderer>(); this->_renderer->setName("mob"); this->_controller = this->addComponent<Controller>(); }
void PhysXRigidbody::removeColliders() { UINT32 numShapes = mInternal->getNbShapes(); PxShape** shapes = (PxShape**)bs_stack_alloc(sizeof(PxShape*) * numShapes); mInternal->getShapes(shapes, sizeof(PxShape*) * numShapes); for (UINT32 i = 0; i < numShapes; i++) { Collider* collider = (Collider*)shapes[i]->userData; collider->_getInternal()->_setCCD(false); mInternal->detachShape(*shapes[i]); } bs_stack_free(shapes); }
void update() override { Entity::update(); if (collider->isAtLimit()) { nstuck++; moving->setVelocity(-moving->getVelocity()); position = position + sf::Vector2f(moving->getVelocity().x*2,moving->getVelocity().y*2);; } else nstuck = 0; }
bool ColliderCircle::CheckCollision(const Collider& aCollider, const CollisionStruct& aCollisionStruct) const { CollisionStruct newStruct; newStruct.myObjectSpace = aCollisionStruct.myArgumentObjectSpace; newStruct.myObjectScale = aCollisionStruct.myArgumentObjectScale; newStruct.myArgumentObjectSpace = aCollisionStruct.myObjectSpace; newStruct.myArgumentObjectScale = aCollisionStruct.myObjectScale; return aCollider.CheckCollision(*this, newStruct); }
GameObject* BulletFactory::GetNewObject() { int nextID = IDFactory::GetNextID(); // TODO: this sucks, fix it Mobile* m = new Mobile(); GameObject* bullet = new Bullet(); Collider* c = new Collider(); m->SetValue(PhysicsComponent::GeneralVelocity, 12); c->SetRadius(1); bullet->SetID(nextID); m->SetCollider(c); bullet->SetMobile(m); return bullet; }
/* * Returns the list of all visible colliders. * * This function is not thread-safe because it relies on a static * array of colliders which could be updated by a different thread. */ void Picker::pickVisible(Scene* scene, Transform* t, std::vector<ColliderData>& picklist) { const std::vector<Component*>& colliders = scene->lockColliders(); for (auto it = colliders.begin(); it != colliders.end(); ++it) { Collider* collider = reinterpret_cast<Collider*>(*it); SceneObject* owner = collider->owner_object(); if (collider->enabled() && (owner != NULL) && owner->enabled()) { ColliderData data(collider); Transform* trans = owner->transform(); glm::mat4 worldmtx = trans->getModelMatrix(); data.HitPosition = glm::vec3(worldmtx[3]); data.Distance = data.HitPosition.length(); data.IsHit = true; picklist.push_back(data); } } std::sort(picklist.begin(), picklist.end(), compareColliderData); scene->unlockColliders(); }
void CRigidbody::updateColliders() { Stack<HSceneObject> todo; todo.push(SO()); while(!todo.empty()) { HSceneObject currentSO = todo.top(); todo.pop(); if(currentSO->hasComponent<CCollider>()) { Vector<HCollider> colliders = currentSO->getComponents<CCollider>(); for (auto& entry : colliders) { if (!entry->isValidParent(mThisHandle)) continue; Collider* collider = entry->_getInternal(); if (collider == nullptr) continue; entry->setRigidbody(mThisHandle, true); mChildren.push_back(entry); collider->setRigidbody(mInternal.get()); mInternal->addCollider(collider->_getInternal()); } } UINT32 childCount = currentSO->getNumChildren(); for (UINT32 i = 0; i < childCount; i++) { HSceneObject child = currentSO->getChild(i); if (child->hasComponent<CRigidbody>()) continue; todo.push(child); } } }
__global__ void kernelCollideVoxelMapsDebug(Voxel* voxelmap, const uint32_t voxelmap_size, OtherVoxel* other_map, Collider collider, uint16_t* results) { //#define DISABLE_STORING_OF_COLLISIONS __shared__ uint16_t cache[cMAX_NR_OF_THREADS_PER_BLOCK]; uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; uint32_t cache_index = threadIdx.x; cache[cache_index] = 0; while (i < voxelmap_size) { // todo / note: at the moment collision check is only used for DYNAMIC and SWEPT VOLUME type, static is used for debugging const bool collision = collider.collide(voxelmap[i], other_map[i]); if (collision) // store collision info { //#ifndef DISABLE_STORING_OF_COLLISIONS // other_map[i].occupancy = 255; // other_map[i].voxeltype = eVT_COLLISION; //#endif cache[cache_index] += 1; } i += blockDim.x * gridDim.x; } // debug: print collision coordinates // if (temp) // { // Vector3ui col_coord = mapToVoxels(voxelmap, dimensions, &(voxelmap[i])); // printf("Collision at voxel (%u) = (%u, %u, %u). Memory addresses are %p and %p.\n", // i, col_coord.x, col_coord.y, col_coord.z, (void*)&(voxelmap[i]), (void*)&(other_map[i])); // } __syncthreads(); uint32_t j = blockDim.x / 2; while (j != 0) { if (cache_index < j) { cache[cache_index] = cache[cache_index] + cache[cache_index + j]; } __syncthreads(); j /= 2; } // copy results from this block to global memory if (cache_index == 0) { results[blockIdx.x] = cache[0]; } #undef DISABLE_STORING_OF_COLLISIONS }
bool Collider::isColliding(const Collider& other) const { double minimumDistance(other.getRadius() + radius_); if (distanceTo(other) <= minimumDistance) { return true; } else { return false; } }
GameObject* BulletFactory::GetBullet(float x, float y, float rotation, GameObject* owner, Controller* owningController) { GameObject* bullet = new Bullet(); Collider* c = new Collider(); Mobile* m = new Mobile(); bullet->SetID(IDFactory::GetNextID()); m->SetCollider(c); bullet->SetMobile(m); c->SetRadius(collisionRadius); m->SetValue(PhysicsComponent::PositionX, x); m->SetValue(PhysicsComponent::PositionY, y); m->SetValue(PhysicsComponent::Rotation, rotation); bullet->SetParent(owner); bullet->SetController(owningController); bullet->SetDamage(damage); bullet->SetRenderable(bulletRenderable); return bullet; }
void GameManager::Play() { Vector<Collider*>& colliders = m_colliderManager->m_colliders; Vector<Target*>& targets = m_targetManager->m_targets; Collider* collider = nullptr; for (int i = 0; i < colliders.size(); i++) { collider = colliders.at(i); if (collider->IsFlying()) { collider->Act(); CheckCollide(collider, targets); } if (collider->IsBullet()) { Bullet* bullet = static_cast<Bullet*>(collider); if (bullet->IsToExplode()) { Explosion* explosion = bullet->GetExplosion(); m_colliderManager->AddExplosion(explosion); m_gameLayer->addChild(explosion); if (m_curStageNum == 12) { CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(FileStuff::SOUND_CAR_CRASH, false, 1.0f, 0, 0); } else { CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(FileStuff::SOUND_FIREWORK_EXPLOSION, false, 1.0f, 0, 0); } } } } m_colliderManager->EraseDeadColliders(); m_targetManager->EraseDeadTargets(); ControlWinFailProgress(); }
NS_BEGIN //PhysicsContext g_PhysicsContext; void PhysicsContext::Initialize(std::unordered_map<uint, GameObject*> allObjects) { contacts.reserve(100); uint size = allObjects.size(); for (std::unordered_map<uint, GameObject*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it) { Collider* collider = it->second->GetComponent<Collider>(); if (collider) { PhysicsObject o; o.collider = collider; o.rigidbody = collider->GetRigidbody(); m_Objects.push_back(o); ++numCollidables; } } }
IntersectData Collider::Intersect(const Collider& other) const { if(m_type == TYPE_SPHERE && other.GetType() == TYPE_SPHERE) { BoundingSphere* self = (BoundingSphere*)this; return self->IntersectBoundingSphere((BoundingSphere&)other); } std::cerr << "Error: Collisions not implemented between specified " << "colliders." << std::endl; exit(1); //Control should never reach this point return IntersectData(false, 0); }
math::IntersectInfo physics::Collider::Intersect(const Collider& collider) const { if (collider_types::TYPE_SPHERE == m_type && collider_types::TYPE_SPHERE == collider.GetType() ) { const math::Sphere* self = dynamic_cast<const math::Sphere*>(this); if (self == nullptr) { CRITICAL_LOG_PHYSICS("Casting collider to BoundingSphere pointer failed."); exit(EXIT_FAILURE); } //return self->DoesIntersectSphere(static_cast<math::Sphere>(collider)); } CRITICAL_LOG_PHYSICS("Only bounding spheres collision is currently handled by the physics engine. Other colliders are not yet supported."); return math::IntersectInfo(REAL_ONE /* some arbitrary (but positive) value */); }
__global__ void kernelCollideVoxelMapsBitvector(ProbabilisticVoxel* voxelmap, const uint32_t voxelmap_size, BitVoxel<length>* other_map, uint32_t loop_size, Collider collider, BitVector<length>* results) { extern __shared__ BitVector<length> cache[]; //[cMAX_NR_OF_THREADS_PER_BLOCK]; const uint32_t i = (blockIdx.x * blockDim.x + threadIdx.x) * loop_size; uint32_t cache_index = threadIdx.x; cache[cache_index] = BitVector<length>(); BitVector<length> temp; // ToDo: replace if with while and increment: i += blockDim.x * gridDim.x; if (i < voxelmap_size) { for (uint32_t k = 0; (k < loop_size && (i + k < voxelmap_size)); k++) { if (collider.collide(voxelmap[i + k])) { temp |= other_map[i + k].bitVector; } } cache[cache_index] = temp; } __syncthreads(); uint32_t j = blockDim.x / 2; while (j != 0) { if (cache_index < j) { cache[cache_index] = cache[cache_index] | cache[cache_index + j]; } __syncthreads(); j /= 2; } // copy results from this block to global memory if (cache_index == 0) { // FOR MEASUREMENT TEMPORARILY EDITED: //results[blockIdx.x] = true; results[blockIdx.x] = cache[0]; } }
__global__ void kernelCollideVoxelMaps(Voxel* voxelmap, const uint32_t voxelmap_size, OtherVoxel* other_map, Collider collider, bool* results) { __shared__ bool cache[cMAX_NR_OF_THREADS_PER_BLOCK]; uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; uint32_t cache_index = threadIdx.x; cache[cache_index] = false; bool temp = false; while (i < voxelmap_size) { if (collider.collide(voxelmap[i], other_map[i])) { temp = true; // voxelmap[i].voxeltype = eVT_COLLISION; // voxelmap[i].occupancy = 255; } i += blockDim.x * gridDim.x; } cache[cache_index] = temp; __syncthreads(); uint32_t j = blockDim.x / 2; while (j != 0) { if (cache_index < j) { cache[cache_index] = cache[cache_index] || cache[cache_index + j]; } __syncthreads(); j /= 2; } // copy results from this block to global memory if (cache_index == 0) { // // FOR MEASUREMENT TEMPORARILY EDITED: // results[blockIdx.x] = true; results[blockIdx.x] = cache[0]; } }
void Map::clearForest(Collider const& collider) { int index( int(collider.getPosition().x/tileSize_) + int(collider.getPosition().y/tileSize_) * width_); for(int i(-collider.getRadius()/tileSize_); i <= collider.getRadius()/tileSize_; ++i) { for(int j(-collider.getRadius()/tileSize_); j <= collider.getRadius()/tileSize_; ++j) { if(tiles_[index + i + j*width_].tileType() == TileType::FOREST){ env_->z2().remove(tiles_[index + i + j*width_]); tiles_[index + i + j*width_] = getCurrentGame().tileAtlas_.at("grass"); Vec2d position(index%width_ + i, index/width_ + j); sf::Vector2f pos; pos.x = (position.x - position.y) * tileSize_ + width_ * tileSize_; pos.y = (position.x + position.y) * tileSize_ * 0.5; tiles_[index + i + j*width_].sprite_.setPosition(pos); tiles_[index + i + j*width_].setZ(pos.y + tileSize_); env_->z2().push_back(tiles_[index + i + j*width_]); std::cout << "BAM" << std::endl; } } } }
/* * Intersects all the colliders in the scene with the input ray * and returns the list of collisions. * * This function is not thread-safe because it relies on a static * array of colliders which could be updated by a different thread. */ void Picker::pickScene(Scene* scene, std::vector<ColliderData>& picklist, Transform* t, float ox, float oy, float oz, float dx, float dy, float dz) { glm::vec3 ray_start(ox, oy, oz); glm::vec3 ray_dir(dx, dy, dz); const std::vector<Component*>& colliders = scene->lockColliders(); const glm::mat4& model_matrix = t->getModelMatrix(); Collider::transformRay(model_matrix, ray_start, ray_dir); for (auto it = colliders.begin(); it != colliders.end(); ++it) { Collider* collider = reinterpret_cast<Collider*>(*it); SceneObject* owner = collider->owner_object(); if (collider->enabled() && (owner != NULL) && owner->enabled()) { ColliderData data = collider->isHit(ray_start, ray_dir); if ((collider->pick_distance() > 0) && (collider->pick_distance() < data.Distance)) { data.IsHit = false; } if (data.IsHit) { picklist.push_back(data); } } } std::sort(picklist.begin(), picklist.end(), compareColliderData); scene->unlockColliders(); }
Collider::Collider(const Collider& collider) { radius_ = collider.getRadius(); position_ = collider.getPosition(); }
double Collider::distanceTo(const Collider& other) const { return distanceTo(other.getPosition()); }
Vec2d Collider::directionTo(const Collider& other) const { return directionTo(other.getPosition()); }
// double-dispatching bool PolygonCollider::will_collide(const Collider &other) const { return other.will_collide(*this); }