void GameObject::tick() { if (!m_HasStarted) { start(); m_HasStarted = true; } if (m_GoToNewPoint) { CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID()); if (physicsComponent != 0) { b2Body* body = physicsComponent->getBody(); float angle = body->GetAngle(); body->SetTransform(m_NewPoint, angle); } m_GoToNewPoint = false; } for (map<int, Component*>::iterator it = m_Components->begin(); it != m_Components->end(); it++) { Component* component = it->second; component->tick(); } for (set<GameObject*>::iterator it = m_Children->begin(); it != m_Children->end(); it++) { GameObject* child = *it; child->tick(); } }
void GOBall::updateVelocity() { // get desired move velocity CController* controller = (CController*)getComponent(CController::classTypeID()); float moveVelocity = controller->getMoveDirection() * VEL_MAX; // get actual velocity CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID()); b2Body* body = physicsComponent->getBody(); b2Vec2 position = body->GetPosition(); b2Vec2 velocity = body->GetLinearVelocity(); // calculate difference and product float product = velocity.x * moveVelocity; float difference = moveVelocity - velocity.x; // if moving, change move velocity /*if (moveVelocity != 0) { std::cout << "MOVING\n" << std::flush; } else { std::cout << "NOT MOVING\n" << std::flush; }*/ if (moveVelocity != 0.0f && (product < 0.0f || fabs(velocity.x) < fabs(moveVelocity) )) { float force = difference * FORCE_CONST; body->ApplyForce(b2Vec2(force, 0.0f), position); } else { float force = -velocity.x * FORCE_CONST * 0.2f; body->ApplyForce(b2Vec2(force, 0.0f), position); } }
void GOBall::jump() { CController* controller = (CController*)getComponent(CController::classTypeID()); if (controller->getJump()) { CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID()); b2Body* body = physicsComponent->getBody(); b2Vec2 velocity = body->GetLinearVelocity(); body->SetLinearVelocity(b2Vec2(velocity.x, JUMP_CONST)); } }
void GOBall::tick() { GameObject::tick(); if (!m_Alive) return; m_ScoreObject->updateScore(m_Score); jump(); updateVelocity(); // sync the position of the physics and graphics objects CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID()); b2Body* body = physicsComponent->getBody(); b2Vec2 position = body->GetPosition(); float32 angle = body->GetAngle(); if (position.y < -200) spawn(); CGraphicsObject* graphicsComponent = (CGraphicsObject*)getComponent(CGraphicsObject::classTypeID()); graphicsComponent->setPosition(position.x, position.y, 0.0f); }
const b2Vec2& GOBall::getPosition() { CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID()); b2Body* body = physicsComponent->getBody(); return body->GetPosition(); }