本文整理汇总了C++中PhysicsComponent类的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsComponent类的具体用法?C++ PhysicsComponent怎么用?C++ PhysicsComponent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PhysicsComponent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleEvent
void StickOnCollision::handleEvent(const CollisionEvent& e)
{
GameObject obj = e.getOtherObject();
if (obj.getType() == GameObject::Type::TILE)
{
hookPoint = obj.getPos();
PhysicsComponent * hookPhysics = owner_.getComponent<PhysicsComponent>();
hookPhysics->setVelX(0);
hookPhysics->setVelY(0);
auto tileCollider = e.getOtherCollider();
auto hookCollider = owner_.getComponent<ColliderComponent>();
float hookBot = hookCollider->getBottom();
float tileBot = tileCollider.getBottom();
float tileTop = tileCollider.getTop();
if (hookBot == tileTop)
{
isConnected = false;
}
else
{
isConnected = true;
}
//LOG("INFO") << "Hook is connected at " << hookPoint;
}
}
示例2: addSingleResult
btScalar ContactCallback::addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1)
{
const btCollisionObject* colObj = colObj0Wrap->getCollisionObject();
PhysicsComponent* physComp = static_cast<PhysicsComponent*>(colObj->getUserPointer());
if(physComp)
physComp->OnAddSingleResult(cp,partId0,index0,colObj1Wrap,partId1,index1);
return 0;
}
示例3: OnAddSingleResult
void PhysicsComponent::OnAddSingleResult(btManifoldPoint& cp,int partId0,int index0,const btCollisionObjectWrapper* collidedObjWrap,int collidedObjPartId,int collidedObjIndex)
{
const btCollisionObject* colObj = collidedObjWrap->getCollisionObject();
PhysicsComponent* physComp = static_cast<PhysicsComponent*>(colObj->getUserPointer());
if(physComp)
{
Entity* entOwner = physComp->GetOwner();
//entOwner->OnHit();
}
}
示例4: class
void RenderComponent::VUpdate()
{
/*
All of this code must go!!! This is shit! Rewrite animation system as a seperate component, starting with a virtual base
class (AnimationComponent) that can then fork off into a bunch of other more specific animation types (e.g. HumanoidAnimationComponent,
MachineAnimationComponent, ParticleAnimationComponent, etc.).
*/
if(_animated)
{
PhysicsComponent* physicsComponent = GetOwner()->GetComponent<PhysicsComponent>(COMPONENT_PHYSICS);
const uint UP = 1, DOWN = 0, RIGHT = 2, LEFT = 3;
int direction = -1;// By default, don't change
if(physicsComponent->GetVelocity().x > 0.5)
{
direction = RIGHT;
}
else if(physicsComponent->GetVelocity().x < -0.5)
{
direction = LEFT;
}
else if(physicsComponent->GetVelocity().y > 0.5)
{
direction = DOWN;
}
else if(physicsComponent->GetVelocity().y < -0.5)
{
direction = UP;
}
if(direction != -1)// If the texture should change...
{
_textureY = direction*(_textureHeight/4);
_textureX = _currentAnimationFrame*(_textureWidth/3);
if(SDL_GetTicks() - _lastAnimationChange >= (100))
{
_currentAnimationFrame++;
if(_currentAnimationFrame == 3)
_currentAnimationFrame = 0;
_lastAnimationChange = SDL_GetTicks();
}
}
else
{
_textureX = 0;
}
}
}
示例5: Update
void Character::Update()
{
// Update state machine
mStateMachine.ProcessStateTransitions();
mStateMachine.UpdateStates();
// Move character
const float MAX_SPEED = 100.0f;
float currSpeed = mSpeedScale * MAX_SPEED;
mPhysicsComponent.SetSpeed(currSpeed);
mPhysicsComponent.Move();
printf("Current speed: %f\n", currSpeed);
}
示例6: updateCamera
void updateCamera()
{
Actor *actor = game_->findActor("wizard");
if (actor) {
PhysicsComponent *component = actor->getPhysicsComponent();
if (component) {
b2Body *bodyBody = component->findBody("body");
if (bodyBody) {
b2Vec2 position = bodyBody->GetPosition();
cameraPosition_.x = position.x;
cameraPosition_.y = position.y;
}
}
}
}
示例7: Update
PHYSICSDLL_API void OrbitComponent::Update(float dt)
{
if (targeting_)
{
Transform* tr = (Transform*)GetSibling(CT_Transform);
Vector3 forceVec = current_dir_;
Vector3 toTarget = target_ - tr->GetPosition();
float bias = toTarget.Length() / target_dist_;
forceVec += toTarget.GetNormalized() * bias;
PhysicsComponent* pComp = (PhysicsComponent*)GetSibling(CT_PhysicsComponent);
if (pComp)
{
pComp->GetRigidBody()->AddForce(forceVec);
current_dir_ = pComp->GetRigidBody()->GetState().Velocity.GetNormalized();
pComp->GetRigidBody()->GetState().Velocity = pComp->GetRigidBody()->GetState().Velocity.GetNormalized() * 2.0f;
}
}
}
示例8: Update
void LocomotionSystem::Update( float i_dt )
{
for ( std::map<EntityHandle, Component*>::iterator it = m_components.begin(); it != m_components.end(); it++ )
{
LocomotionComponent* pComponent = (LocomotionComponent*) it->second;
if ( pComponent )
{
if ( pComponent->m_locomotionMode )
{
pComponent->m_locomotionMode->Update( i_dt );
}
PhysicsComponent* pPhysicsComponent = EntitySystem::GetInstance()->GetComponent<PhysicsComponent>( pComponent->m_entityHandle );
if ( pPhysicsComponent )
{
float yVel = pPhysicsComponent->GetVelocity().y;
JumpState pJumpState;
if ( yVel > 0.5f )
{
pJumpState = JumpState::JUMPING;
}
else if ( yVel < 0.5f )
{
pJumpState = JumpState::FALLING;
}
else if ( yVel == 0.0f )
{
pJumpState = JumpState::NOT_JUMPING;
}
if ( pJumpState == JumpState::FALLING && pComponent->m_jumpState == JumpState::JUMPING )
{
EventManager::GetInstance()->SendEvent( "VelocityApexReached", &pComponent->m_entityHandle );
}
pComponent->m_jumpState = pJumpState;
}
}
}
}
示例9: run
void KeyboardSystem::run(Entity* entity, float time)
{
PhysicsComponent* physComp = entity->getComponent<PhysicsComponent>();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space))
{
physComp->jump();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
physComp->walk(1);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) )
{
physComp->walk(-1);
}
else
{
physComp->walk(0);
}
}
示例10: deleteEntity
void ProjectileCollideSystem::update()
{
for(int subID = 0; subID < subscribedEntities[0].size(); subID++)
{
//Get projectile component
Entity * projectileEnt = entities[subscribedEntities[0][subID]];
ColliderComponent* projectileCollideComp = static_cast<ColliderComponent*>(projectileEnt->getComponent(ColliderComponent::getStaticID()));
WorldComponent * projectileWorldComp = static_cast<WorldComponent*>(projectileEnt->getComponent(WorldComponent::getStaticID()));
PhysicsComponent* projectilePhysicsComp = static_cast<PhysicsComponent*>(projectileEnt->getComponent(PhysicsComponent::getStaticID()));
//Check projectile for collisions
for(int i = 0; i < projectileCollideComp->collisionData.size(); i++)
{
std::shared_ptr<CollisionPair> collision = projectileCollideComp->collisionData[i];
int projectilePairID = collision->getCollisionPairID(subscribedEntities[0][subID]);//Projectile's CollisionPairID
int collidingPairID = collision->getOppositePairID(projectilePairID);//The Colliding Ent's CollisionPairID
Entity * collidingEnt = entities[collision->getCollisionEntityID(collidingPairID)]; //The Colliding Entity
//Check the type of the collided entity and perform action
if(collidingEnt->hasComponent(TerrainComponent::getStaticID()))
{
deleteEntity(projectileEnt->entityID);
}
if(collidingEnt->hasComponent(PhysicsComponent::getStaticID()) && !collidingEnt->hasComponent(PlayerComponent::getStaticID()))
{
glm::vec2 col = collision->getMinimumTranslation(projectilePairID);
PhysicsComponent* physicsComp = static_cast<PhysicsComponent*>(collidingEnt->getComponent(PhysicsComponent::getStaticID()));
//Should 1. Be conserving energy
//Should 2. Be making sure the velocity is transfered correctly
//http://gafferongames.com/virtual-go/collision-response-and-coulomb-friction/
float j = -(1+physicsComp->coefficientRestitution)*glm::dot(projectilePhysicsComp->velocity*projectilePhysicsComp->mass,glm::normalize(col)); //projectiles impulse
float impulseMag = glm::max(j, 0.0f);
//Logger()<<impulseMag<<std::endl;
physicsComp->impulse(impulseMag*glm::normalize(-col));
projectilePhysicsComp->impulse(impulseMag*glm::normalize(col));
}
}
}
}
示例11: GhostComponent
BaseComponent* PhysicsModule::getComponent(Entity* parent, float mass, Shape s, int group,
int mask, bool hasTrigger)
{
PhysicsComponent* component;
// checking if component exists
if (components.find(parent->getName()) == components.end()) {
component = hasTrigger ? new GhostComponent((btScalar) mass, s, group, mask)
: new PhysicsComponent((btScalar) mass, s, group, mask);
component->setParent(parent);
components[parent->getName()] = component;
if (hasTrigger) {
ghosts[parent->getName()] = (GhostComponent*) component;
}
} else {
component = components[parent->getName()];
}
return component;
}
示例12: t
int Physics::castRay(float x, float y, float z, float dx, float dy, float dz, float* pos, float* normal)
{
btCollisionWorld::ClosestRayResultCallback t(btVector3(x,y,z),btVector3(dx,dy,dz));
m_physicsWorld->rayTest(btVector3(x,y,z),btVector3(dx,dy,dz),t);
PhysicsComponent* obj = (PhysicsComponent*)(t.m_collisionObject->getUserPointer());
if(obj)
{
if(pos)
{
pos[0] = t.m_hitPointWorld.x();
pos[1] = t.m_hitPointWorld.y();
pos[2] = t.m_hitPointWorld.z();
}
if(normal)
{
normal[0] = t.m_hitNormalWorld.x();
normal[1] = t.m_hitNormalWorld.y();
normal[2] = t.m_hitNormalWorld.z();
}
return obj->owner()->worldId();
}
return 0;
}
示例13: update
void RenderSystem::update(float dt, std::vector<GameObject*>* objects)
{
for (Ite i = objects->begin(); i != objects->end(); i++)
{
RenderComponent* render = (*i)->getComponent<RenderComponent>();
if (render != nullptr)
{
RectangleShape shape = *render->getDrawable();
PhysicsComponent* physics = (*i)->getComponent<PhysicsComponent>();
if (physics != nullptr)
{
Vector2f position = mScale * flipY(physics->getBody()->GetPosition());
shape.setPosition(position);
shape.setSize(1.02f * mScale * physics->getSize());
shape.setOrigin(shape.getSize() * 0.5f);
// positive direction is opposite in Box2D
shape.setRotation(-1.0f * physics->getBody()->GetAngle() * 180 / b2_pi);
}
mWindow->draw(shape);
}
}
}
示例14: GetSibling
void ModelComponent::Update(float dt)
{
Graphics* gSys = (Graphics*)Engine::GetCore()->GetSystem(ST_Graphics);
gSys->AddDebugModel(this);
Input* input = (Input*) Engine::GetCore()->GetSystem(ST_Input);
InputHandler* handler = input->GetHandler();
Transform* tr = (Transform*)GetSibling(CT_Transform);
PhysicsComponent* phys = (PhysicsComponent*) GetSibling(CT_PhysicsComponent);
if(handler->Check("MoveRight"))
{
tr->SetPosition(tr->GetPosition() + Vector3(1.0f,0.0f,0.0f) * dt);
phys->Reset();
}
Model* newModel = gSys->GetModel(model_);
if(newModel && base_ != newModel)
{
base_->RemoveInstance(this);
base_ = newModel;
base_->AddInstance(this);
}
}
示例15: btCompoundShape
void PhysicsComponent::OnHierarchyChange()
{
// Clear out old data
if(absoluteCShape != nullptr)
delete absoluteCShape;
absoluteMass = mass;
if(children.size() == 0)
{
absoluteCOG = cog;
absoluteCShape = cShape;
}
else
{
btCompoundShape* compAbsShape = new btCompoundShape();
absoluteCShape = compAbsShape;
compAbsShape->addChildShape(btTransform(), cShape);
for(auto it = children.begin(); it != children.end(); ++it)
{
PhysicsComponent* curr = static_cast<PhysicsComponent*>(*it);
compAbsShape->addChildShape(curr->getTransform(), curr->absoluteCShape);
absoluteMass += curr->absoluteMass;
}
absoluteCOG.setValue(0.0f, 0.0f, 0.0f);
absoluteCOG += cog * (mass / absoluteMass);
for(auto it = children.begin(); it != children.end(); ++it)
{
PhysicsComponent* curr = static_cast<PhysicsComponent*>(*it);
absoluteCOG += curr->cog
* (curr->absoluteMass / absoluteMass);
}
}
// Allow collision shape to be traced back to us
absoluteCShape->setUserPointer(owner);
// Only entities with no parents should have rigid bodies.
if(parent == nullptr)
{
//TODO: Update body mass and possibly motionstate
// If the body already exists, this entity was already a parent
// beforehand. We just need to update it's collision shape
// with the new absolute shape.
if(body != nullptr)
{
body->setCollisionShape(absoluteCShape);
body->setMassProps(absoluteMass, btVector3());
}
// Otherwise we need to instantiate a new rigid body and register
// it with the physics world.
else
{
btRigidBody::btRigidBodyConstructionInfo ci(absoluteMass, nullptr,
absoluteCShape);
body = new btRigidBody(ci);
}
// Set new center of gravity
btTransform comTrans;
comTrans.setOrigin(absoluteCOG);
body->setCenterOfMassTransform(comTrans);
}
// If the entitiy is no longer a parent, remove it from the world
else if(body != nullptr)
{
physMan->GetWorld()->removeRigidBody(body);
delete body;
body = nullptr;
}
// There is no else. Child objects do not get their own rigid body
}