Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubNei committed Apr 12, 2017
1 parent 7b2e12b commit a730ae3
Showing 1 changed file with 112 additions and 112 deletions.
224 changes: 112 additions & 112 deletions mygame/FirstPersonCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class FirstPersonCamera : ComponentWithShortcuts
int scrollWheelValue;
Vector3 currentVelocity;

Vector3 walkOnSphere_lastVectorUp;
Vector3 walkOnSphere_vectorForward;
bool walkOnShere_start;
Vector3 walkOnSphere_lastUp;
Vector3 walkOnSphere_lastForward;
bool walkOnSphere_isFirstRun;

Camera cam => Scene.MainCamera;
CVar WalkOnPlanet => Debug.GetCVar("game / walk on planet");
Expand All @@ -56,7 +56,7 @@ void Start()
{
if (cvar.Bool)
{
walkOnShere_start = true;
walkOnSphere_isFirstRun = true;
}
});

Expand All @@ -66,6 +66,8 @@ void Start()
Transform.LookAt(planet.Transform.Position);
if (moveCameraToSurfaceOnStart)
Transform.Position = new WorldPos((float)-planet.RadiusMin, 0, 0) + planet.Transform.Position;

Update(0.1f); // spool up
}

WorldPos savedPosition1;
Expand Down Expand Up @@ -145,170 +147,168 @@ void Update(float deltaTime)
}


if (Input.LockCursor == false) return;

if (scrollWheelDelta > 0) cameraSpeedModifier *= 1.3f;
if (scrollWheelDelta < 0) cameraSpeedModifier /= 1.3f;
cameraSpeedModifier = MyMath.Clamp(cameraSpeedModifier, 1, 100000);
float currentSpeed = cameraSpeedModifier;


var planetLocalPosition = planet.Transform.Position.Towards(position).ToVector3d();
var sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
var onPlanetSurfaceHeight = planet.GetSurfaceHeight(planetLocalPosition);
var onPlanetDistanceToSurface = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;

{
var planetLocalPosition = planet.Transform.Position.Towards(position).ToVector3d();
var sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
var onPlanetSurfaceHeight = planet.GetSurfaceHeight(planetLocalPosition);
var onPlanetDistanceToSurface = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;
Debug.AddValue("camera / distance to surface", onPlanetDistanceToSurface);

{
var s = MyMath.SmoothStep(1, 30000, (float)onPlanetDistanceToSurface);
cam.NearClipPlane = 1000 * s + 0.5f;
cam.FarClipPlane = 5000000 * s + 100000;
cam.Recalculate();
}
}


if (Input.LockCursor)
{

if (scrollWheelDelta > 0) cameraSpeedModifier *= 1.3f;
if (scrollWheelDelta < 0) cameraSpeedModifier /= 1.3f;
cameraSpeedModifier = MyMath.Clamp(cameraSpeedModifier, 1, 100000);
float currentSpeed = cameraSpeedModifier;


if (speedBasedOnDistanceToPlanet)
{
var s = MyMath.Clamp(onPlanetDistanceToSurface, 1, 30000);
currentSpeed *= (1 + (float)s / 5.0f);
}

}



if (Input.GetKey(Key.ShiftLeft)) currentSpeed *= 5;

Debug.AddValue("camera / real speed", currentSpeed);
if (Input.GetKey(Key.ShiftLeft)) currentSpeed *= 5;

var targetVelocity = Vector3.Zero;
if (Input.GetKey(Key.W)) targetVelocity += currentSpeed * Constants.Vector3Forward;
if (Input.GetKey(Key.S)) targetVelocity -= currentSpeed * Constants.Vector3Forward;
if (Input.GetKey(Key.D)) targetVelocity += currentSpeed * Constants.Vector3Right;
if (Input.GetKey(Key.A)) targetVelocity -= currentSpeed * Constants.Vector3Right;
if (Input.GetKey(Key.Space)) targetVelocity += currentSpeed * Constants.Vector3Up;
if (Input.GetKey(Key.ControlLeft)) targetVelocity -= currentSpeed * Constants.Vector3Up;
Debug.AddValue("camera / real speed", currentSpeed);

//var pos = Matrix4.CreateTranslation(targetVelocity);
var targetVelocity = Vector3.Zero;
if (Input.GetKey(Key.W)) targetVelocity += currentSpeed * Constants.Vector3Forward;
if (Input.GetKey(Key.S)) targetVelocity -= currentSpeed * Constants.Vector3Forward;
if (Input.GetKey(Key.D)) targetVelocity += currentSpeed * Constants.Vector3Right;
if (Input.GetKey(Key.A)) targetVelocity -= currentSpeed * Constants.Vector3Right;
if (Input.GetKey(Key.Space)) targetVelocity += currentSpeed * Constants.Vector3Up;
if (Input.GetKey(Key.ControlLeft)) targetVelocity -= currentSpeed * Constants.Vector3Up;

//var pos = Matrix4.CreateTranslation(targetVelocity);

float pitchDelta = 0;
float yawDelta = 0;
float rollDelta = 0;

float c = mouseSensitivty * (float)deltaTime;
yawDelta += mouseDelta.X * c;
pitchDelta += mouseDelta.Y * c;
float pitchDelta = 0;
float yawDelta = 0;
float rollDelta = 0;

if (Input.GetKey(Key.Q)) rollDelta -= c;
if (Input.GetKey(Key.E)) rollDelta += c;
float c = mouseSensitivty * (float)deltaTime;
yawDelta += mouseDelta.X * c;
pitchDelta += mouseDelta.Y * c;

if (Input.GetKey(Key.Q)) rollDelta -= c;
if (Input.GetKey(Key.E)) rollDelta += c;

if (Input.GetKeyDown(Key.C))
{
rotation = position.Towards(planet.Transform.Position).ToVector3().LookRot();
}

if (WalkOnPlanet.Bool)
{

var up = planet.Center.Towards(position).ToVector3().Normalized();
var fwd = walkOnSphere_vectorForward;
if (Input.GetKeyDown(Key.C))
{
rotation = position.Towards(planet.Transform.Position).ToVector3().LookRot();
}

if (walkOnShere_start)
if (WalkOnPlanet.Bool)
{
walkOnSphere_lastVectorUp = up;

var pointOnPlanet = planet.Center.Towards(position).ToVector3d();
var s = planet.CalestialToSpherical(pointOnPlanet);
s.latitude += 0.1f;
var fwdToPole = pointOnPlanet.Towards(planet.SphericalToCalestial(s)).Normalized().ToVector3().Normalized();
var up = planet.Center.Towards(position).ToVector3().Normalized();
var forward = walkOnSphere_lastForward;

fwd = Constants.Vector3Forward.RotateBy(rotation);
}
if (walkOnSphere_isFirstRun)
{
walkOnSphere_lastUp = up;

if (!walkOnShere_start)
{
var upDeltaAngle = up.Angle(walkOnSphere_lastVectorUp);
var upDeltaRot = Quaternion.FromAxisAngle(up.Cross(walkOnSphere_lastVectorUp), upDeltaAngle).Inverted();
var pointOnPlanet = planet.Center.Towards(position).ToVector3d();
var s = planet.CalestialToSpherical(pointOnPlanet);
//s.latitude += 0.1f;
//var forwardToPole = pointOnPlanet.Towards(planet.SphericalToCalestial(s)).Normalized().ToVector3().Normalized();
forward = Constants.Vector3Forward.RotateBy(rotation);
}
else
{
var upDeltaAngle = up.Angle(walkOnSphere_lastUp);
var upDeltaRot = Quaternion.FromAxisAngle(up.Cross(walkOnSphere_lastUp), upDeltaAngle).Inverted();

fwd = fwd.RotateBy(upDeltaRot);
}
forward = forward.RotateBy(upDeltaRot);
}


var left = up.Cross(fwd);
var left = up.Cross(forward);

var rotDelta =
Quaternion.FromAxisAngle(up, -yawDelta) *
Quaternion.FromAxisAngle(left, pitchDelta);
var rotDelta =
Quaternion.FromAxisAngle(up, -yawDelta) *
Quaternion.FromAxisAngle(left, pitchDelta);


fwd = fwd.RotateBy(rotDelta);
forward = forward.RotateBy(rotDelta);

{
// clamping up down rotation
var maxUpDownAngle = 80;
var minUp = MyMath.ToRadians(90 - maxUpDownAngle);
var maxDown = MyMath.ToRadians(90 + maxUpDownAngle);
var angle = fwd.Angle(up);
if (angle < minUp)
fwd = up.RotateBy(Quaternion.FromAxisAngle(left, minUp));
else if (angle > maxDown)
fwd = up.RotateBy(Quaternion.FromAxisAngle(left, maxDown));
}
{
// clamping up down rotation
var maxUpDownAngle = 80;
var minUp = MyMath.ToRadians(90 - maxUpDownAngle);
var maxDown = MyMath.ToRadians(90 + maxUpDownAngle);
var angle = forward.Angle(up);
if (angle < minUp)
forward = up.RotateBy(Quaternion.FromAxisAngle(left, minUp));
else if (angle > maxDown)
forward = up.RotateBy(Quaternion.FromAxisAngle(left, maxDown));
}


fwd.Normalize();
up.Normalize();
forward.Normalize();

rotation = QuaternionUtility.LookRotation(fwd, up);
rotation = QuaternionUtility.LookRotation(forward, up);

walkOnSphere_vectorForward = fwd;
walkOnSphere_lastVectorUp = up;
walkOnShere_start = false;
walkOnSphere_lastForward = forward;
walkOnSphere_lastUp = up;
walkOnSphere_isFirstRun = false;

}
else
{
var rotDelta =
Quaternion.FromAxisAngle(-Vector3.UnitX, pitchDelta) *
Quaternion.FromAxisAngle(-Vector3.UnitY, yawDelta) *
Quaternion.FromAxisAngle(-Vector3.UnitZ, rollDelta);
}
else
{
var rotDelta =
Quaternion.FromAxisAngle(-Vector3.UnitX, pitchDelta) *
Quaternion.FromAxisAngle(-Vector3.UnitY, yawDelta) *
Quaternion.FromAxisAngle(-Vector3.UnitZ, rollDelta);

rotation = rotation * rotDelta;
}
rotation = rotation * rotDelta;

walkOnSphere_isFirstRun = true;
}


Transform.Rotation = rotation;

targetVelocity = targetVelocity.RotateBy(Transform.Rotation);
currentVelocity = Vector3.Lerp(currentVelocity, targetVelocity, velocityChangeSpeed * (float)deltaTime);
Transform.Rotation = rotation;

position += currentVelocity * (float)deltaTime;
targetVelocity = targetVelocity.RotateBy(Transform.Rotation);
currentVelocity = Vector3.Lerp(currentVelocity, targetVelocity, velocityChangeSpeed * (float)deltaTime);

// make cam on top of the planet
if (collideWithPlanetSurface)
{
var planetLocalPosition = planet.Transform.Position.Towards(position).ToVector3d();
var sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
var onPlanetSurfaceHeight = planet.GetSurfaceHeight(planetLocalPosition);
var onPlanetDistanceToSurface = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;
position += currentVelocity * (float)deltaTime;

var h = onPlanetSurfaceHeight + 2;
if (sphericalPlanetLocalPosition.altitude <= h || WalkOnPlanet.Bool)
// make cam on top of the planet
if (collideWithPlanetSurface)
{
sphericalPlanetLocalPosition.altitude = h;
position = planet.Transform.Position + planet.SphericalToCalestial(sphericalPlanetLocalPosition);
planetLocalPosition = planet.Transform.Position.Towards(position).ToVector3d();
sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
onPlanetSurfaceHeight = planet.GetSurfaceHeight(planetLocalPosition);
onPlanetDistanceToSurface = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;

var h = onPlanetSurfaceHeight + 2;
if (sphericalPlanetLocalPosition.altitude <= h || WalkOnPlanet.Bool)
{
sphericalPlanetLocalPosition.altitude = h;
position = planet.Transform.Position + planet.SphericalToCalestial(sphericalPlanetLocalPosition);
}
}
}


Transform.Position = position; // += Entity.Transform.Position.Towards(position).ToVector3d() * deltaTime * 10;

//Log.Info(entity.transform.position);
Transform.Position = position; // += Entity.Transform.Position.Towards(position).ToVector3d() * deltaTime * 10;

//Log.Info(entity.transform.position);
}

}

Expand Down

0 comments on commit a730ae3

Please sign in to comment.