Home > Back-end >  JMonkeyEngine3 game engine can't use physics engine
JMonkeyEngine3 game engine can't use physics engine

Time:09-28

Recently, look at the JMonkeyEngine3 tutorial, make the physics engine,
This is the test code
Package Test. Test1;

The import com. Jme3. App. SimpleApplication;
The import com. Jme3. Bullet. BulletAppState;
The import com. Jme3. Bullet. Collision. Shapes. BoxCollisionShape;
The import com. Jme3. Bullet. Collision. Shapes. SphereCollisionShape;
The import com. Jme3. Bullet. Control. RigidBodyControl;
The import com. Jme3. Input. KeyInput;
The import com. Jme3. Input. MouseInput;
The import com. Jme3. Input. Controls. ActionListener;
The import com. Jme3. Input. Controls. KeyTrigger;
The import com. Jme3. Input. Controls. MouseButtonTrigger;
The import com. Jme3. Light. AmbientLight;
The import com. Jme3. Light. DirectionalLight;
The import com. Jme3. Material. The material;
The import com. Jme3. Math. ColorRGBA;
The import com. Jme3. Math. Vector2f;
The import com. Jme3. Math. Vector3f;
The import com. Jme3. Scene. Geometry;
The import com. Jme3. Scene. Shape. The Box;
The import com. Jme3. Scene. Shape. Sphere;
The import com. Jme3. Scene. Shape. Sphere. TextureMode;
The import com. Jme3. Texture. The texture;
The import com. Jme3. Texture. The texture. WrapMode;

Public class Test extends SimpleApplication implements ActionListener {

/* *
* fire, fire balls, mouse the left key trigger,
*/
Public final static String FIRE="FIRE";
/* *
* to show or hide the debug BulletAppState shape, press the blank space key trigger,
*/
Public final static String DEBUG="DEBUG";

/* * * brick size/
Private static final float brickLength=0.48 f;
Private static final float brickWidth=0.24 f;
Private static final float brickHeight=0.12 f;

Private BulletAppState BulletAppState;

@ Override
Public void simpleInitApp () {
CAM. SetLocation (new Vector3f (0, 4 f, 6 f));
CAM. LookAt (new Vector3f (2, 2, 0), Vector3f. UNIT_Y);

BulletAppState=new bulletAppState ();
StateManager. Attach (bulletAppState);

//initialize button
InitKeys ();

//initialize the light
InitLight ();

//initialize the scene
InitScene ();
}

@ Override
Public void onAction (String name, Boolean isPressed, float TPF) {
If (isPressed) {
If (FIRE) equals (name)) {
ShootBall ();
} else if (DEBUG. Equals (name)) {
Boolean debugEnabled=bulletAppState. IsDebugEnabled ();
BulletAppState setDebugEnabled (! DebugEnabled);
}
}
}

/* *
* initialize input keys
*/
Private void initKeys () {
InputManager. AddMapping (FIRE, new MouseButtonTrigger (MouseInput. BUTTON_LEFT));
InputManager. AddMapping (DEBUG, new KeyTrigger (KeyInput. KEY_SPACE));
InputManager. AddListener (this, the FIRE, the DEBUG).
}

/* *
* initialize the light
*/
Private void initLight () {
//the ambient light
AmbientLight ambient=new AmbientLight ();
Ambient. SetColor (new ColorRGBA (0.3 f, f 0.3, 0.3 f, 1 f));

//the sun
DirectionalLight sun=new DirectionalLight ();
Sun. SetDirection (new Vector3f (1, 2, 3). The normalizeLocal ());

RootNode. AddLight (ambient);
RootNode. AddLight (sun);
}

/* *
* initialize the scene
*/
Private void initScene () {
MakeFloor ();
MakeWall ();
}

/* *
* production floor
*/
Private void makeFloor () {
//grid
The Box floor=new Box (10 f, f 0.1, 5 f);
Floor. ScaleTextureCoordinates (new Vector2f (3, 6));

//
Material mat=new Material (assetManager, "Common/MatDefs/Misc/Unshaded j3md");
Texture Tex=assetManager. LoadTexture (" Textures/Terrain/Pond, Pond. JPG ");
Tex. SetWrap (WrapMode. Repeat);
Mat. SetTexture (" ColorMap, Tex);

//geometry
Geometry the java.awt.geom=new Geometry (" floor ", floor);
The java.awt.geom. SetMaterial (mat);
The java.awt.geom. SetLocalTranslation (0, 0.1 f, 0).//down a certain distance, the floor surface and xoz plane,

//rigid body
RigidBodyControl rigidBody=new RigidBodyControl (0);
The java.awt.geom. AddControl (rigidBody);
RigidBody. SetCollisionShape (new BoxCollisionShape (new Vector3f (10 f, f 0.1, 5 f)));

RootNode. AttachChild (the java.awt.geom);
BulletAppState. GetPhysicsSpace (). The add (rigidBody);
}

/* *
* build a wall
*/
Private void makeWall () {
//use a for loop to generate a wall made up of many brick wall,
Float startpt=brickLength/4;
Float height=0;
For (int j=0; J & lt; 15. J++) {
For (int I=0; I & lt; 6; I++) {
BrickLength Vector3f n=new Vector3f (I * * 2 + startpt, brickHeight + height, 0).
MakeBrick (vt);
}
Startpt=- startpt;
Height +=2 * brickHeight;
}
}

/* *
* in the specified location placed a physical brick
*
* @ param loc bricks the location of the
*/
Private void makeBrick Vector3f (loc) {
//grid
Box Box=new Box (brickLength brickHeight, brickWidth);
Box. ScaleTextureCoordinates (new Vector2f (1 f,. 5 f));

//
Material mat=new Material (assetManager, "Common/MatDefs/Misc/Unshaded j3md");
Texture Tex=assetManager. LoadTexture (" Textures/Terrain/BrickWall/BrickWall. JPG ");
Mat. SetTexture (" ColorMap, Tex);

//geometry
Geometry the java.awt.geom=new Geometry (" brick ", box);
The java.awt.geom. SetMaterial (mat);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related