1 /*
2 Copyright (c) 2019-2020 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.ext.newton.character;
29 
30 import std.math;
31 import dlib.core.ownership;
32 import dlib.core.memory;
33 import dlib.math.vector;
34 import dlib.math.matrix;
35 import dlib.math.transformation;
36 import dlib.math.quaternion;
37 import dagon.core.event;
38 import dagon.core.time;
39 import dagon.graphics.entity;
40 
41 import bindbc.newton;
42 import dagon.ext.newton.world;
43 import dagon.ext.newton.shape;
44 import dagon.ext.newton.rigidbody;
45 
46 class NewtonCharacterComponent: EntityComponent
47 {
48     NewtonSphereShape lowerShape;
49     NewtonSphereShape upperShape;
50     NewtonCompoundShape shape;
51     NewtonRigidBody rbody;
52     NewtonRigidBody sensorBody;
53     float height;
54     float mass;
55     bool onGround = false;
56     Vector3f targetVelocity = Vector3f(0.0f, 0.0f, 0.0f);
57     Matrix4x4f prevTransformation;
58     float radius;
59     float shapeRadius;
60     float eyeHeight;
61     
62     this(NewtonPhysicsWorld world, Entity e, float height, float mass)
63     {
64         super(world.eventManager, e);
65         this.height = height;
66         this.mass = mass;
67         radius = height * 0.5f;
68         shapeRadius = radius * 0.5f;
69         eyeHeight = height * 0.5f;
70         lowerShape = New!NewtonSphereShape(shapeRadius, world);
71         lowerShape.setTransformation(translationMatrix(Vector3f(0.0f, -shapeRadius, 0.0f)));
72         upperShape = New!NewtonSphereShape(shapeRadius, world);
73         upperShape.setTransformation(translationMatrix(Vector3f(0.0f, shapeRadius, 0.0f)));
74         NewtonCollisionShape[2] shapes = [lowerShape, upperShape];
75         shape = New!NewtonCompoundShape(shapes, world);
76         
77         rbody = world.createDynamicBody(shape, mass);
78         rbody.groupId = world.kinematicGroupId;
79         rbody.raycastable = false;
80         rbody.enableRotation = false;
81         
82         Quaternionf rot = e.rotation;
83         rbody.transformation =
84             translationMatrix(e.position) *
85             rot.toMatrix4x4;
86         NewtonBodySetMatrix(rbody.newtonBody, rbody.transformation.arrayof.ptr);
87         prevTransformation = Matrix4x4f.identity;
88         
89         rbody.createUpVectorConstraint(Vector3f(0.0f, 1.0f, 0.0f));
90         rbody.gravity = Vector3f(0.0f, -20.0f, 0.0f);
91         
92         Vector3f sensorSize = Vector3f(radius, radius * 0.5f, radius);
93         auto sensorShape = New!NewtonBoxShape(sensorSize, world);
94         sensorBody = world.createDynamicBody(sensorShape, 1.0f);
95         sensorBody.groupId = world.sensorGroupId;
96         sensorBody.sensor = true;
97         sensorBody.collisionCallback = &onSensorCollision;
98     }
99     
100     void onSensorCollision(NewtonRigidBody sensorBody, NewtonRigidBody otherBody)
101     {
102         onGround = true;
103     }
104     
105     void updateVelocity()
106     {
107         Vector3f velocityChange = targetVelocity - rbody.velocity;
108         velocityChange.y = 0.0f;
109         rbody.velocity = rbody.velocity + velocityChange;
110 
111         onGround = false;
112         auto m = rbody.transformation * translationMatrix(Vector3f(0.0f, -radius, 0.0f));
113         NewtonBodySetMatrix(sensorBody.newtonBody, m.arrayof.ptr);
114 
115         targetVelocity = Vector3f(0.0f, 0.0f, 0.0f);
116     }
117     
118     override void update(Time t)
119     {
120         rbody.update(t.delta);
121 
122         entity.prevTransformation = prevTransformation;
123 
124         entity.position = rbody.position.xyz;
125         entity.transformation = rbody.transformation * scaleMatrix(entity.scaling);
126         entity.invTransformation = entity.transformation.inverse;
127         entity.rotation = rbody.rotation;
128 
129         entity.absoluteTransformation = entity.transformation;
130         entity.invAbsoluteTransformation = entity.invTransformation;
131         entity.prevAbsoluteTransformation = entity.prevTransformation;
132 
133         prevTransformation = entity.transformation;
134     }
135     
136     void move(Vector3f direction, float speed)
137     {
138         targetVelocity += direction * speed;
139     }
140     
141     void jump(float height)
142     {
143         if (onGround)
144         {
145             float jumpSpeed = sqrt(2.0f * height * -rbody.gravity.y);
146             Vector3f v = rbody.velocity;
147             v.y = jumpSpeed;
148             rbody.velocity = v;
149             onGround = false;
150         }
151     }
152     
153     void duck()
154     {
155         // TODO
156         //eyeHeight = 0.0f;
157     }
158     
159     void unduck()
160     {
161         // TODO
162         //eyeHeight = height * 0.5f;
163     }
164     
165     Vector3f position()
166     {
167         return rbody.position.xyz;
168     }
169     
170     Vector3f eyePoint()
171     {
172         return rbody.position.xyz + Vector3f(0.0f, eyeHeight, 0.0f);
173     }
174 }
175 
176 NewtonCharacterComponent makeCharacter(Entity entity, NewtonPhysicsWorld world, float height, float mass)
177 {
178     return New!NewtonCharacterComponent(world, entity, height, mass);
179 }