1 /*
2 Copyright (c) 2021 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.constraint;
29 
30 import dlib.core.ownership;
31 import dlib.core.memory;
32 import dlib.math.vector;
33 import dlib.math.matrix;
34 import bindbc.newton;
35 import dagon.ext.newton.world;
36 import dagon.ext.newton.rigidbody;
37 
38 abstract class NewtonConstraint: Owner
39 {
40     NewtonPhysicsWorld world;
41     NewtonJoint* joint;
42     
43     this(NewtonPhysicsWorld world)
44     {
45         super(world);
46         this.world = world;
47     }
48     
49     float stiffness()
50     {
51         return NewtonJointGetStiffness(joint);
52     }
53     void stiffness(float s)
54     {
55         NewtonJointSetStiffness(joint, s);
56     }
57 }
58 
59 class NewtonBallConstraint: NewtonConstraint
60 {
61     NewtonRigidBody body1;
62     NewtonRigidBody body2;
63     Vector3f pivotPoint;
64     
65     this(NewtonPhysicsWorld world, NewtonRigidBody body1, NewtonRigidBody body2, Vector3f pivotPoint)
66     {
67         super(world);
68         this.body1 = body1;
69         this.body2 = body2;
70         this.pivotPoint = pivotPoint;
71         this.joint = NewtonConstraintCreateBall(world.newtonWorld, pivotPoint.arrayof.ptr, body1.newtonBody, body2.newtonBody);
72         NewtonJointSetCollisionState(this.joint, 1);
73     }
74     
75     void setConeLimits(Vector3f axis, float maxConeAngle, float maxTwistAngle)
76     {
77         NewtonBallSetConeLimits(joint, axis.arrayof.ptr, maxConeAngle, maxTwistAngle);
78     }
79 }
80 
81 class NewtonSliderConstraint: NewtonConstraint
82 {
83     NewtonRigidBody body1;
84     NewtonRigidBody body2;
85     Vector3f pivotPoint;
86     Vector3f pivotAxis;
87     
88     this(NewtonPhysicsWorld world, NewtonRigidBody body1, NewtonRigidBody body2, Vector3f pivotPoint, Vector3f pivotAxis)
89     {
90         super(world);
91         this.body1 = body1;
92         this.body2 = body2;
93         this.pivotPoint = pivotPoint;
94         this.pivotAxis = pivotAxis;
95         this.joint = NewtonConstraintCreateSlider(world.newtonWorld, pivotPoint.arrayof.ptr, pivotAxis.arrayof.ptr, body1.newtonBody, body2.newtonBody);
96         NewtonJointSetCollisionState(this.joint, 1);
97     }
98 }
99 
100 class NewtonCorkscrewConstraint: NewtonConstraint
101 {
102     NewtonRigidBody body1;
103     NewtonRigidBody body2;
104     Vector3f pivotPoint;
105     Vector3f pivotAxis;
106     
107     this(NewtonPhysicsWorld world, NewtonRigidBody body1, NewtonRigidBody body2, Vector3f pivotPoint, Vector3f pivotAxis)
108     {
109         super(world);
110         this.body1 = body1;
111         this.body2 = body2;
112         this.pivotPoint = pivotPoint;
113         this.pivotAxis = pivotAxis;
114         this.joint = NewtonConstraintCreateCorkscrew(world.newtonWorld, pivotPoint.arrayof.ptr, pivotAxis.arrayof.ptr, body1.newtonBody, body2.newtonBody);
115         NewtonJointSetCollisionState(this.joint, 1);
116     }
117 }
118 
119 class NewtonUniversalConstraint: NewtonConstraint
120 {
121     NewtonRigidBody body1;
122     NewtonRigidBody body2;
123     Vector3f pivotPoint;
124     Vector3f pivotAxis1;
125     Vector3f pivotAxis2;
126     
127     this(NewtonPhysicsWorld world, NewtonRigidBody body1, NewtonRigidBody body2, Vector3f pivotPoint, Vector3f pivotAxis1, Vector3f pivotAxis2)
128     {
129         super(world);
130         this.body1 = body1;
131         this.body2 = body2;
132         this.pivotPoint = pivotPoint;
133         this.pivotAxis1 = pivotAxis1;
134         this.pivotAxis2 = pivotAxis2;
135         this.joint = NewtonConstraintCreateUniversal(world.newtonWorld, pivotPoint.arrayof.ptr, pivotAxis1.arrayof.ptr, pivotAxis2.arrayof.ptr, body1.newtonBody, body2.newtonBody);
136         NewtonJointSetCollisionState(this.joint, 1);
137     }
138 }
139 
140 class NewtonUpVectorConstraint: NewtonConstraint
141 {
142     NewtonRigidBody body1;
143     Vector3f pivotAxis;
144     
145     this(NewtonPhysicsWorld world, NewtonRigidBody body1, Vector3f pivotAxis)
146     {
147         super(world);
148         this.body1 = body1;
149         this.pivotAxis = pivotAxis;
150         this.joint = NewtonConstraintCreateUpVector(world.newtonWorld, pivotAxis.arrayof.ptr, body1.newtonBody);
151     }
152 }