I need assistance with understanding how to utilize classes instead of cramming everything in the ma

Okay so how would I for example make it so I can shoot cannon balls by calling the funciton from another class instead of the main file, Lets use this tutorial as an example: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics



I’m guessing I"d have to port the makeCannonBall(); function into a class I’ve tried this but apparently I don’t know how to attach models to the rootnode.

If the function is called makeCannonBall(); use the following (must be done in the class itself):



[java]makeCannonBall(Node nodeName);[/java]



Then, from the Main class, when calling the function, insert the rootNode into the brackets.



From the function, you can then attach the model to the rootNode as following:



[java]rootNode.attachChild(model);[/java]



Hope I answered your question.

That works but now instead of loading gemotry I want to load a model(.j3o file) For some reason the assetManager.loadModel(); Dosen’t seem to work in a external class file, i get a “Symbol cannot be found”

JME requires some level of Java experience before you can really use it. I strongly recommend that you run through some general Java tutorials.



The issue here is that the other class doesn’t have an assetManager field. This is an extremely basic Java coding thing. Without a semi-decent foundation you are going to get tripped up on hundreds of little things like this.

I agree with pspeed. But to close this matter, you must replace (Node rootNode) with whatever you need. For example:



[java](AssetManager assetManager)[/java]



Effectively, it’s like this:



(TypeOfObject objectNameForThisClass)



Correct me if I’m wrong.



Just Google ‘Java tutorials’ to learn it.

@pspeed said:
JME requires some level of Java experience before you can really use it. I strongly recommend that you run through some general Java tutorials.

The issue here is that the other class doesn't have an assetManager field. This is an extremely basic Java coding thing. Without a semi-decent foundation you are going to get tripped up on hundreds of little things like this.


Assetmanager is found out side of JME?

I understand that there is a library missing. I wasn't obvlivious to that, I'm just trying to figure out how to load models while in another class no need to get all condensending. I haven't found one tutorial that demonstrates the use of loading models from another class file seperate from the main running file, they are all one file tutorials. I have a strong foundation of more then multiple programming languages including Actionscript 3, Unity3D, PHP, MySQL.



@memonick said:
I agree with pspeed. But to close this matter, you must replace (Node rootNode) with whatever you need. For example:

[java](AssetManager assetManager)[/java]

Effectively, it's like this:

(TypeOfObject objectNameForThisClass)

Correct me if I'm wrong.

Just Google 'Java tutorials' to learn it.

As a work around I'm making the main class load a new model into the newly created class control the model, there must be a better way then this, I know ActionScript3 is similar in structure and all I had to do in AS3 was reference the movieclip linkage name, in JME3 I'm assuming there is a usefull way of loading models in classes?

I forgot to mention I already made the external class extends Simple Application still don’t know why my model wont load, it says in the output box that it attached the node but I no see it :confused:

It could be becuase my blender file didn’t have a texture and therefore its model is transparent.

I don’t quite know what you mean by “external class”.

Are you well familiarised with OOP?

If so, you should know what fields and methods are and the difference between class and object.

Assuming you are, the path to solving your problem would probably be clearer to see if you provided an example of the relevant code where you’re having problems, or a test case.

Are you working in the jME SDK or another IDE?

If working in another IDE, are you sure you have all the required libraries in the class path when compiling?

@shanebeastlyb said:
I forgot to mention I already made the external class extends Simple Application still don't know why my model wont load, it says in the output box that it attached the node but I no see it :/


Only one class in your application should extend SimpleApplication. Otherwise you have more than one application in your application and that makes no sense.
@shanebeastlyb said:
Assetmanager is found out side of JME?

I understand that there is a library missing. I wasn't obvlivious to that, I'm just trying to figure out how to load models while in another class no need to get all condensending. I haven't found one tutorial that demonstrates the use of loading models from another class file seperate from the main running file, they are all one file tutorials. I have a strong foundation of more then multiple programming languages including Actionscript 3, Unity3D, PHP, MySQL.


AssetManager is another class. You are not missing a library, you are just missing a reference.

Please, for yourself, go do some Java tutorials. You lack even the basic understanding of classes and fields.
1 Like
@Tumaini said:
I don't quite know what you mean by "external class".
Are you well familiarised with OOP?
If so, you should know what fields and methods are and the difference between class and object.
Assuming you are, the path to solving your problem would probably be clearer to see if you provided an example of the relevant code where you're having problems, or a test case.
Are you working in the jME SDK or another IDE?
If working in another IDE, are you sure you have all the required libraries in the class path when compiling?

I'm not very good with programming terminology, I almost always program by myself and therefore haven't aquired the skill to properly communicate with others what exactly are my issues and the proper etiqutte to this kind of thing but I assure you I am very capable of understanding I just need an answer or some direction I'm not asking anyone to do work for me or anything like that (not that I'm accusing anyone of suggesting that)I just want to learn. I'm familiar with OOP. Lets see if i can word this differently

I'm making a test game just for learning becuase I would like to some day down the road help make video games.
I have the Main.java file and then I have a ResourceBlock.java file. The main file will load the ground/terrain, skybox and the player, and then the player will Left click on the mouse a block will appear where the player is looking at. At the moment I just have the Block spawn at the default location when using rootNode.attachChild(); just to see if i can make the block spawn. Everything works perfectly fine with the exception of that when I left click the box dosen't show up at the default location even tho in the console is says model attached to node.
Here is my code

Check out mostly the new ResourceBlock() in the Left Mouse click action

Main.java
[java]package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.math.Quaternion;
import com.jme3.math.*;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.*;
import com.jme3.input.ChaseCamera;
import com.jme3.input.*;
import com.jme3.input.controls.*;
import com.jme3.light.*;
import com.jme3.math.Matrix3f;
import com.jme3.math.FastMath;
import com.jme3.bullet.control.*;
import com.jme3.bullet.*;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.bullet.collision.shapes.*;
import com.jme3.texture.Texture;

import mygame.ResourceBlock;



/**
* test
* @author shane B.
* @date April/03/2012
*/
public class Main extends SimpleApplication implements ActionListener{
//Declare class variables
private CharacterControl player;
private float playerSpeed;
private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;

public static void main(String[] args) {
Main app = new Main();
app.start();
}

public void simpleInitApp() {
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);


//Set player speed
playerSpeed = 0.2f;

//Add light to enviroment
setUpLight();
setUpInput();

sceneModel = assetManager.loadModel("Scenes/mainTerrain.j3o");

// We set up collision detection for the player by creating
// a capsule collision shape and a CharacterControl.
// The CharacterControl offers extra settings for
// size, stepheight, jumping, falling, and gravity.
// We also put the player in its starting position.
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(70);
player.setPhysicsLocation(new Vector3f(0, 10, 0));



// We attach the scene and the player to the rootNode and the physics space,
// to make them appear in the game world.
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(sceneModel);
bulletAppState.getPhysicsSpace().add(player);

//Attach skybox
rootNode.attachChild(assetManager.loadModel("Scenes/mainSkyBox.j3o"));

}


private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);

DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}

private void setUpInput(){
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));

inputManager.addMapping("Left Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");

inputManager.addListener(this, "Left Click");
}

public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
if (value) { left = true; } else { left = false; }
} else if (binding.equals("Right")) {
if (value) { right = true; } else { right = false; }
} else if (binding.equals("Up")) {
if (value) { up = true; } else { up = false; }
} else if (binding.equals("Down")) {
if (value) { down = true; } else { down = false; }
} else if (binding.equals("Jump")) {
player.jump();
} else if(binding.equals("Left Click")){
//Create new block

ResourceBlock Block = new ResourceBlock();
Block.newBlock();

}
}

@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(playerSpeed);
Vector3f camLeft = cam.getLeft().clone().multLocal(playerSpeed);
walkDirection.set(0, 0, 0);
if (left) { walkDirection.addLocal(camLeft); }
if (right) { walkDirection.addLocal(camLeft.negate()); }
if (up) { walkDirection.addLocal(camDir); }
if (down) { walkDirection.addLocal(camDir.negate()); }
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}

public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]




ResourceBlock.java
[java]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.scene.*;
import com.jme3.app.SimpleApplication;
import com.jme3.material.*;
import com.jme3.texture.*;

/**
*
* @author xenland
*/
public class ResourceBlock extends SimpleApplication{
public void ResourceBlock(){
//Create block

}

public void newBlock(){
Spatial tmp_block = assetManager.loadModel("Models/box.j3o");
rootNode.attachChild(tmp_block);
}

@Override
public void simpleInitApp(){

}

}
[/java]

Let me know if you have anymore questions... I'd be happy to answer them.
@shanebeastlyb said:
I'm familiar with OOP.


You say that but I don't think you really know what it means. But nevermind. I will help you this time but your knowledge of Java and OOP will prohibit you from making much progress until you run through some Java tutorials.

Just the fact that you've extended SimpleApplication in two separate classes shows your low skill level pretty well. How many applications do you want in your application? There should be exactly one application in your application. So exactly one SimpleApplication subclass.

Anyway... back to the problem at hand...

ResourceBlock.java
[java]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.scene.*;
import com.jme3.material.*;
import com.jme3.texture.*;

/**
*
* @author xenland
*/
public class ResourceBlock {
public void ResourceBlock(){
//Create block
}

public void newBlock( AssetManager assetManager, Node rootNode ){
Spatial tmp_block = assetManager.loadModel("Models/box.j3o");
rootNode.attachChild(tmp_block);
}
}
[/java]

Then where you call it:
ResourceBlock block = new ResourceBlock();
block.newBlock(assetManager, rootNode);

Alternately, you could do:
ResourceBlock.java
[java]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.scene.*;
import com.jme3.material.*;
import com.jme3.texture.*;

/**
*
* @author xenland
*/
public class ResourceBlock {
private AssetManager assetManager;
private Node rootNode;

public void ResourceBlock(AssetManager assetManager, Node rootNode){
//Create block
this.assetManager = assetManager;
this.rootNode = rootNode;
}

public void newBlock(){
Spatial tmp_block = assetManager.loadModel("Models/box.j3o");
rootNode.attachChild(tmp_block);
}
}
[/java]

And then:
ResourceBlock block = new ResourceBlock(assetManager, rootNode);
block.newBlock();

Instead of assetManager and rootNode, you could also pass the whole application object and get those as needed. Left as an exercise for the reader.

A word on experience to all of you nascent Java/OOP starters: a Java developer with the necessary experience to properly program against a toolkit like JME would be able to write a text adventure game in a weekend. Easily. (http://www.malinche.net/demos/) Something simple like the original "Dungeon" (which was even built into Emacs) would be trivial to write for anyone with the requisite skill. There is really none of that which isn't applicable if you dumb down the command parser to simple two token commands... so it's not even like you are learning unnecessary skills.

So if something like that seems hard or impossible... go back to your Java tutorials a bit, maybe. It's not that we don't want you here... it's just that teaching the fundamental basics of programming doesn't really interest us and we aren't really that good at it. We'd rather answer JME questions.

Thanks, your solution helps me clear up alot of preconceptions I had previously about how JME is really set up. I guess I could disect JME3 SDK’s source code and find what I’m looking for except I wouldn’t know where to begin. I appreaciate this solution alot; this question really goes beyond to what it might look like at face value, its a step forward to me helping understand how the interworkings of jme work. In the future I will be more aware of the difference between Java and JME questions.



Cheers mates!

@shanebeastlyb said:
Thanks, your solution helps me clear up alot of preconceptions I had previously about how JME is really set up. I guess I could disect JME3 SDK's source code and find what I'm looking for except I wouldn't know where to begin. I appreaciate this solution alot; this question really goes beyond to what it might look like at face value, its a step forward to me helping understand how the interworkings of jme work. In the future I will be more aware of the difference between Java and JME questions.

Cheers mates!


For the record, this isn't a JME question at all.

[java]
class SomeClass {
private int myVariable = 24;

public void doStuff() {
AnotherClass a = new AnotherClass();
a.someUtility();
}
}
[/java]

[java]
class AnotherClass {
public void someUtility() {
System.out.println( "Why can't I see myVariable:" + myVariable );
}
}
[/java]

You'd have had exactly the same problem with the above. A basic object scoping problem.

maybe scoping was the problem my ResourceBlock class still continues to read out “Asset Manager symbol can not be found” when using your suggestion

[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    /

    package mygame;

    import com.jme3.scene.
    ;

    import com.jme3.material.;

    import com.jme3.texture.
    ;



    /**

    *
  • @author xenland

    */

    public class ResourceBlock {

    private AssetManager assetManager;

    private Node rootNode;



    public void ResourceBlock(AssetManager assetManager, Node rootNode){

    //Create block

    this.assetManager = assetManager;

    this.rootNode = rootNode;

    }



    public void newBlock(){

    Spatial tmp_block = assetManager.loadModel(“Models/box.j3o”);

    rootNode.attachChild(tmp_block);

    }

    }[/java]



    which was why i was doing extends simpleapplication so it wouldn’t give me errors.

Oh wait never I forgot to import " import com.jme3.asset.*; " it works great now! Thanks mate!

Okay so here is a JME question why is it when I spawn a box it just sits there in mid air and dosen’t drop to the floor

I’ve cut out the part of the code where the user left clicks there mouse it will execute the following

[java]

//Create new block

Spatial tmp_block = assetManager.loadModel(“Models/box.j3o”);

rootNode.attachChild(tmp_block);

tmp_block.move(0, 20, 0);







ball_phy = new RigidBodyControl(1f);

tmp_block.addControl(ball_phy);



bulletAppState.getPhysicsSpace().add(tmp_block);

[/java]







Full code here…



[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.math.;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.
;

import com.jme3.input.;

import com.jme3.input.controls.
;

import com.jme3.light.;

import com.jme3.bullet.control.
;

import com.jme3.bullet.;

import com.jme3.bullet.collision.shapes.
;





/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication implements ActionListener {



    private CharacterControl player;

    private Spatial groundSignal;

    private BulletAppState bulletAppState;

    private Vector3f walkDirection = new Vector3f();

    private float playerSpeed;

    private RigidBodyControl ball_phy;

    private boolean left = false, right = false, up = false, down = false;



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    //Initiate physics

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);



    //Create the groundsignal, the point where it is the absolute most bottom of the map

    groundSignal = assetManager.loadModel("Scenes/mainTerrain.j3o");

    groundSignal.move(0, 30, 0);

    rootNode.attachChild(groundSignal); //Attach to stage



    //Set player speed

    playerSpeed = 0.2f;



    //Add light to enviroment

    setUpLight();



    //Remap Input

    setUpInput();



    //Setup player piece, create a capsule w/ physics attached

    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

    player = new CharacterControl(capsuleShape, 0.05f);

    player.setJumpSpeed(25);

    player.setFallSpeed(30);

    player.setGravity(65);

    player.setPhysicsLocation(new Vector3f(0, 20, 0));





    //Applay physics to objects

    bulletAppState.getPhysicsSpace().add(player);

    bulletAppState.getPhysicsSpace().add(groundSignal); //Attach groundSignal to physics





    }



    private void setUpLight() {

    // We add light so we see the scene

    AmbientLight al = new AmbientLight();

    al.setColor(ColorRGBA.White.mult(1.3f));

    rootNode.addLight(al);



    DirectionalLight dl = new DirectionalLight();

    dl.setColor(ColorRGBA.White);

    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());

    rootNode.addLight(dl);

    }



    private void setUpInput(){

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));

    inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));

    inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));

    inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));



    inputManager.addMapping("Left Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));



    inputManager.addListener(this, "Left");

    inputManager.addListener(this, "Right");

    inputManager.addListener(this, "Up");

    inputManager.addListener(this, "Down");

    inputManager.addListener(this, "Jump");



    inputManager.addListener(this, "Left Click");

    }



    public void onAction(String binding, boolean value, float tpf) {

    if (binding.equals("Left")) {

    if (value) { left = true; } else { left = false; }

    } else if (binding.equals("Right")) {

    if (value) { right = true; } else { right = false; }

    } else if (binding.equals("Up")) {

    if (value) { up = true; } else { up = false; }

    } else if (binding.equals("Down")) {

    if (value) { down = true; } else { down = false; }

    } else if (binding.equals("Jump")) {

    player.jump();

    } else if(binding.equals("Left Click")){

    //Create new block

    Spatial tmp_block = assetManager.loadModel("Models/box.j3o");

    rootNode.attachChild(tmp_block);

    tmp_block.move(0, 20, 0);







    ball_phy = new RigidBodyControl(1f);

    tmp_block.addControl(ball_phy);



    bulletAppState.getPhysicsSpace().add(tmp_block);



    }

    }



    @Override

    public void simpleUpdate(float tpf) {

    Vector3f camDir = cam.getDirection().clone().multLocal(playerSpeed);

    Vector3f camLeft = cam.getLeft().clone().multLocal(playerSpeed);

    walkDirection.set(0, 0, 0);

    if (left) { walkDirection.addLocal(camLeft); }

    if (right) { walkDirection.addLocal(camLeft.negate()); }

    if (up) { walkDirection.addLocal(camDir); }

    if (down) { walkDirection.addLocal(camDir.negate()); }

    player.setWalkDirection(walkDirection);

    cam.setLocation(player.getPhysicsLocation());

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }

    [/java]