void UTankTrack::DriveTrack() { auto ForceApplied = GetForwardVector() * CurrentThrottle * TrackMaxDrivingForce; auto ForceLocation = GetComponentLocation(); auto TankRoot = Cast<UPrimitiveComponent>(GetOwner()->GetRootComponent()); TankRoot->AddForceAtLocation(ForceApplied, ForceLocation); }
///--------------------------------------------------------------------------------- /// ///--------------------------------------------------------------------------------- inline const Vector3 Camera3D::GetUpVector() const { Vector3 cameraUp = CrossProduct( GetRightVector(), GetForwardVector() ); cameraUp.Normalize(); return cameraUp; }
void CGraphicsCamera::Dolly( double inc ) { CVector3D dir = GetForwardVector(); CVector3D temp = dir*inc; CPoint3D delta( temp.x, temp.y, temp.z ); MoveBy( delta ); }
EAngle Matrix4x4::GetAngles() const { #ifdef _DEBUG // If any of the below is not true then you have a matrix that has been scaled or reflected or something and it won't work to try to pull its Eulers bool b = fabs(GetForwardVector().LengthSqr() - 1) < 0.001f; if (!b) { TAssertNoMsg(b); return EAngle(0, 0, 0); } b = fabs(GetUpVector().LengthSqr() - 1) < 0.001f; if (!b) { TAssertNoMsg(b); return EAngle(0, 0, 0); } b = fabs(GetLeftVector().LengthSqr() - 1) < 0.001f; if (!b) { TAssertNoMsg(b); return EAngle(0, 0, 0); } b = GetForwardVector().Cross(GetLeftVector()).Equals(GetUpVector(), 0.001f); if (!b) { TAssertNoMsg(b); return EAngle(0, 0, 0); } #endif if (m[0][2] > 0.999999f) return EAngle(asin(Clamp(m[0][2], -1.0f, 1.0f)) * 180/M_PI, -atan2(m[1][0], m[1][1]) * 180/M_PI, 0); else if (m[0][2] < -0.999999f) return EAngle(asin(Clamp(m[0][2], -1.0f, 1.0f)) * 180/M_PI, -atan2(m[1][0], m[1][1]) * 180/M_PI, 0); // Clamp to [-1, 1] looping float flPitch = fmod(m[0][2], 2.0f); if (flPitch > 1) flPitch -= 2; else if (flPitch < -1) flPitch += 2; return EAngle(asin(flPitch) * 180/M_PI, -atan2(-m[0][1], m[0][0]) * 180/M_PI, atan2(-m[1][2], m[2][2]) * 180/M_PI); }
Vector Matrix4x4::GetScale() const { Vector vecReturn; vecReturn.x = GetForwardVector().Length(); vecReturn.y = GetUpVector().Length(); vecReturn.z = GetRightVector().Length(); return vecReturn; }
void UTankTrack::DriveTrack() { if (ensure(TankRoot)) { FVector ForceApplied = GetForwardVector() * CurrentThrottle * TrackMaxDrivingForce; FVector ForceLocation = GetComponentLocation(); TankRoot->AddForceAtLocation(ForceApplied, ForceLocation); } }
void UTankTrack::SetThrottle(float Throttle) { auto Name = GetName(); UE_LOG(LogTemp, Warning, TEXT("%s throttle: %f"), *Name, Throttle); auto ForceApplied = Throttle * TrackMaxDrivingForce * GetForwardVector(); auto ForceLocation = GetComponentLocation(); auto TankRootComponent = Cast<UPrimitiveComponent>(GetOwner()->GetRootComponent()); TankRootComponent->AddForceAtLocation(ForceApplied, ForceLocation); }
// Use the information embedded in a matrix to create its inverse. // http://youtu.be/7CxKAtWqHC8 Matrix4x4 Matrix4x4::InvertedTR() const { // This method can only be used if the matrix is a translation/rotation matrix. // The below asserts will trigger if this is not the case. TAssert(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001f); // Each basis vector should be length 1. TAssert(fabs(GetUpVector().LengthSqr() - 1) < 0.00001f); TAssert(fabs(GetRightVector().LengthSqr() - 1) < 0.00001f); TAssert(fabs(GetForwardVector().Dot(GetUpVector())) < 0.0001f); // All vectors should be orthogonal. TAssert(fabs(GetForwardVector().Dot(GetRightVector())) < 0.0001f); TAssert(fabs(GetRightVector().Dot(GetUpVector())) < 0.0001f); Matrix4x4 M; // Create the transposed upper 3x3 matrix for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) M.m[i][j] = m[j][i]; // The new matrix translation = -Rt M.SetTranslation(-(M*GetTranslation())); return M; }
DoubleMatrix4x4 DoubleMatrix4x4::InvertedRT() const { TAssertNoMsg(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001); TAssertNoMsg(fabs(GetLeftVector().LengthSqr() - 1) < 0.00001); TAssertNoMsg(fabs(GetUpVector().LengthSqr() - 1) < 0.00001); DoubleMatrix4x4 r; for (int h = 0; h < 3; h++) for (int v = 0; v < 3; v++) r.m[h][v] = m[v][h]; r.SetTranslation(r*(-GetTranslation())); return r; }
// Not a true inversion, only works if the matrix is a translation/rotation matrix. void Matrix4x4::InvertRT() { TAssertNoMsg(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001f); TAssertNoMsg(fabs(GetLeftVector().LengthSqr() - 1) < 0.00001f); TAssertNoMsg(fabs(GetUpVector().LengthSqr() - 1) < 0.00001f); Matrix4x4 t; for (int h = 0; h < 3; h++) for (int v = 0; v < 3; v++) t.m[h][v] = m[v][h]; Vector vecTranslation = GetTranslation(); Init(t); SetTranslation(t*(-vecTranslation)); }
Ray Camera::GetScreenRay(float x, float y) { Ray ret; // If projection is invalid, just return a ray pointing forward if (!IsProjectionValid()) { ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO; ret.direction_ = GetForwardVector(); return ret; } Matrix4 viewProjInverse = (GetProjection(false) * GetInverseWorldTransform()).Inverse(); // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis x = 2.0f * x - 1.0f; y = 1.0f - 2.0f * y; Vector3 near(x, y, 0.0f); Vector3 far(x, y, 1.0f); ret.origin_ = viewProjInverse * near; ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized(); return ret; }
explicit PawnPath(const APawn &Walker) : Start(GetLocation(Walker)), End(GetLocation(Walker) + GetForwardVector(Walker) * WALKER_SIGHT_RADIUS) {}
void ModuleControl::OnEvent( const SEvent& event ) { // 鼠标消息处理 if (event.EventType == EET_MOUSE_INPUT_EVENT) { // 滚轮处理 if (event.MouseInput.Wheel == -1) { vector3df vForward; GetForwardVector(vForward); ModuleposRelateToCamaraVector = ModuleposRelateToCamaraVector*2; setModuleposRelateToCamara(ModuleposRelateToCamaraVector); } if (event.MouseInput.Wheel == 1) { vector3df vForward; GetForwardVector(vForward); ModuleposRelateToCamaraVector = ModuleposRelateToCamaraVector/2; setModuleposRelateToCamara(ModuleposRelateToCamaraVector); } IrrlichtDevice* device = MyIrrlichtEngine::GetEngine()->GetDevice(); CursorPos = device->getCursorControl()->getRelativePosition(); if (CursorPos.equals(CenterCursor)) { return; } if (event.MouseInput.isLeftPressed()) { vector3df v = pModule->getPosition(); matrix4 vm = pCamara->getProjectionMatrix(); //pCamara->get //f32 data[4]; //data[0] = v.X; //data[1] = v.Y; //data[2] = v.Z; //data[3] = 1; //vm.multiplyWith1x4Matrix(data); //std::cout << "a" <<std::endl; } // 鼠标移动处理 f32 xdelta = (CursorPos.Y - CenterCursor.Y)*30; f32 ydelta = (CursorPos.X - CenterCursor.X)*30; if (m_vCamaraRotation.X >= 85.0 && xdelta > 0) { xdelta = 0; } if (m_vCamaraRotation.X <= -85.0 && xdelta < 0) { xdelta = 0; } m_vCamaraRotation += vector3df(xdelta, 0.0, 0.0); m_vCamaraRotation += vector3df(0.0, ydelta, 0.0); SetCamaraRotation(m_vCamaraRotation); device->getCursorControl()->setPosition(CenterCursor); } // 按键消息处理 if (event.EventType == EET_KEY_INPUT_EVENT) { if (event.KeyInput.Key == KEY_KEY_W ) { } if (event.KeyInput.Key == KEY_KEY_S ) { } if (event.KeyInput.Key == KEY_KEY_A ) { m_vModuleRotation += vector3df(0.f, 0.f, 1.f); pModule->setRotation(m_vModuleRotation); } if (event.KeyInput.Key == KEY_KEY_D ) { m_vModuleRotation += vector3df(0.f, 0.f, -1.f); pModule->setRotation(m_vModuleRotation); } if (event.KeyInput.Key == KEY_UP) { } if (event.KeyInput.Key == KEY_DOWN) { } if (event.KeyInput.Key == KEY_LEFT) { } if (event.KeyInput.Key == KEY_RIGHT) { } } }
void Bullet::UpdatePosition(double &dt) { Translate(GetForwardVector() * speed * dt); }
void Camera3D::UpdateCameraFromInput(float deltaSeconds, const float& moveSpeed){ const float degreesPerMouseDelta = 0.08f; float moveSpeedUnitsPerSecond = moveSpeed; const float flySpeedUnitsPerSecond = moveSpeed * 0.5f; const float turboMultiplier = 8.0f; float yawRadians = ConvertDegreesToRadians(m_orientation.yawDegreesAboutZ); UNUSED(yawRadians); //this allows movement absolute to the world basis //move camera forward according to yaw Vector3 cameraForwardXY;// = Vector3(cos(yawRadians), sin(yawRadians), 0.0f); //define forward cameraForwardXY = GetForwardVector(); cameraForwardXY.Normalize(); //cameraForwardXY = Vector3::FORWARD; //move camera left according to yaw Vector3 cameraLeftXY = Vector3(-cameraForwardXY.y, cameraForwardXY.x, 0); //move camera right according to yaw Vector3 cameraRightXY = -1.0f * cameraLeftXY; //move camera backward according to yaw Vector3 cameraBackwardXY = -1.0f * cameraForwardXY; Vector3 cameraMovementVector(0.0f, 0.0f, 0.0f); //move in a direction if (theInputSystem->IsKeyDown('W') || theInputSystem->IsKeyDown(VK_UP)){ cameraMovementVector += cameraForwardXY * moveSpeedUnitsPerSecond * deltaSeconds; } else if (theInputSystem->IsKeyDown('S') || theInputSystem->IsKeyDown(VK_DOWN)){ cameraMovementVector += cameraBackwardXY * moveSpeedUnitsPerSecond * deltaSeconds; } else if (theInputSystem->IsKeyDown('A') || theInputSystem->IsKeyDown(VK_LEFT)){ cameraMovementVector += cameraLeftXY * moveSpeedUnitsPerSecond * deltaSeconds; } else if (theInputSystem->IsKeyDown('D') || theInputSystem->IsKeyDown(VK_RIGHT)){ cameraMovementVector += cameraRightXY * moveSpeedUnitsPerSecond * deltaSeconds; } //keyboard flight else if (theInputSystem->IsKeyDown('Q')){ cameraMovementVector.z += flySpeedUnitsPerSecond * deltaSeconds; } else if (theInputSystem->IsKeyDown('E')){ cameraMovementVector.z -= flySpeedUnitsPerSecond * deltaSeconds; } //turn on turbo, turbo only increases move speed if (theInputSystem->IsKeyDown(VK_SHIFT)){ cameraMovementVector *= turboMultiplier; } //adding the combined movement vector to camera pos m_position += cameraMovementVector; Vector2 mouseResetPosition (400.0f , 300.0f ); Vector2 mousePosition = theInputSystem->GetMousePosition(); //theInputSystem->ConsolePrintMousePosition(); Vector2 distanceMouseHasMovedSinceLastFrame = mousePosition - mouseResetPosition; if(!theInputSystem->IsKeyDown(VK_CONTROL)) theInputSystem->SnapMousePosition((int)mouseResetPosition.x, (int)mouseResetPosition.y ); m_orientation.yawDegreesAboutZ -= distanceMouseHasMovedSinceLastFrame.x * degreesPerMouseDelta * deltaSeconds; m_orientation.pitchDegreesAboutY += distanceMouseHasMovedSinceLastFrame.y * degreesPerMouseDelta* deltaSeconds; //clamp pitch degrees if(m_orientation.pitchDegreesAboutY > 89.0f ) m_orientation.pitchDegreesAboutY = 89.0f; if(m_orientation.pitchDegreesAboutY < -89.0f ) m_orientation.pitchDegreesAboutY = -89.0f; }
//Gets a plane at the focal point perpendicular to the forward vector CPlane3D CGraphicsCamera::GetFocalPlane() { return CPlane3D( GetForwardVector(), m_focalPt ); }
CVector3D CGraphicsCamera::GetRightVector() { CVector3D forward = GetForwardVector(); return forward.cross( m_up ).normalize(); }
explicit PawnPath(const AWheeledVehicle &Vehicle) : PawnPath(GetLocation(Vehicle), GetForwardVector(Vehicle), GetForwardSpeed(Vehicle)) {}