Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Last active November 11, 2016 20:47
Show Gist options
  • Save MrSmith33/08c9df07f5f53ee8c0dda3d756422b3c to your computer and use it in GitHub Desktop.
Save MrSmith33/08c9df07f5f53ee8c0dda3d756422b3c to your computer and use it in GitHub Desktop.
Physics for voxelman
static struct InputState
{
ivec3 inputs = ivec3(0,0,0);
int boost = 1;
bool jump;
}
InputState gatherInputs()
{
InputState state;
if(guiPlugin.mouseLocked)
{
if(input.isKeyPressed("key.fast")) state.boost = 1;
if(input.isKeyPressed("key.right")) state.inputs.x = 1;
else if(input.isKeyPressed("key.left")) state.inputs.x = -1;
if(input.isKeyPressed("key.forward")) state.inputs.z = 1;
else if(input.isKeyPressed("key.backward")) state.inputs.z = -1;
if(input.isKeyPressed("key.up")) {
state.inputs.y = 1;
state.jump = true;
} else if(input.isKeyPressed("key.down")) state.inputs.y = -1;
}
return state;
}
// returns position delta
vec3 handleWalk(ref vec3 speed, float dt)
{
InputState state = gatherInputs();
vec3 inputAccel = vec3(0,0,0);
if (onGround)
{
uint cameraSpeed = cameraSpeedOpt.get!uint;
vec3 horInputs = vec3(state.inputs.x, 0, state.inputs.z);
if (horInputs != vec3(0,0,0))
horInputs.normalize();
horInputs *= cameraSpeed;
vec3 cameraMovement = toCameraCoordinateSystem(horInputs);
if (dt > 0) {
inputAccel.x = cameraMovement.x / dt;
inputAccel.z = cameraMovement.z / dt;
}
if (state.jump)
{
speed.y = jumpSpeed;
onGround = false;
}
//if (horInputs == vec3(0,0,0))
//{
speed.x = 0;
speed.z = 0;
//}
}
vec3 accel = vec3(0, -gravity, 0) + inputAccel;
//float speedScalar = speed.length;
//float airFrictionMult = speedScalar / maxSpeed;
//vec3 airFriction = - accel * airFrictionMult;
float speedScalar = speed.y;
float airFrictionMult = speedScalar / maxSpeed;
vec3 airFriction = vec3(0, - accel.y * airFrictionMult, 0);
vec3 groundFriction = vec3(0,0,0);
if (onGround)
{
//groundFriction
}
vec3 fullAcceleration = groundFriction + airFriction + accel;
speed += fullAcceleration * dt;
vec3 targetDelta = speed * dt;
return targetDelta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment