banner



How To Animate An Object Rotating In Jme 3

Lesson seven: Animation

Return to the home page

The goal:

Click for full size. Hover over for more details.

Click hither to download the lawmaking. (If the page opens in the broswer, you can right click on link and click "Save Link As", or right click on the page and click "Salve As")

        // most applications volition want to extend the SimpleGame grade             import com.jme.app.SimpleGame;                          // a vector containing 3 bladder numbers;             //   used to store positions and translations             import com.jme.math.Vector3f;             import com.jme.math.Quaternion;             import com.jme.math.FastMath;                          // geometric shapes used in program             import com.jme.scene.shape.Box;                          // use predefined colors and create custom colors             import com.jme.renderer.ColorRGBA;                          // work with bones materials             import com.jme.scene.state.MaterialState;                          // process user input             import com.jme.input.InputHandler;             import com.jme.input.KeyInput;             import com.jme.input.KeyBindingManager;             import com.jme.input.FirstPersonHandler;                          // an internal vertex of the scenegraph tree;             //  use to group together objects,             //  utilize render settings, which affects all child vertices             import com.jme.scene.Node;             import com.jme.scene.Spatial;             import com.jme.scene.Geometry;                          // animate spatials             import com.jme.blitheness.SpatialTransformer;                          /**              *   Demonstrate two types of movement:              *   translation (uses Vectors)              *   and rotation (using Quaternions).              */             public class AnimDemo extends SimpleGame             {                // a geometric shape that will exist rendered                Box myBox;                SpatialTransformer myMover;                             public static void principal(String[] args)                {                    // create an instance of this programme                    AnimDemo app = new AnimDemo();                                 // before the main program starts, brandish the graphics configuration window                    //   where the user tin choose amongst screen resolution, colour depth, and full screen options                    app.setConfigShowMode(ConfigShowMode.AlwaysShow);                                 // The start() method runs simpleInitGame(),                    //   so starts the game loop: simpleUpdate(), then simpleRender()                    // To stop game loop, telephone call finish()                    app.start();                }                             protected void simpleInitGame()                {                    // brand the standard cursor visible.                    org.lwjgl.input.Mouse.setGrabbed(fake);                    // SimpleGame class defaults mouse behavior to rotate view; here we disable this.                    ((FirstPersonHandler)input).getMouseLookHandler().setEnabled(false);                                 // create a material to utilize to geometric objects, use objects' default colour,                    //  apply to front and back, and assign rendering to root node                    MaterialState customMaterial = brandish.getRenderer().createMaterialState();                    customMaterial.setColorMaterial(MaterialState.ColorMaterial.AmbientAndDiffuse);                    customMaterial.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);                    rootNode.setRenderState(customMaterial);                                 myBox = new Box("south", new Vector3f(0,0,0), 1, ane, 1 );                    myBox.setRandomColors();                    rootNode.attachChild( myBox );                                 buildInput();                                 myMover = new SpatialTransformer();                                 // parameter: number of spatial objects this transformer will motility.                    myMover = new SpatialTransformer(1);                                 // load spatial(due south) to be affected into the transformer.                    // parameters: spatial variable, index in SpatialTransformer (like an array), -1 (always).                    myMover.setObject( myBox, 0, -1 );                                 // draw transformations of each object at each betoken in fourth dimension.                    // parameters: index in SpatialTransformer, fourth dimension at which this state should be reached,                    //   and Vector3f||Quaternion||Vector3f (for Position||Rotation||Scale)                    myMover.setPosition( 0, 0, new Vector3f(0,0,0) );                    myMover.setRotation( 0, 0, new Quaternion() );                    myMover.setScale( 0, 0, new Vector3f(1,one,one) );                                 // from starting time until given time, the given transformation volition be continuously applied                    myMover.setPosition( 0, 1, new Vector3f(5,5,v) );                    myMover.setPosition( 0, ii, new Vector3f(0,0,0) );                    myMover.setRotation( 0, 2, new Quaternion() );                    myMover.setRotation( 0, iii, new Quaternion().fromAngleAxis(FastMath.PI / 3, new Vector3f(1,i,1)) );                    myMover.setScale( 0, iii, new Vector3f(1,1,1) );                    myMover.setScale( 0, four, new Vector3f(1,3,one) );                    myMover.setScale( 0, 5, new Vector3f(iii,3,one) );                    myMover.setScale( 0, half dozen, new Vector3f(ane,1,i) );                    myMover.setPosition( 0, 6.0f, new Vector3f(0,0,0) );                    myMover.setPosition( 0, vi.5f, new Vector3f(2,0,0) );                    myMover.setPosition( 0, vii.0f, new Vector3f(2,2,0) );                    myMover.setPosition( 0, 7.5f, new Vector3f(0,2,0) );                    myMover.setPosition( 0, 8.0f, new Vector3f(0,0,0) );                                 // automatically generate all of the small, incrimental values not specified above. Makes the animation dainty and smooth                    myMover.interpolateMissing();                    // choose the object(southward) to exist controlled by this SpatialTransformer                    // Notation: user will not be able to move myBox unless myBox.clearControllers() is called                    myBox.addController(myMover);                    // don't start animator until nosotros say so.                    myMover.setActive(faux);                }                             protected void simpleUpdate()                {                    effort {   Thread.sleep(g/120);   } // force decrease in frames per second                    catch (Exception e) {             }                                 processInput();                }                             protected void simpleRender()                {                    // nothing to add                }                             // create input bindings                public void buildInput()                {                    // remove all pre-existing key assignments                    // KeyBindingManager.getKeyBindingManager().removeAll();                                 KeyBindingManager.getKeyBindingManager().set up( "exit", KeyInput.KEY_ESCAPE );                    KeyBindingManager.getKeyBindingManager().set( "animateBox", KeyInput.KEY_1 );                    KeyBindingManager.getKeyBindingManager().set up( "animatePause", KeyInput.KEY_2 );                    KeyBindingManager.getKeyBindingManager().set( "animateRestart",  KeyInput.KEY_3 );                    KeyBindingManager.getKeyBindingManager().ready( "animateStats", KeyInput.KEY_4 );                }                             // process input entered since concluding update.                public void processInput()                {                    // to determine if SpatialTransformer is currently animative                    //  (because it remains agile even when animation sequence has completed)                    boolean currentlyTransforming =                    ( myMover.getCurTime() > 0 && myMover.getCurTime() < myMover.getMaxTime() );                                 if (KeyBindingManager.getKeyBindingManager().isValidCommand("animateBox", false))                        myMover.setActive(true);                                 if (KeyBindingManager.getKeyBindingManager().isValidCommand("animatePause", false))                        myMover.setActive( !myMover.isActive() );                                 if (KeyBindingManager.getKeyBindingManager().isValidCommand("animateRestart", false))                        myMover.setCurTime( 0 );                                 if (KeyBindingManager.getKeyBindingManager().isValidCommand("animateStats", false))                    {                        Organization.out.println("Animator fourth dimension:   " + myMover.getCurTime() );                        System.out.println("Animator active? " + myMover.isActive() );                    }                }             }      

Render to the home page

Source: https://home.adelphi.edu/~ni18128/jmet/tutorials/lesson7.html

Posted by: thompsonmorpegir.blogspot.com

0 Response to "How To Animate An Object Rotating In Jme 3"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel