1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/physics/rigidbody.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.physics.rigidbody;
10 
11 import liberty.framework.terrain.impl;
12 import liberty.math.transform;
13 import liberty.math.vector;
14 import liberty.scene.entity;
15 import liberty.time;
16 
17 /**
18  *
19 **/
20 class RigidBody {
21   public {
22     Vector3F gravityDirection = Vector3F.down;
23     float gravity = -80.0f;
24     float jumpPower = 20.0f;
25     float upSpeed = 0;
26     float moveSpeed = 5.0f;
27     bool onGround;
28 
29     Entity entity;
30     Terrain terrain;
31   }
32 
33   /**
34    *
35   **/
36   this(Entity entity, Terrain terrain)   {
37     this.entity = entity;
38     this.terrain = terrain;
39   }
40 
41   /**
42    *
43   **/
44   typeof(this) processPx() {
45     const float deltaTime = Time.getDelta;
46     upSpeed += gravity * deltaTime;
47 
48     entity.component!Transform.setLocationY!"+="(upSpeed * deltaTime);
49     const Vector3F worldPos = entity.component!Transform.getLocation;
50 
51     const float terrainHeight = terrain.getHeight(worldPos.x, worldPos.z);
52 
53     onGround = false;
54     
55     if (worldPos.y < terrainHeight) {
56       onGround = true;
57       upSpeed = 0;
58       entity.component!Transform.setLocationY(terrainHeight);
59     }
60 
61     return this;
62   }
63 
64   /**
65    *
66   **/
67   typeof(this) jump() {
68     upSpeed = jumpPower;
69     return this;
70   }
71 }