int currentDirection() { // int head = (int)round(currentHeading() / (M_PI / 2)); // if (head == 4) { // head = 0; // } // printf("\n\n\n%i\n\n\n", (int)round(currentHeading() / (M_PI / 2))); return (int)round(currentHeading() / (M_PI / 2)); }
void turnToDirection(int turnDirection) { adjustAngle(); int currentDir; currentDir = currentDirection(); double target = (currentDir + turnDirection) * M_PI / 2; while(fabs(currentHeading() - target) > turnThreshold) { if(fabs(currentHeading() - target) < slowThreshold) { if(currentHeading() > target) { set_motors(-5,5); } if(currentHeading() < target) { set_motors(5,-5); } } else { if(currentHeading() > target) { set_motors(-15,15); } if(currentHeading() < target) { set_motors(15,-15); } } calcPos(); } set_motors(0,0); adjustAngle(); usleep(10000); }
void turn90() { while( fabs(currentHeading() - (M_PI/2.0)) > turnThreshold) { if(fabs(currentHeading() - (M_PI/2.0)) < slowThreshold) { if(currentHeading() > (M_PI/2.0)) { set_motors(-1,1); } if(currentHeading() < (M_PI/2.0)) { set_motors(1,-1); } } else { if(currentHeading() > (M_PI/2.0)) { set_motors(-15,15); } if(currentHeading() < (M_PI/2.0)) { set_motors(15,-15); } } calcPos(); } set_motors(0,0); printf("Target : %f\n", M_PI/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)); }