Using the 3D graphics and associated Physics engine develop an arcade style game
ID: 664874 • Letter: U
Question
Using the 3D graphics and associated Physics engine develop an arcade style game that allows a user to fire at various types of targets and be awarded points when the bullet or missile collides with the target. The following functionality is required:
1. At least 4 types of targets should be displayed in the game. As targets are destroyed they should recreated at another location in the scene automatically.
2. Each of the targets should be moving in the scene.
3. At least one of the targets should have gravity/physics applied.
4. Bullets or missiles should be unlimited.
5. A Heads-up Display should be provided with a minimum of score totals for each target, total score, high score, and the number of bullets/missiles fired.
6. High score should be stored in an ASCII file. Optionally, scores can be kept in a database with ability to have initials and scores for top 5 stored and displayed.
7. Simple directions for running the game should be included on the screen
8. A game timer should be embedded limiting a game session to 5 minutes or less.
9. Coding conventions should be followed based on the programming language used.
Explanation / Answer
Target.Java
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;
import java.util.Random;
public final class Target {
private BulletAppState bulletAppState;
private Node rootNode;
private Node shootables;
private AssetManager assetManager;
public Geometry outlineBall1;
public Geometry outlineBall2;
public Geometry outlineBall3;
public Geometry outlineBall4;
public RigidBodyControl ballCollision1;
public RigidBodyControl ballCollision2;
public RigidBodyControl ballCollision3;
public RigidBodyControl ballCollision4;;
public Target (AssetManager assetManager, Node rootNode, Node shootables, BulletAppState bulletAppState) {
this.bulletAppState = bulletAppState;
this.rootNode = rootNode;
this.shootables = shootables;
this.assetManager = assetManager;
rootNode.attachChild(shootables);
// Creates balls
createBall1();
createBall2();
createBall3();
createBall4();
}
public void createBall1() {
Random random = new Random();
Sphere createBall = new Sphere(32, 32, 0.8f);
outlineBall1 = new Geometry("Ball 1", createBall);
createBall.setTextureMode(Sphere.TextureMode.Projected);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.randomColor());
outlineBall1.setMaterial(mat);
outlineBall1.setLocalTranslation((random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f));
shootables.attachChild(outlineBall1);
ballCollision1 = new RigidBodyControl(1f);
outlineBall1.addControl(ballCollision1);
bulletAppState.getPhysicsSpace().add(ballCollision1);
ballCollision1.setMass(1);
ballCollision1.setFriction(0);
ballCollision1.setRestitution(1.0f);
}
public void createBall2() {
Random random = new Random();
Sphere createBall = new Sphere(32, 32, 0.6f);
outlineBall2 = new Geometry("Ball 2", createBall);
createBall.setTextureMode(Sphere.TextureMode.Projected);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.randomColor());
outlineBall2.setMaterial(mat);
outlineBall2.setLocalTranslation((random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f));
shootables.attachChild(outlineBall2);
ballCollision2 = new RigidBodyControl(1f);
outlineBall2.addControl(ballCollision2);
bulletAppState.getPhysicsSpace().add(ballCollision2);
ballCollision2.setMass(1);
ballCollision2.setFriction(0);
ballCollision2.setRestitution(1.0f);
}
public void createBall3() {
Random random = new Random();
Sphere createBall = new Sphere(32, 32, 0.4f);
outlineBall3 = new Geometry("Ball 3", createBall);
createBall.setTextureMode(Sphere.TextureMode.Projected);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.randomColor());
outlineBall3.setMaterial(mat);
outlineBall3.setLocalTranslation((random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f));
shootables.attachChild(outlineBall3);
ballCollision3 = new RigidBodyControl(1f);
outlineBall3.addControl(ballCollision3);
bulletAppState.getPhysicsSpace().add(ballCollision3);
ballCollision3.setMass(1);
ballCollision3.setFriction(0);
ballCollision3.setRestitution(1.0f);
}
public void createBall4() {
Random random = new Random();
Sphere createBall = new Sphere(32, 32, 0.2f);
outlineBall4 = new Geometry("Ball 4", createBall);
createBall.setTextureMode(Sphere.TextureMode.Projected);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.randomColor());
outlineBall4.setMaterial(mat);
outlineBall4.setLocalTranslation((random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f), (random.nextFloat() * 2.99f));
shootables.attachChild(outlineBall4);
ballCollision4 = new RigidBodyControl(1f);
outlineBall4.addControl(ballCollision4);
bulletAppState.getPhysicsSpace().add(ballCollision4);
ballCollision4.setMass(1);
ballCollision4.setFriction(0);
ballCollision4.setRestitution(1.0f);
}
}
Cube.Java
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
/*
* This class creates cubic scene objects made of six side of walls.
* @author yujishimojo
*/
public class Cube {
private BulletAppState bulletAppState;
private Node rootNode;
private AssetManager assetManager;
public Cube (AssetManager assetManager, Node rootNode, BulletAppState bulletAppState) {
this.bulletAppState = bulletAppState;
this.rootNode = rootNode;
this.assetManager = assetManager;
// Create back side of cube
createBackCube();
// Create front side of cube
createFrontCube();
// Create right side of cube
createRightSideCube();
// Create left side of cube
createLeftSideCube();
// Create bottom side of cube
createBottomSideCube();
// Create top side of cube
createTopSideCube();
}
private void createBackCube() {
Mesh back = new Mesh();
// Back Wall
Vector3f[] backVertices = new Vector3f[4];
backVertices[0] = new Vector3f(0, 0, 0);
backVertices[1] = new Vector3f(9, 0, 0);
backVertices[2] = new Vector3f(0, 9, 0);
backVertices[3] = new Vector3f(9, 9, 0);
// Back Wall
Vector2f[] backCoord = new Vector2f[4];
backCoord[0] = new Vector2f(0, 0);
backCoord[1] = new Vector2f(1, 0);
backCoord[2] = new Vector2f(0, 1);
backCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
back.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(backVertices));
back.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(backCoord));
back.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
back.updateBound();
// Create back of cube
Geometry backGeom = new Geometry("OurMesh", back);
Material backMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
backMat.setColor("Color", ColorRGBA.White);
backGeom.setMaterial(backMat);
backMat.getAdditionalRenderState().setWireframe(true);
backMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
rootNode.attachChild(backGeom);
RigidBodyControl backWallCollision = new RigidBodyControl(0f);
backGeom.addControl(backWallCollision);
bulletAppState.getPhysicsSpace().add(backWallCollision);
backWallCollision.setRestitution(0.15f);
backWallCollision.setFriction(0);
}
private void createFrontCube() {
Mesh front = new Mesh();
// Front Wall
Vector3f[] frontVertices = new Vector3f[4];
frontVertices[0] = new Vector3f(0, 0, 9);
frontVertices[1] = new Vector3f(9, 0, 9);
frontVertices[2] = new Vector3f(0, 9, 9);
frontVertices[3] = new Vector3f(9, 9, 9);
// Front Wall
Vector2f[] frontCoord = new Vector2f[4];
frontCoord[0] = new Vector2f(0, 0);
frontCoord[1] = new Vector2f(1, 0);
frontCoord[2] = new Vector2f(0, 1);
frontCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
front.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(frontVertices));
front.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(frontCoord));
front.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
front.updateBound();
// Create front of cube
Geometry frontGeom = new Geometry("OurMesh", front);
Material frontMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
frontGeom.setMaterial(frontMat);
frontMat.setTransparent(true);
frontMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.FrontAndBack);
rootNode.attachChild(frontGeom);
RigidBodyControl frontWallCollision = new RigidBodyControl(0f);
frontGeom.addControl(frontWallCollision);
bulletAppState.getPhysicsSpace().add(frontWallCollision);
frontWallCollision.setRestitution(0.15f);
frontWallCollision.setFriction(0);
}
private void createRightSideCube() {
Mesh rightSide = new Mesh();
Quaternion quatA = new Quaternion();
quatA.fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y);
// Right Wall
Vector3f[] rightSideVertices = new Vector3f[4];
rightSideVertices[0] = new Vector3f(3, 0, -9);
rightSideVertices[1] = new Vector3f(12, 0, -9);
rightSideVertices[2] = new Vector3f(3, 9, -9);
rightSideVertices[3] = new Vector3f(12, 9, -9);
Vector3f vectorA = quatA.mult(rightSideVertices[0]);
Vector3f vectorB = quatA.mult(rightSideVertices[1]);
Vector3f vectorC = quatA.mult(rightSideVertices[2]);
Vector3f vectorD = quatA.mult(rightSideVertices[3]);
Vector3f[] rightSideYes = new Vector3f[4];
rightSideYes[0] = new Vector3f(vectorA);
rightSideYes[1] = new Vector3f(vectorB);
rightSideYes[2] = new Vector3f(vectorC);
rightSideYes[3] = new Vector3f(vectorD);
// Right Wall
Vector2f[] rightSideCoord = new Vector2f[4];
rightSideCoord[0] = new Vector2f(0, 0);
rightSideCoord[1] = new Vector2f(1, 0);
rightSideCoord[2] = new Vector2f(0, 1);
rightSideCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
rightSide.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(rightSideYes));
rightSide.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(rightSideCoord));
rightSide.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
rightSide.updateBound();
// Create Right of cube
Geometry rightSideGeom = new Geometry("OurMesh", rightSide);
rightSideGeom.setLocalTranslation(0, 0, -3);
Material rightSideMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rightSideMat.setColor("Color", ColorRGBA.White);
rightSideGeom.setMaterial(rightSideMat);
rightSideMat.getAdditionalRenderState().setWireframe(true);
rightSideMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
RigidBodyControl rightWallCollision = new RigidBodyControl(0f);
rightSideGeom.addControl(rightWallCollision);
bulletAppState.getPhysicsSpace().add(rightWallCollision);
rightWallCollision.setRestitution(0.15f);
rightWallCollision.setFriction(0);
rootNode.attachChild(rightSideGeom);
}
private void createLeftSideCube() {
Mesh leftSide = new Mesh();
Quaternion quatA = new Quaternion();
quatA.fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y);
// Left Wall
Vector3f[] leftSideVertices = new Vector3f[4];
leftSideVertices[0] = new Vector3f(6, 0, 0);
leftSideVertices[1] = new Vector3f(-3, 0, 0);
leftSideVertices[2] = new Vector3f(6, 9, 0);
leftSideVertices[3] = new Vector3f(-3, 9, 0);
Vector3f vectorA = quatA.mult(leftSideVertices[0]);
Vector3f vectorB = quatA.mult(leftSideVertices[1]);
Vector3f vectorC = quatA.mult(leftSideVertices[2]);
Vector3f vectorD = quatA.mult(leftSideVertices[3]);
Vector3f[] leftSideYes = new Vector3f[4];
leftSideYes[0] = new Vector3f(vectorA);
leftSideYes[1] = new Vector3f(vectorB);
leftSideYes[2] = new Vector3f(vectorC);
leftSideYes[3] = new Vector3f(vectorD);
// Left Wall
Vector2f[] leftSideCoord = new Vector2f[4];
leftSideCoord[0] = new Vector2f(0, 0);
leftSideCoord[1] = new Vector2f(1, 0);
leftSideCoord[2] = new Vector2f(0, 1);
leftSideCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
leftSide.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(leftSideYes));
leftSide.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(leftSideCoord));
leftSide.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
leftSide.updateBound();
// Create Left of cube
Geometry leftSideGeom = new Geometry("OurMesh", leftSide);
Material leftSideMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
leftSideMat.setColor("Color", ColorRGBA.White);
leftSideGeom.setMaterial(leftSideMat);
leftSideGeom.setLocalTranslation(0, 0, 3);
leftSideMat.getAdditionalRenderState().setWireframe(true);
leftSideMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
rootNode.attachChild(leftSideGeom);
RigidBodyControl leftWallCollision = new RigidBodyControl(0f);
leftSideGeom.addControl(leftWallCollision);
bulletAppState.getPhysicsSpace().add(leftWallCollision);
leftWallCollision.setRestitution(0.15f);
leftWallCollision.setFriction(0);
}
private void createBottomSideCube() {
Mesh bottomSide = new Mesh();
Quaternion quatA = new Quaternion();
quatA.fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X);
// Bottom Wall
Vector3f[] bottomSideVertices = new Vector3f[4];
bottomSideVertices[0] = new Vector3f(0, 0, 9);
bottomSideVertices[1] = new Vector3f(0, -9, 9);
bottomSideVertices[2] = new Vector3f(9, 0, 9);
bottomSideVertices[3] = new Vector3f(9, -9, 9);
Vector3f vectorA = quatA.mult(bottomSideVertices[0]);
Vector3f vectorB = quatA.mult(bottomSideVertices[1]);
Vector3f vectorC = quatA.mult(bottomSideVertices[2]);
Vector3f vectorD = quatA.mult(bottomSideVertices[3]);
Vector3f[] bottomSideYes = new Vector3f[4];
bottomSideYes[0] = new Vector3f(vectorA);
bottomSideYes[1] = new Vector3f(vectorB);
bottomSideYes[2] = new Vector3f(vectorC);
bottomSideYes[3] = new Vector3f(vectorD);
// Bottom Wall
Vector2f[] bottomSideCoord = new Vector2f[4];
bottomSideCoord[0] = new Vector2f(0, 0);
bottomSideCoord[1] = new Vector2f(1, 0);
bottomSideCoord[2] = new Vector2f(0, 1);
bottomSideCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
bottomSide.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(bottomSideYes));
bottomSide.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(bottomSideCoord));
bottomSide.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
bottomSide.updateBound();
// Create Bottom of cube
Geometry bottomSideGeom = new Geometry("OurMesh", bottomSide);
Material bottomSideMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
bottomSideMat.setColor("Color", ColorRGBA.White);
bottomSideGeom.setMaterial(bottomSideMat);
bottomSideMat.getAdditionalRenderState().setWireframe(true);
bottomSideMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
rootNode.attachChild(bottomSideGeom);
RigidBodyControl bottomWallCollision = new RigidBodyControl(0f);
bottomSideGeom.addControl(bottomWallCollision);
bulletAppState.getPhysicsSpace().add(bottomWallCollision);
bottomWallCollision.setRestitution(0.15f);
bottomWallCollision.setFriction(0);
}
private void createTopSideCube() {
Mesh topSide = new Mesh();
Quaternion quatA = new Quaternion();
quatA.fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X);
// Top Wall
Vector3f[] topSideVertices = new Vector3f[4];
topSideVertices[0] = new Vector3f(0, 0, 0);
topSideVertices[1] = new Vector3f(0, -9, 0);
topSideVertices[2] = new Vector3f(9, 0, 0);
topSideVertices[3] = new Vector3f(9, -9, 0);
Vector3f vectorA = quatA.mult(topSideVertices[0]);
Vector3f vectorB = quatA.mult(topSideVertices[1]);
Vector3f vectorC = quatA.mult(topSideVertices[2]);
Vector3f vectorD = quatA.mult(topSideVertices[3]);
Vector3f[] topSideYes = new Vector3f[4];
topSideYes[0] = new Vector3f(vectorA);
topSideYes[1] = new Vector3f(vectorB);
topSideYes[2] = new Vector3f(vectorC);
topSideYes[3] = new Vector3f(vectorD);
// Top Wall
Vector2f[] topSideCoord = new Vector2f[4];
topSideCoord[0] = new Vector2f(0, 0);
topSideCoord[1] = new Vector2f(1, 0);
topSideCoord[2] = new Vector2f(0, 1);
topSideCoord[3] = new Vector2f(1, 1);
// Indexes. We define the order in which mesh should be constructed
int[] indexes = {2, 0, 1, 1, 3, 2};
// Setting buffers
topSide.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(topSideYes));
topSide.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(topSideCoord));
topSide.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));
topSide.updateBound();
// Create Top of cube
Geometry topSideGeom = new Geometry("OurMesh", topSide);
Material topSideMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
topSideMat.setColor("Color", ColorRGBA.White);
topSideGeom.setMaterial(topSideMat);
topSideMat.getAdditionalRenderState().setWireframe(true);
topSideMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
RigidBodyControl topWallCollision = new RigidBodyControl(0f);
topSideGeom.addControl(topWallCollision);
bulletAppState.getPhysicsSpace().add(topWallCollision);
topWallCollision.setFriction(0);
topWallCollision.setRestitution(0.15f);
rootNode.attachChild(topSideGeom);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.