Skip to content

Commit

Permalink
Rename NewtonCharacterComponent to NewtonCharacterController
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Jan 15, 2025
1 parent 31a95bf commit ee7a264
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
40 changes: 24 additions & 16 deletions extensions/newton/src/dagon/ext/newton/character.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ import dagon.ext.newton.world;
import dagon.ext.newton.shape;
import dagon.ext.newton.rigidbody;

class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
/*
* A simple rigidbody-based character controller.
* Collision model consists of two spheres, one on top of another.
* Supports jumping and crouching.
*/
class NewtonCharacterController: EntityComponent, NewtonRaycaster
{
NewtonPhysicsWorld world;
NewtonSphereShape lowerShape;
Expand All @@ -60,8 +65,8 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
float height;
float halfHeight;
float shapeRadius;
float minEyeHeight;
float maxEyeHeight;
float crouchEyeHeight;
float normalEyeHeight;
float eyeHeight;
float targetEyeHeight;
float crouchSpeed;
Expand All @@ -77,6 +82,7 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
Vector3f groundContactPosition = Vector3f(0.0f, 0.0f, 0.0f);
Vector3f groundContactNormal = Vector3f(0.0f, 0.0f, 0.0f);
Vector3f ceilingPosition = Vector3f(0.0f, 0.0f, 0.0f);

float maxRaycastDistance = 100.0f;
protected float closestHitParam = 1.0f;

Expand All @@ -88,9 +94,9 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
this.mass = mass;
this.halfHeight = height * 0.5f;
shapeRadius = radius;
minEyeHeight = height * 0.5f;
maxEyeHeight = height;
eyeHeight = maxEyeHeight;
crouchEyeHeight = height * 0.5f;
normalEyeHeight = height;
eyeHeight = normalEyeHeight;
targetEyeHeight = eyeHeight;
crouchSpeed = 6.0f;
lowerShape = New!NewtonSphereShape(shapeRadius, world);
Expand Down Expand Up @@ -230,18 +236,18 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
if (!isCrouching && !isForcefullyCrouching)
{
isForcefullyCrouching = true;
NewtonBodySetCollision(rbody.newtonBody, lowerShape.newtonCollision);
rbody.setCollisionShape(lowerShape);
}

targetEyeHeight = minEyeHeight;
targetEyeHeight = crouchEyeHeight;
}
else
{
if (!isCrouching && isForcefullyCrouching)
{
isForcefullyCrouching = false;
NewtonBodySetCollision(rbody.newtonBody, shape.newtonCollision);
targetEyeHeight = maxEyeHeight;
rbody.setCollisionShape(shape);
targetEyeHeight = normalEyeHeight;
}
}

Expand Down Expand Up @@ -280,13 +286,13 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
isCrouching = mode;
if (isCrouching)
{
NewtonBodySetCollision(rbody.newtonBody, lowerShape.newtonCollision);
targetEyeHeight = minEyeHeight;
rbody.setCollisionShape(lowerShape);
targetEyeHeight = crouchEyeHeight;
}
else
{
NewtonBodySetCollision(rbody.newtonBody, shape.newtonCollision);
targetEyeHeight = maxEyeHeight;
rbody.setCollisionShape(shape);
targetEyeHeight = normalEyeHeight;
}
}

Expand All @@ -299,7 +305,9 @@ class NewtonCharacterComponent: EntityComponent, NewtonRaycaster
}
}

NewtonCharacterComponent makeCharacter(Entity entity, NewtonPhysicsWorld world, float height, float radius, float mass)
alias NewtonCharacterComponent = NewtonCharacterController;

NewtonCharacterController makeCharacter(Entity entity, NewtonPhysicsWorld world, float height, float radius, float mass)
{
return New!NewtonCharacterComponent(world, entity, height, radius, mass);
return New!NewtonCharacterController(world, entity, height, radius, mass);
}
6 changes: 6 additions & 0 deletions extensions/newton/src/dagon/ext/newton/rigidbody.d
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class NewtonRigidBody: Owner
void defaultContactCallback(NewtonRigidBody, NewtonRigidBody, const void*)
{
}

void setCollisionShape(NewtonCollisionShape shape)
{
if (shape.newtonCollision)
NewtonBodySetCollision(newtonBody, shape.newtonCollision);
}

void update(double dt)
{
Expand Down

0 comments on commit ee7a264

Please sign in to comment.