示例#1
0
int main(int argc, char** argv) {
    initTruck();
    int start_time = timeInSecs();
    long double start_lat = getLatitude();
    long double start_lon = getLongitude();
    int start_heading = getHeading();
    int phase = 0;

    setSteering(0);
    setThrottle(50);
    while(TRUE){

        int target_heading;
        if(phase == 0){
            if(distance(start_lat, getLatitude(), start_lon, getLongitude())>5){
                setThrottle(30);
                setSteering(60);
                phase = 1;
                target_heading = newHeading(90);
                }
        }
        else if (phase == 1){
            if(getHeading()==target_heading){
                setSteering(-60);
                target_heading=start_heading;
                phase = 2;

            }
        }
        else if (phase == 2){
            if(getHeading()==target_heading){
                setSteering(0);
                setThrottle(0);
                while(TRUE);
            }
        }

        background();
    }
    return (EXIT_SUCCESS);
}
示例#2
0
void CharacterGame::update(long elapsedTime)
{
    Vector2 direction;

    if (_gamepad->getButtonState(0) == Gamepad::BUTTON_PRESSED)
    {
        // Jump while the gamepad button is being pressed
        jump();
    }

	if (_gamepad->isJoystickActive(0))
    {
        // Get joystick direction
        direction = _gamepad->getJoystickState(0);
    }
    else
    {
        // Construct direction vector from keyboard input
        if (_keyFlags & NORTH)
            direction.y = 1;
        else if (_keyFlags & SOUTH)
            direction.y = -1;
        if (_keyFlags & EAST)
            direction.x = 1;
        else if (_keyFlags & WEST)
            direction.x = -1;

        direction.normalize();
        if ((_keyFlags & RUNNING) == 0)
            direction *= 0.5f;
    }

    // Update character animation and velocity
    if (direction.isZero())
    {
        play("idle", true);
        _character->setVelocity(Vector3::zero());
    }
    else
    {
        bool running = (direction.lengthSquared() > 0.75f);
        float speed = running ? RUN_SPEED : WALK_SPEED;

        play(running ? "running" : "walking", true, 1.0f);

        // Orient the character relative to the camera so he faces the direction we want to move.
        const Matrix& cameraMatrix = _scene->getActiveCamera()->getNode()->getWorldMatrix();
        Vector3 cameraRight, cameraForward;
        cameraMatrix.getRightVector(&cameraRight);
        cameraMatrix.getForwardVector(&cameraForward);

        // Get the current forward vector for the mesh node (negate it since the character was modelled facing +z)
        Vector3 currentHeading(-_characterMeshNode->getForwardVectorWorld());

        // Construct a new forward vector for the mesh node
        Vector3 newHeading(cameraForward * direction.y + cameraRight * direction.x);

        // Compute the rotation amount based on the difference between the current and new vectors
        float angle = atan2f(newHeading.x, newHeading.z) - atan2f(currentHeading.x, currentHeading.z);
        if (angle > MATH_PI)
            angle -= MATH_PIX2;
        else if (angle < -MATH_PI)
            angle += MATH_PIX2;
        angle *= (float)elapsedTime * 0.001f * MATH_PIX2;
        _characterMeshNode->rotate(Vector3::unitY(), angle);

        // Update the character's velocity
        Vector3 velocity = -_characterMeshNode->getForwardVectorWorld();
        velocity.normalize();
        velocity *= speed;
        _character->setVelocity(velocity);
    }

    // Adjust camera to avoid it from being obstructed by walls and objects in the scene.
    adjustCamera(elapsedTime);

    // Project the character's shadow node onto the surface directly below him.
    PhysicsController::HitResult hitResult;
    Vector3 v = _character->getNode()->getTranslationWorld();
    if (getPhysicsController()->rayTest(Ray(Vector3(v.x, v.y + 1.0f, v.z), Vector3(0, -1, 0)), 100.0f, &hitResult))
        _characterShadowNode->setTranslation(Vector3(hitResult.point.x, hitResult.point.y + 0.1f, hitResult.point.z));
}