stuart6854

Object.java/GameObject.java

Jun 1st, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package org.bearengine.objects;
  2.  
  3. import org.joml.*;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. /**
  9.  * Created by Stuart on 22/05/2016.
  10.  */
  11. public class Object {
  12.  
  13.     public static List<Object> Objects = new ArrayList<>();
  14.  
  15.     public Vector3d Position;
  16.     public Quaterniond Rotation;
  17.     public Vector3d Scale;
  18.  
  19.     private Matrix4d m_transformation;
  20.     private boolean transformIsDirty = true;
  21.  
  22.     public Object(){
  23.         this.Position = new Vector3d(0, 0, 0);
  24.         this.Rotation = new Quaterniond();
  25.         this.Scale = new Vector3d(1, 1, 1);
  26.  
  27.         this.m_transformation = new Matrix4d();
  28.  
  29.         Objects.add(this);
  30.     }
  31.  
  32.     public void SetPosition(float x, float y, float z){
  33.         Position.set(x, y, z);
  34.     }
  35.  
  36.     public void TranslatePosition(Vector3f translation){
  37.         this.TranslatePosition(translation.x, translation.y, translation.z);
  38.     }
  39.  
  40.     public void TranslatePosition(float x, float y, float z){
  41.         Position.add(x, y, z);
  42.     }
  43.  
  44.     public void SetRotation(float x, float y, float z){
  45.         Rotation.set(x, y, z);
  46.     }
  47.  
  48.     public void Rotate(Vector3f rotation){
  49.         Rotation.rotate(rotation.x, rotation.y, rotation.z);
  50.     }
  51.  
  52.     public void Rotate(float x, float y, float z){
  53.         Rotation.rotate(x, y, z);
  54.     }
  55.  
  56.     public void SetScale(float x, float y, float z){
  57.         Scale.set(x, y, z);
  58.     }
  59.  
  60.     public Matrix4d GetTransformMatrix(){
  61.         if(transformIsDirty){
  62.             UpdateTransformMatrix();
  63.         }
  64.         return m_transformation;
  65.     }
  66.  
  67.     private void UpdateTransformMatrix(){
  68.         m_transformation.identity().translate(Position).rotate(Rotation).scale(Scale);
  69.  
  70.         transformIsDirty = false;
  71.     }
  72.  
  73.     public void Destroy(){
  74.         Objects.remove(this);
  75.         Position = null;
  76.         Rotation = null;
  77.         Scale = null;
  78.         m_transformation = null;
  79.     }
  80.  
  81. }
  82.  
  83. package org.bearengine.objects;
  84.  
  85. import org.bearengine.graphics.rendering.Renderer;
  86. import org.bearengine.graphics.types.Mesh;
  87.  
  88. /**
  89.  * Created by Stuart on 28/05/2016.
  90.  */
  91. public class GameObject extends Object {
  92.  
  93.     public String Name = "GameObject";
  94.  
  95.     private Mesh mesh;
  96.  
  97.     public GameObject(){
  98.         super();
  99.     }
  100.  
  101.     private void RegisterRender(){
  102.         Renderer.RegisterGameObject(this);
  103.     }
  104.  
  105.     private void UnRegisterRender(){
  106.         Renderer.UnRegisterGameObject(this);
  107.     }
  108.  
  109.     public Mesh getMesh() {
  110.         return mesh;
  111.     }
  112.  
  113.     public void setMesh(Mesh mesh) {
  114.         UnRegisterRender();
  115.         this.mesh = mesh;
  116.         RegisterRender();
  117.     }
  118.  
  119.  
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment