I was trying to make a bullet class which stores all the info about bullets. Then I made an array of the bullet class in my weapon class. For testing I gave each bullet a different position. When I added each of the bullets to the rootNode I only saw one bullet in the world. For checking, I printed the position of all the bullets and was surprised to see that all the bullet had the same position with the last specified position. I double checked my code if i made any mistake with the position but I think i didn’t.
Here is an example of what I am doing:
[java]
public class Bullet
{
public Spatial model;
public Vector3f position;
public float size;
public Bullet(Spatial model, Vector3f position, float size)
{
this.model = model;
this.position = position;
this.size = size;
setupLocals();
}
private void setupLocals()
{
model.setLocalTranslation(position);
model.setLocalScale(size);
}
}
public class Weapon
{
public Spatial model;
public Vector3f position;
public float size;
public Bullet bullet [];
public int max = 5;
public Weapon(Spatial model, Vector3f position, float size, Spatial bulletModel, float bulletSize)
{
this.model = model;
this.position = position;
this.size = size;
setupLocals();
setupBullet(bulletModel, bulletSize);
}
private void setupLocals()
{
model.setLocalTranslation(position);
model.setLocalScale(size);
}
private void setupBullet(Spatial bulletModel, float bulletSize)
{
bullet = new Bullet[max];
for(int i = 0; i < bullet.length; i++)
{
bullet = new Bullet(bulletModels, new Vector3f(i,i,i), bulletSize);
}
}
}
public class main extends SimpleApplication
{
Weapon weapon;
public static void main(String [] args)
{
main app = new main();
app.start();
}
public void simpleInitApp()
{
Spatial weaponModel = assetManager.loadModel(“Models/Weapon…”); //Weapon Model
Spatial bulletModel = assetManager.loadModel(“Models/Bullet…”); //Bullet Model
weapon = new Weapon(weaponModel, Vector3f.ZERO, 1, bulletModel, 0.5f);
rootNode.attachChild(weapon.model);
for(int k = 0; k < weapon.bullet.length; k++)
{
rootNode.attachChild(weapon.bullet[k].model);
}
for(int k = 0; k < weapon.bullet.length; k++)
{
System.out.println(“No.”+k+" = "+weapon.bullet[k].model.getLocalTranslation()); //For checking the positions
}
}
}
[/java]
After printing their LocalTranslation they all seems to have the same Localtranslation. If I am doing something wrong please tell me.
K Out!
you probably only have one model instance, which is being referenced by all "Bullet"s.
you could try (sudo-ish code) :
[java]// line 44
bullet = new Bullet(bulletModel.clone(), new Vector3f(i,i,i), bulletSize);[/java]
or (but i wouldn’t recommend this method, but may help testing/debugging nb you will need to pass in assetManager, or you could defined a getBulletMesh method in your main class to return a new Spatial for you)
[java]// line 44
bullet = new Bullet(assetManager.loadModel(“Models/Bullet…”), new Vector3f(i,i,i), bulletSize);[/java]
also, line 44 “bulletModels” … bulletModels is not defined anywhere, perhaps you need populate (and possibly define) that prior to calling it.
Good luck
James
many thanks james. Yes it worked. I cloned the models and now there are multiple bullets. I guess I only needed to call the .clone(). Many thanks.
K Out!