本文整理汇总了C++中b2Vec2::Normalize方法的典型用法代码示例。如果您正苦于以下问题:C++ b2Vec2::Normalize方法的具体用法?C++ b2Vec2::Normalize怎么用?C++ b2Vec2::Normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类b2Vec2
的用法示例。
在下文中一共展示了b2Vec2::Normalize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: accumForce
bool Neural_SB::accumForce(b2Vec2 & totForce, b2Vec2 force) const
{
//std::cout << "\t\tTotalForce : " << totForce.x << " " << totForce.y << std::endl;
b2Vec2 tot = totForce + force;
if( tot.LengthSquared() <= m_fMaxForce*m_fMaxForce )
{
totForce += force;
return true;
}
force.Normalize();
force *= abs(m_fMaxForce - totForce.Length());
totForce += force;
return false;
}
示例2: onFire
void Flamethrower::onFire(b2Vec2 direction, bool constantFire, bool isPlayer)
{
direction.Normalize();
b2Mat22 matrix1 = b2Mat22(degree2radian(float(rand() % 600) / 100.0f));
b2Mat22 matrix2 = b2Mat22(degree2radian(float(rand() % 600) / 100.0f));
b2Mat22 matrix3 = b2Mat22(degree2radian(float(rand() % 600 - 300) / 100.0f));
direction = b2Mul(matrix1, direction);
b2Vec2 direction2 = b2Mul(matrix2, direction);
b2Vec2 direction3 = b2Mul(matrix3, direction);
EntityFactory *entityFactory = new FlamethrowerBulletFactory();
EntityProperties& properties = entityFactory->getDefaultProperties();
properties.x = meter2pixel(this->getBody(0)->GetPosition().x);
properties.y = meter2pixel(this->getBody(0)->GetPosition().y);
properties.special = true;
float speed = 0.7f;
FlamethrowerBullet *flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.75f * direction, this->getBody(0)->GetPosition());
flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.75f * direction2, this->getBody(0)->GetPosition());
flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.82f * direction3, this->getBody(0)->GetPosition());
}
示例3: update
bool GameLevelState::update(yam2d::ESContext* _context, float _deltaTime)
{
compFac->getPhysicsWorld()->Step(_deltaTime, 10, 10);
map->update(_deltaTime);
static bool ballHit = false;
static bool ballHitThisFrame;
static b2Vec2 directionVec;//Vector from player to ball when hit
ballHitThisFrame = false;
//Collisions
unsigned int numContacts = contactListener->contacts.size();
for (unsigned int i = 0; i < numContacts; i++)
{
const Contact& contact = contactListener->contacts[i];
yam2d::GameObject* A = ((PhysicsBody*) contact.fixtureA->GetBody()->GetUserData())->getGameObject();
yam2d::GameObject* B = ((PhysicsBody*) contact.fixtureB->GetBody()->GetUserData())->getGameObject();
if ((A->getName() == "Ball" && B->getName() == "Player"))
{
//Physics
if (!ballHit)
{
//Reverse last movements ( separate objects )
A->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballVelocity.x, A->getComponent<PhysicsBody>()->getBody()->GetPosition().y - ballVelocity.y), A->getComponent<PhysicsBody>()->getBody()->GetAngle());
B->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - playerSpeed.x, B->getComponent<PhysicsBody>()->getBody()->GetPosition().y - playerSpeed.y), B->getComponent<PhysicsBody>()->getBody()->GetAngle());
//Direction
directionVec = A->getComponent<PhysicsBody>()->getBody()->GetPosition() - B->getComponent<PhysicsBody>()->getBody()->GetPosition();
directionVec.Normalize();
directionVec *= 0.1f;
//Check for side hits
if (A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f <
B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5f
||
A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f >
B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5f)
{
ballVelocity.x = -ballVelocity.x + playerSpeed.x;
ballVelocity.y -= directionVec.y;
}
else //No side hit
{
ballVelocity.y = -ballVelocity.y;
}
ballVelocity.x -= (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x) / 22.0f;
ballHit = true;
ballHitThisFrame = true;
}
}
else if ((B->getName() == "Ball" && A->getName() == "Player"))
{
//Physics
if (!ballHit)
{
//Reverse last movements ( separate objects )
B->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballVelocity.x, B->getComponent<PhysicsBody>()->getBody()->GetPosition().y - ballVelocity.y), B->getComponent<PhysicsBody>()->getBody()->GetAngle());
A->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - playerSpeed.x, A->getComponent<PhysicsBody>()->getBody()->GetPosition().y - playerSpeed.y), A->getComponent<PhysicsBody>()->getBody()->GetAngle());
//Direction
directionVec = B->getComponent<PhysicsBody>()->getBody()->GetPosition() - A->getComponent<PhysicsBody>()->getBody()->GetPosition();
directionVec.Normalize();
directionVec *= 0.1f;
//Check for side hits
if (B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5f <
A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f
||
B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5f >
A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f)
{
ballVelocity.x = -ballVelocity.x + playerSpeed.x;
ballVelocity.y -= directionVec.y;
}
else //No side hit
{
ballVelocity.y = -ballVelocity.y;
}
ballVelocity.x -= (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x) / 22.0f;
ballHit = true;
ballHitThisFrame = true;
}
}
if ((A->getName() == "Ball" && B->getName() == "Tile"))
{
//Physics
if (!ballHit)
{
//Check for side hits
if (A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f <
B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5
||
A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f >
B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5)
{
ballVelocity.x = -ballVelocity.x;
}
//.........这里部分代码省略.........