Skip to content

Commit

Permalink
Added MeshToMesh collision detection
Browse files Browse the repository at this point in the history
* Added path to models in resources as constants to Mesh class
* Added SphereToMesh collision detection
* Added MeshToMesh collision detection
* fixed bugs
  • Loading branch information
John committed Nov 21, 2018
1 parent c9a9fef commit eaed4f8
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 181 deletions.
8 changes: 5 additions & 3 deletions ProjectHistory
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ v0.1 - 24/08/2018
* Renamed SceneFrame to SceneWindow
* Moved JPanel from each Camera to 1 JPanel in the SceneWindow (less ram usage, fixed flickering bug when more than 1 Camera is active)
* Moved projection type, rendering type... from camera to shader class in form of constants
27/10/2018
v0.2 - 27/10/2018
* Fixed some bugs
30/10/2018
* Fixed multiple cameras overlapping bug
Expand Down Expand Up @@ -255,8 +255,10 @@ v0.1 - 24/08/2018
* Fixed som exporter bug
* Fixed som files from resources folder
* Added new som file to resources


* Added path to models in resources as constants to Mesh class
* Added SphereToMesh collision detection
* Added MeshToMesh collision detection
* fixed bugs



69 changes: 52 additions & 17 deletions blend2SOM/export_som.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def write(filepath,
bpy.context.scene.frame_set(i)
bpy.context.scene.update()
for bone in bones:
animData.addBone(bone)
animData.addB_Position(bone.head)
bone.rotation_mode = 'XYZ'
animData.addB_Rotation(bone.rotation_euler)
animData.addB_Scale(bone.scale)
animsData.append(animData)
if me is not None:
bm = bmesh.new()
Expand Down Expand Up @@ -210,16 +213,36 @@ def writeToFile(filepath, meshData, animsData):
for animData in animsData:
file.write(" Animation < " + "\n")
file.write(" Name < " + animData.name + "> Name \n")
file.write(" Bones < ")
i = 0
for value in animData.bones:
file.write(" bPosition < ")
for value in animData.bPosition:
i += 1
if (i < len(animData.bones)):
if (i < len(animData.bPosition)):
file.write("%i," % value)
else:
file.write(("%i" % value))

file.write(" > Bones" + "\n")
file.write(" > bPosition" + "\n")
i = 0
file.write(" bRotation < ")
for value in animData.bRotation:
i += 1
if (i < len(animData.bRotation)):
file.write("%i," % value)
else:
file.write(("%i" % value))

file.write(" > bRotation" + "\n")
i = 0
file.write(" bScale < ")
for value in animData.bScale:
i += 1
if (i < len(animData.bScale)):
file.write("%i," % value)
else:
file.write(("%i" % value))

file.write(" > bScale" + "\n")
file.write(" > Animation " + "\n")
file.write("> Animations" + "\n")

Expand Down Expand Up @@ -314,15 +337,27 @@ def addM_Color(self, value):
class AnimData:
def __init__(self, name):
self.name = name
self.bones = []
def addBone(self, bone):
self.bones.append(bone.head[0]*100)
self.bones.append(bone.head[1]*100)
self.bones.append(bone.head[2]*100)
bone.rotation_mode = 'XYZ'
self.bones.append(math.degrees(bone.rotation_euler[0]))
self.bones.append(math.degrees(bone.rotation_euler[1]))
self.bones.append(math.degrees(bone.rotation_euler[2]))
self.bones.append(bone.scale[0])
self.bones.append(bone.scale[1])
self.bones.append(bone.scale[2])
self.bPosition = []
self.bRotation = []
self.bScale = []

def addB_Position(self, value):
self.bPosition.append(value[0]*100)
self.bPosition.append(value[1]*100)
self.bPosition.append(value[2]*100)

def addB_Rotation(self, value):
self.bRotation.append(math.degrees(value[0]))
self.bRotation.append(math.degrees(value[1]))
self.bRotation.append(math.degrees(value[2]))

def addB_Scale(self, value):
self.bScale.append(value[0])
self.bScale.append(value[1])
self.bScale.append(value[2])






12 changes: 6 additions & 6 deletions src/main/java/com/johnsproject/jpge/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static Engine getInstance() {
private Thread jpgeThread;
private Thread physicsThread;

private int graphicsUpdateRate = 50;
private int graphicsUpdateRate = 30;
private int jpgeUpdateRate = 30;
private int physicsUpdateRate = 50;
private int physicsUpdateRate = 30;

private long startTime = System.currentTimeMillis();
private boolean playing = true;
Expand Down Expand Up @@ -197,7 +197,7 @@ public void setSceneWindow(SceneWindow sceneWindow) {

/**
* Returns how often graphics should be updated in a second.
* Default is 50.
* Default is 30.
*
* @return how often graphics should be updated in a second.
*/
Expand All @@ -207,7 +207,7 @@ public int getGraphicsUpdateRate() {

/**
* Sets how often graphics should be updated in a second.
* Default is 50.
* Default is 30.
*
* @param graphicsUpdateRate value to set.
*/
Expand Down Expand Up @@ -237,7 +237,7 @@ public void setJPGEUpdateRate(int jpgeUpdateRate) {

/**
* Returns how often physics should be updated in a second.
* Default is 50.
* Default is 30.
*
* @return how often physics should be updated in a second.
*/
Expand All @@ -247,7 +247,7 @@ public int getPhysicsUpdateRate() {

/**
* Sets how often physics should be updated in a second.
* Default is 50.
* Default is 30.
*
* @param physicsUpdateRate value to set.
*/
Expand Down
64 changes: 30 additions & 34 deletions src/main/java/com/johnsproject/jpge/dto/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,30 @@
public class Mesh {
private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z;

/** Values containing location of data in a vertex. This is used by the SOM importer when parsing values. */
public static final byte V_NORMAL = 3, V_BONE_INDEX = 6, V_MATERIAL_INDEX = 7;
/** Default vertex length. This is used by the SOM importer when parsing values. */
public static final byte VERTEX_LENGTH = 8;
/**
* Path to default models under the resources folder.
* Load them with:
* <br>
* <br>
* <code>
* SOMImporter.load(getClass().getResourceAsStream(Mesh.RESOURCES_));
* </code>
*/
public static final String RESOURCES_CUBE = "/cube.som",
RESOURCES_CONE = "/cone.som",
RESOURCES_CYLINDER = "/cylinder.som",
RESOURCES_SPHERE = "/sphere.som",
RESOURCES_PLANE = "/plane.som",
RESOURCES_MONKEY = "/monkey.som",
RESOURCES_TORUS = "/torus.som",
RESOURCES_ALL = "/meshes.som";

private Vertex[] vertexes;
private Vertex[] vertexesBuffer;
/** Values containing location of data in a face. This is used by the SOM importer when parsing values. */
public static final byte F_VERTEX_1 = 0, F_VERTEX_2 = 1, F_VERTEX_3 = 2,
F_UV_1 = 4, F_UV_2 = 6, F_UV_3 = 8,
F_MATERIAL_INDEX = 3, F_CULLED = 10;
/** Default face length. This is used by the SOM importer when parsing values. */
public static final byte FACE_LENGTH = 11;
private Face[] faces;
/** Default material length. This is used by the SOM importer when parsing values. */
public static final byte MATERIAL_LENGTH = 4;
private Material[] materials;
/** Values containing location of data in a bone. This is used by the SOM importer when parsing values. */
public static final byte POSITION = 0, ROTATION = 3, SCALE = 6;
/** Default bone length. This is used by the SOM importer when parsing values. */
public static final byte BONE_LENGTH = 9;
private Animation[] animations;
private Animation currentAnimation;
private int currentAnimation;

/**
* Creates a new instance of the Mesh class filled with the given values.
Expand All @@ -53,7 +55,7 @@ public Mesh (Vertex[] vertexes, Face[] faces, Material[] materials, Animation[]
this.faces = faces;
this.materials = materials;
this.animations = animations;
this.currentAnimation = animations[0];
this.currentAnimation = 0;
}

/**
Expand Down Expand Up @@ -169,7 +171,10 @@ public Material getMaterial(int index) {
* @param name name of animation.
*/
public void playAnimation(String name) {
currentAnimation = getAnimation(name);
for (int i = 0; i < animations.length; i++) {
if (animations[i].getName().equals(name))
currentAnimation = i;
}
}

/**
Expand All @@ -178,16 +183,7 @@ public void playAnimation(String name) {
* @param id id of animation.
*/
public void playAnimation(int id) {
currentAnimation = animations[id];
}

/**
* Sets the given animation as current animation of this mesh.
*
* @param animation animation to play.
*/
public void playAnimation(Animation animation) {
currentAnimation = animation;
currentAnimation = id;
}

/**
Expand All @@ -196,7 +192,7 @@ public void playAnimation(Animation animation) {
* @return current animation of this mesh.
*/
public Animation getCurrentAnimation() {
return currentAnimation;
return animations[currentAnimation];
}

/**
Expand All @@ -216,11 +212,11 @@ public Animation getAnimation(int index) {
* @return animation at the given index.
*/
public Animation getAnimation(String name) {
Animation anim = null;
for (Animation animation : animations) {
if (animation.getName() == name) anim = animation;
for (int i = 0; i < animations.length; i++) {
if (animations[i].getName().equals(name))
return animations[i];
}
return anim;
return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/johnsproject/jpge/dto/Rigidbody.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Rigidbody {
private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z;

public static final int COLLISION_SPHERE = 0;
public static final int COLLISION_TERRAIN = 2;
public static final int COLLISION_MESH = 2;

private int mass = 10;
private int[] velocity = new int[3];
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/johnsproject/jpge/dto/SceneObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public Shader getShader() {
* @param shader {@link Shader} to set.
*/
public void setShader(Shader shader) {
changed = true;
this.shader = shader;
}

Expand All @@ -115,6 +116,7 @@ public void setShader(Shader shader) {
* @return {@link Rigidbody} used by this scene object.
*/
public Rigidbody getRigidbody() {
changed = true;
return rigidbody;
}

Expand All @@ -124,6 +126,7 @@ public Rigidbody getRigidbody() {
* @return if this object is active or not.
*/
public boolean isActive() {
changed = true;
return active;
}

Expand All @@ -134,6 +137,7 @@ public boolean isActive() {
* @param active
*/
public void setActive(boolean active) {
changed = true;
this.active = active;
}
}
Loading

0 comments on commit eaed4f8

Please sign in to comment.