Hey Guys,
I am getting a concurrentModificationException in my code. I have narrowed it down to when I add in portal Controls. I am getting the error from the phsyics space. I have tried everything to deal with it but everything seems not to work. I don’t know what to do now. I am attaching all the code as needed. Help Please guys, I need this as part of my Java Final project at school.
GravityWell.java
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Image;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.util.SkyFactory;
import com.jme3.util.TangentBinormalGenerator;
import java.util.ConcurrentModificationException;
import java.util.Random;
import mygame.Game;
/**
*
-
@author Gogol
*/
public class GravityWell
{
Random rand;
byte X;
byte Y;
byte Z;
byte WellType = -1;
public SolarSystem Parent;
public Node Well;
int radius = 150;
Game App;
long LocalSeed;
public BulletAppState bullet;
private Portal[] Portals = new Portal[6];
AssetManager assetManager;
FeatureGenerator genny;
public GravityWell(byte x, byte y, byte z, SolarSystem Parent)
{
this.X = x;
this.Y = y;
this.Z = z;
this.Parent = Parent;
this.App = Parent.Parent.Parent.app;
this.bullet = App.bulletAppState;
this.assetManager = Parent.Parent.Parent.app.getAssetManager();
this.genny = new FeatureGenerator(this);
LocalSeed = Parent.SystemSeed + this.X + this.Y + this.Z;
rand = new Random(LocalSeed);
Well = new Node("" + LocalSeed);
//this is the test!
this.bullet.initialize(App.getStateManager(), App);
generateBackGround();
//ERROR GENERATES around the Portal Construction.
try
{
//generatePortals();
}
catch(Exception e)
{
System.out.println("Error with portal Generation");
e.printStackTrace();
}
if(this.X == 0 && this.Y == 0 && this.Z == 0)
{
this.generateStar();
//Comment this one in to test surrounding wells so i can do planet stuff.
try
{
generatePortals();
}
catch(Exception e)
{
System.out.println("Error with portal Generation");
e.printStackTrace();
}
}
else
{
this.generateFeatures();
this.calcSunLighting(X,Y,Z);
try
{
generatePortals();
}
catch(Exception e)
{
System.out.println("Error with portal Generation");
e.printStackTrace();
}
}
}
public synchronized void generateFeatures()
{
byte temp = WellType <= 0 ? (byte) rand.nextInt(3): WellType;
switch(temp)
{
case 0:
{
System.out.println("This is an Empty Gravity Well");
break;
}
case 1:
{
this.attachChild(genny.getFeature(FeatureGenerator.PLANETARY_FEATURE));
break;
}
case 2:
{
System.out.println("This is a Gravity Well with an Anomoly in it!");
break;
}
}
}
public synchronized void generateStar()
{
System.out.println("This is a Gravity Well with a Star in it!");
}
public void generateBackGround()
{
AssetManager assetManager = App.getAssetManager();
Spatial sky;
Texture up = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_top3.jpg");
Texture down = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_bottom4.jpg");
Texture north = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_front5.jpg");
Texture south = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_back6.jpg");
Texture east = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_left2.jpg");
Texture west = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_right1.jpg");
sky = SkyFactory.createSky(assetManager, east, west, north, south, up, down);
sky.setLocalTranslation(0, 0, 0);
Well.attachChild(sky);
}
public void calcSunLighting(byte x, byte y, byte z)
{
DirectionalLight Sun = new DirectionalLight();
Sun.setDirection(new Vector3f(x,y,z));
Sun.setColor(ColorRGBA.Pink);
Well.addLight(Sun);
}
public boolean hasNeighbor(byte x, byte y, byte z)
{
if(Parent.checkCoords(x, y, z))
{
return true;
}
return false;
}
public synchronized void generatePortals() throws Exception
{
System.out.println("Creating Portals!");
System.out.println("
");
if(hasNeighbor( (byte) (this.X + 1), Y, Z))
{
Portal East = new Portal((byte) (this.X + 1), Y, Z, this);
East.Mesh.setLocalTranslation(radius, 0, 0);
Well.attachChild(East.Mesh);
Portals[0] = East;
}
if(hasNeighbor( (byte) (this.X - 1), Y, Z))
{
Portal West = new Portal((byte) (this.X - 1), Y, Z, this);
West.Mesh.setLocalTranslation(-radius, 0, 0);
Well.attachChild(West.Mesh);
Portals[1] = West;
}
if(hasNeighbor( X, (byte) (this.Y + 1), Z))
{
Portal Top = new Portal(X, (byte) (this.Y + 1), Z, this);
Top.Mesh.setLocalTranslation(0, radius, 0);
Well.attachChild(Top.Mesh);
Portals[2] = Top;
}
if(hasNeighbor( X, (byte) (this.Y - 1), Z))
{
Portal Bottom = new Portal(X, (byte) (this.Y - 1), Z, this);
Bottom.Mesh.setLocalTranslation(0, -radius, 0);
Well.attachChild(Bottom.Mesh);
Portals[3] = Bottom;
}
if(hasNeighbor( X, Y, (byte) (this.Z + 1)))
{
Portal North = new Portal( X, Y, (byte) (this.Z + 1), this);
North.Mesh.setLocalTranslation(0, 0, radius);
Well.attachChild(North.Mesh);
Portals[4] = North;
}
if(hasNeighbor( X, Y, (byte) (this.Z - 1)))
{
Portal South = new Portal( X, Y, (byte) (this.Z - 1), this);
South.Mesh.setLocalTranslation(0, 0, -radius);
Well.attachChild(South.Mesh);
Portals[5] = South;
}
}
public byte[] getCords()
{
byte[] Cords = new byte[3];
Cords[0] = X;
Cords[1] = Y;
Cords[2] = Z;
return Cords;
}
public synchronized void doWellCleanUp()
{
for(int i = 0; i < Portals.length; i++)
{
if(Portals != null)
{
Portals.destory();
}
}
bullet.getPhysicsSpace().removeAll(Well);
Well.removeFromParent();
}
public void attachChild(SpaceObject Object)
{
Object.Parent = this;
Well.attachChild(Object.Mesh);
}
}
[/java]
Portal.java
[java]
byte x, y, z;
String IDENT;
public GravityWell Parent;
Node Mesh;
public Portal(byte x, byte y, byte z, GravityWell Parent) throws Exception
{
this.x = x;
this.y = y;
this.z = z;
this.Parent = Parent;
IDENT = "Portal " + Parent.LocalSeed + " -- "+ "[" +Parent.X + "," + Parent.Y + "," + Parent.Z + "]" + "->[" + x + "," + y + "," + z + "]";
Mesh = new Node(IDENT);
Box b = new Box( 1, 1, 1); // create cube shape at the origin
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(Parent.App.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Green); // set color of material to blue
geom.setMaterial(mat);
Mesh.attachChild(geom);
try
{
PortalControl pControl = new PortalControl(this);
Mesh.addControl(pControl);
}
catch (Exception e)
{
System.out.println("Error with Portal Control");
throw e;
}
}
public String getID()
{
return IDENT;
}
public byte[] getJumpCords()
{
byte[] Cords = new byte[3];
Cords[0] = x;
Cords[1] = y;
Cords[2] = z;
return Cords;
}
public synchronized void destory()
{
Parent.bullet.getPhysicsSpace().removeCollisionListener(Mesh.getControl(PortalControl.class));
Parent.bullet.getPhysicsSpace().removeAll(Mesh);
Mesh.removeFromParent();
System.out.println(IDENT + " was Destroied!");
}
[/java]
PortalControl.java
[java]
import actor.Ship;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.math.Vector3f;
import java.lang.Exception;
import space.Portal;
/**
*
* @author Gogol
*/
public class PortalControl extends GhostControl implements PhysicsCollisionListener
{
BulletAppState bullet;
Portal Parent;
public PortalControl(Portal portal) throws Exception
{
Parent = portal;
this.bullet = Parent.Parent.bullet;
addToPhysicsEngine();
System.out.println(Parent.getID());
}
public void collision(PhysicsCollisionEvent event)
{
System.out.println(event);
if(event.getNodeA() == null || event.getNodeB() == null)
{
System.out.println("**************************");
System.out.println(" !!NULL!! ");
System.out.println("**************************");
return;
}
String A = event.getNodeA().getName();
String B = event.getNodeB().getName();
if(A.startsWith("Portal"))
{
System.out.println(B);
Ship S = (Ship) event.getNodeB();
this.doJump(S);
}
else if(B.startsWith("Portal"))
{
System.out.println(A);
Ship S = (Ship) event.getNodeA();
this.doJump(S);
}
}
public synchronized void doJump(Ship Ship)
{
Parent.Parent.doWellCleanUp();
Ship.jump(Parent.getJumpCords());
System.out.println("JUMP!!");
}
private synchronized void addToPhysicsEngine() throws Exception
{
BoxCollisionShape B = new BoxCollisionShape(new Vector3f(.5f, .5f, .5f));
super.setCollisionShape(B);
this.bullet.getPhysicsSpace().add(this);
this.bullet.getPhysicsSpace().addCollisionListener(this);
}
}
[/java]