Skip to content

Commit

Permalink
NewtonCharacterComponent.enableGravity; FirstPersonViewComponent.dire…
Browse files Browse the repository at this point in the history
…ction, FirstPersonViewComponent.directionHorizontal, FirstPersonViewComponent.right, FirstPersonViewComponent.up
  • Loading branch information
gecko0307 committed Jan 15, 2025
1 parent 858c12e commit 31a95bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
17 changes: 14 additions & 3 deletions extensions/newton/src/dagon/ext/newton/character.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
NewtonSphereShape upperShape;
NewtonCompoundShape shape;
NewtonRigidBody rbody;
float height;
float mass;

Vector3f gravity = Vector3f(0.0f, -20.0f, 0.0f);

Vector3f targetVelocity = Vector3f(0.0f, 0.0f, 0.0f);
Matrix4x4f prevTransformation;

float mass;
float height;
float halfHeight;
float shapeRadius;
float minEyeHeight;
Expand Down Expand Up @@ -110,7 +113,7 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
prevTransformation = Matrix4x4f.identity;

rbody.createUpVectorConstraint(Vector3f(0.0f, 1.0f, 0.0f));
rbody.gravity = Vector3f(0.0f, -20.0f, 0.0f);
rbody.gravity = gravity;
NewtonBodySetAutoSleep(rbody.newtonBody, false);

rbody.contactCallback = &onContact;
Expand Down Expand Up @@ -286,6 +289,14 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
targetEyeHeight = maxEyeHeight;
}
}

void enableGravity(bool mode)
{
if (mode)
rbody.gravity = gravity;
else
rbody.gravity = Vector3f(0.0f, 0.0f, 0.0f);
}
}

NewtonCharacterComponent makeCharacter(Entity entity, NewtonPhysicsWorld world, float height, float radius, float mass)
Expand Down
19 changes: 16 additions & 3 deletions src/dagon/ui/firstpersonview.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ class FirstPersonViewComponent: EntityComponent
float pitch = 0.0f;
float turn = 0.0f;

Quaternionf orientationV = Quaternionf.identity;
Quaternionf orientationH = Quaternionf.identity;

Quaternionf baseOrientation = Quaternionf.identity;

Vector3f direction = Vector3f(0.0f, 0.0f, 1.0f);
Vector3f directionHorizontal = Vector3f(0.0f, 0.0f, 1.0f);
Vector3f right = Vector3f(1.0f, 0.0f, 0.0f);
Vector3f up = Vector3f(0.0f, 1.0f, 0.0f);

this(EventManager em, Entity e)
{
super(em, e);
Expand Down Expand Up @@ -147,10 +155,10 @@ class FirstPersonViewComponent: EntityComponent
prevMouseY = eventManager.mouseY;
}

auto rotPitch = rotationQuaternion(Vector3f(1.0f,0.0f,0.0f), degtorad(pitch));
auto rotTurn = rotationQuaternion(Vector3f(0.0f,1.0f,0.0f), degtorad(turn));
orientationV = rotationQuaternion(Vector3f(1.0f, 0.0f, 0.0f), degtorad(pitch));
orientationH = rotationQuaternion(Vector3f(0.0f, 1.0f, 0.0f), degtorad(turn));

Quaternionf orientation = baseOrientation * rotTurn * rotPitch;
Quaternionf orientation = baseOrientation * orientationH * orientationV;

entity.transformation =
(translationMatrix(entity.position) *
Expand All @@ -171,6 +179,11 @@ class FirstPersonViewComponent: EntityComponent
entity.invAbsoluteTransformation = entity.invTransformation;
entity.prevAbsoluteTransformation = entity.prevTransformation;
}

direction = orientation.rotate(Vector3f(0.0f, 0.0f, 1.0f));
directionHorizontal = orientationH.rotate(Vector3f(0.0f, 0.0f, 1.0f));
right = orientationH.rotate(Vector3f(1.0f, 0.0f, 0.0f));
up = orientationH.rotate(Vector3f(01.0f, 1.0f, 0.0f));
}

override void onFocusGain()
Expand Down

0 comments on commit 31a95bf

Please sign in to comment.