[Tutorial] Collision objects from TMX map

  ... tutorials on how to use AndEngine.

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Wed Jul 13, 2011 9:15 am

I'm getting about 46 fps on my desire. What device are you using?
Beer Freeze - Never leave your beer in the freezer again
Crundy
 
Posts: 96
Joined: Mon Jun 20, 2011 4:07 pm

Re: [Tutorial] Collision objects from TMX map

Postby rioneye » Wed Jul 13, 2011 8:13 pm

Crundy wrote:I'm getting about 46 fps on my desire. What device are you using?

HTC EVO. I think it only gets that low when its plugged into my pc. I took it out and saw a jump in FPS but couldn't measure it.
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Wed Jul 13, 2011 10:06 pm

I tried using my wife's Wildfire and was only getting 10fps. I wonder if adding the object layers to the scene is causing a slowdown?
Beer Freeze - Never leave your beer in the freezer again
Crundy
 
Posts: 96
Joined: Mon Jun 20, 2011 4:07 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Fri Jul 15, 2011 4:04 pm

Nicolas mentioned somewhere that layers does affect performance quite a bit. And I expect having physics in the script also takes up some performance.

I still have trouble getting the example working on its own. I really cannot see what I am doing wrong, but performance tests where what I had hoped to be doing. I have access to a HTC Hero, HTC Sensation and Samsung Galaxy S. 3 different generations of Android devices.

I guess I can just test it out directly in the newest AndEngineExamples TMX example as I got working at first but that would suck.

I got the newest andengine.jar from the newest AndEngine Examples again. This was an active build, the last one was not. I got something else working but not this script, even after fixing some changes.

The script I am using now is this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package tmx.test;
  2.  
  3. import javax.microedition.khronos.opengles.GL10;
  4.  
  5. import org.anddev.andengine.engine.Engine;
  6. import org.anddev.andengine.engine.camera.BoundCamera;
  7. import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
  8. import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
  9. import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
  10. import org.anddev.andengine.engine.options.EngineOptions;
  11. import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
  12. import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
  13. import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
  14. import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
  15. import org.anddev.andengine.entity.layer.tiled.tmx.TMXObject;
  16. import org.anddev.andengine.entity.layer.tiled.tmx.TMXObjectGroup;
  17. import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
  18. import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
  19. import org.anddev.andengine.entity.primitive.Rectangle;
  20. import org.anddev.andengine.entity.scene.Scene;
  21. import org.anddev.andengine.entity.shape.Shape;
  22. import org.anddev.andengine.entity.sprite.AnimatedSprite;
  23. import org.anddev.andengine.entity.util.FPSLogger;
  24. import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
  25. import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
  26. import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
  27. import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
  28. import org.anddev.andengine.opengl.texture.Texture;
  29. import org.anddev.andengine.opengl.texture.TextureOptions;
  30. import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
  31. import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
  32. import org.anddev.andengine.opengl.texture.region.TextureRegion;
  33. import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
  34. import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
  35. import org.anddev.andengine.ui.activity.BaseGameActivity;
  36. import org.anddev.andengine.util.Debug;
  37.  
  38.  
  39. import com.badlogic.gdx.math.Vector2;
  40. import com.badlogic.gdx.physics.box2d.Body;
  41. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  42. import com.badlogic.gdx.physics.box2d.FixtureDef;
  43.  
  44. public class CrundysTMXCollision3Activity extends BaseGameActivity {
  45.          
  46.     private TMXTiledMap mTMXTiledMap;
  47.     private BoundCamera mBoundChaseCamera;
  48.  
  49.     private static final int CAMERA_WIDTH = 480;
  50. private static final int CAMERA_HEIGHT = 320;
  51. private Scene mScene;
  52.  
  53.     private static final long[] ANIMATE_DURATION = new long[]{200, 200, 200};
  54.     private static final int PLAYER_VELOCITY = 2;
  55.  
  56.    
  57.         private BitmapTextureAtlas mBitmapTextureAtlas;
  58.     private BitmapTextureAtlas mTexturePlayer;
  59.     private Body mPlayerBody;
  60.     private TiledTextureRegion mPlayerTextureRegion;
  61.         private BitmapTextureAtlas mOnScreenControlTexture;
  62.         private TextureRegion mOnScreenControlBaseTextureRegion;
  63.         private TextureRegion mOnScreenControlKnobTextureRegion;
  64.  
  65.         private DigitalOnScreenControl mDigitalOnScreenControl;
  66.     private PhysicsWorld mPhysicsWorld;
  67.  
  68.     private enum PlayerDirection{
  69.             NONE,
  70.             UP,
  71.             DOWN,
  72.             LEFT,
  73.             RIGHT
  74.     }
  75.     private PlayerDirection playerDirection = PlayerDirection.NONE;
  76.  
  77.  
  78.     @Override
  79.     public Engine onLoadEngine() {
  80.             this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  81.             return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mBoundChaseCamera));
  82.     }
  83.  
  84.     @Override
  85.     public void onLoadResources() {
  86.         BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
  87.  
  88.             // Control texture
  89.             this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  90.                 this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
  91.                 this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
  92.  
  93.             // Player sprite texture
  94.             this.mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
  95.             this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "hero.png", 0, 0, 3, 4); // 72x128
  96.  
  97.             // Load the textures
  98.             this.mEngine.getTextureManager().loadTextures(this.mTexturePlayer, this.mOnScreenControlTexture);
  99.     }
  100.  
  101.     @Override
  102.     public Scene onLoadScene() {
  103.             this.mEngine.registerUpdateHandler(new FPSLogger());
  104.            
  105.             // Create physics world
  106.             this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
  107.            
  108.             // Create the scene and register the physics world
  109.             mScene = new Scene();
  110.             mScene.registerUpdateHandler(this.mPhysicsWorld);
  111.  
  112.             // Load the TMX map
  113.             try {
  114.                     final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.NEAREST, null);
  115.                     this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "test.tmx");
  116.             } catch (final TMXLoadException tmxle) {
  117.                     Debug.e(tmxle);
  118.             }
  119.  
  120.             // Add the non-object layers to the scene
  121.             for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
  122.                     TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
  123.                     if (!layer.getTMXLayerProperties().containsTMXProperty("wall", "true"))
  124.                     mScene.attachChild(layer);
  125.             }
  126.  
  127.             // Read in the unwalkable blocks from the object layer and create boxes for each
  128.             this.createUnwalkableObjects(mTMXTiledMap);
  129.            
  130.             // Make the camera not exceed the bounds of the TMXEntity.
  131.             final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
  132.             this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
  133.             this.mBoundChaseCamera.setBoundsEnabled(true);
  134.             // Add outer walls
  135.             this.addBounds(tmxLayer.getWidth(), tmxLayer.getHeight());
  136.  
  137.             // Calculate the coordinates for the player, so it's centred on the camera.
  138.             final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
  139.             final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;
  140.  
  141.             // Create the player sprite and add it to the scene.
  142.             final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
  143.             this.mBoundChaseCamera.setChaseEntity(player);
  144.             final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
  145.             mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef);
  146.             this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false){
  147.                     @Override
  148.                     public void onUpdate(float pSecondsElapsed){
  149.                             super.onUpdate(pSecondsElapsed);
  150.                             mBoundChaseCamera.updateChaseEntity();
  151.                     }
  152.             });
  153.             mScene.attachChild(player);
  154.            
  155.             // Add the control
  156.             this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() {
  157.                     @Override
  158.                     public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
  159.                             // Set the correct walking animation
  160.                             if (pValueY == 1){
  161.                                     // Up
  162.                                     if (playerDirection != PlayerDirection.UP){
  163.                                             player.animate(ANIMATE_DURATION, 0, 2, true);
  164.                                             playerDirection = PlayerDirection.UP;
  165.                                     }
  166.                             }else if (pValueY == -1){
  167.                                     // Down
  168.                                     if (playerDirection != PlayerDirection.DOWN){
  169.                                             player.animate(ANIMATE_DURATION, 9, 11, true);
  170.                                             playerDirection = PlayerDirection.DOWN;
  171.                                     }
  172.                             }else if (pValueX == -1){
  173.                                     // Left
  174.                                     if (playerDirection != PlayerDirection.LEFT){
  175.                                             player.animate(ANIMATE_DURATION, 3, 5, true);
  176.                                             playerDirection = PlayerDirection.LEFT;
  177.                                     }
  178.                             }else if (pValueX == 1){
  179.                                     // Right
  180.                                     if (playerDirection != PlayerDirection.RIGHT){
  181.                                             player.animate(ANIMATE_DURATION, 6, 8, true);
  182.                                             playerDirection = PlayerDirection.RIGHT;
  183.                                     }
  184.                             }else{
  185.                                     if (player.isAnimationRunning()){
  186.                                             player.stopAnimation();
  187.                                             playerDirection = PlayerDirection.NONE;
  188.                                     }
  189.                             }
  190.                             // Set the player's velocity
  191.                             mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
  192.                     }
  193.             });
  194.             this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  195.             this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
  196.             this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
  197.             this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
  198.             this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
  199.             this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
  200.             this.mDigitalOnScreenControl.refreshControlKnobPosition();
  201.  
  202.             mScene.setChildScene(this.mDigitalOnScreenControl);
  203.            
  204.             return mScene;
  205.     }
  206.  
  207.     @Override
  208.     public void onLoadComplete() {
  209.             // TODO Auto-generated method stub
  210.  
  211.     }
  212.    
  213.     private void createUnwalkableObjects(TMXTiledMap map){
  214.             // Loop through the object groups
  215.              for(final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {
  216.                      if(group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")){
  217.                              // This is our "wall" layer. Create the boxes from it
  218.                              for(final TMXObject object : group.getTMXObjects()) {
  219.                                     final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
  220.                                     final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
  221.                                     PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
  222.                                     rect.setVisible(false);
  223.                                     mScene.attachChild(rect);
  224.                              }
  225.                      }
  226.              }
  227.     }
  228.    
  229.     private void addBounds(float width, float height){
  230.             final Shape bottom = new Rectangle(0, height - 2, width, 2);
  231.             bottom.setVisible(false);
  232.             final Shape top = new Rectangle(0, 0, width, 2);
  233.             top.setVisible(false);
  234.             final Shape left = new Rectangle(0, 0, 2, height);
  235.             left.setVisible(false);
  236.             final Shape right = new Rectangle(width - 2, 0, 2, height);
  237.             right.setVisible(false);
  238.  
  239.             final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
  240.             PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottom, BodyType.StaticBody, wallFixtureDef);
  241.             PhysicsFactory.createBoxBody(this.mPhysicsWorld, top, BodyType.StaticBody, wallFixtureDef);
  242.             PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  243.             PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  244.  
  245.             this.mScene.attachChild(bottom);
  246.             this.mScene.attachChild(top);
  247.             this.mScene.attachChild(left);
  248.             this.mScene.attachChild(right);
  249.     }
  250.  
  251.  
  252. }
Parsed in 0.079 seconds, using GeSHi 1.0.8.4


I changed a few things, bcause some texture stuff got changed apparantly. Anything else looks wrong?
Last edited by kblood on Sat Jul 16, 2011 9:03 am, edited 1 time in total.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Fri Jul 15, 2011 4:50 pm

30.import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
31.import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;

Is some of the stuff that changed, and I had to change line 86 to 96 so they could work with these changes.

Debugging does not give any answers. I did manage to make the bluescreen test.

I also tried it in the newest AndEngine examples and it does not work with that anymore. Maybe someone could zip a working version of this example? I double checked that I was not just missing assets or something like that. But everything checks out.

Im trying to start from scratch. Making my old projects work in a new one using Android 2.2 as the base engine, allowing for the device to use down to Android 1.6, like it seems AndEngineExamples is doing, and I have made it work with Android 2.2 and my other projects in a new project, but an old AndEngine. Now I am trying to make one of them work with the newest AndEngine, if that works I will then try with this script again, but I doubt it will work since it does not work any other way. So I hope someone can share a zip file of a working project.

I finished testing. Nothing wrong with my way of using Android 2.2 or the new AndEngine.jar revision 289, because managed to get my old tmx tests working with it, using several sprites, digital controls and the TMX background of course.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby alfred112 » Sun Jul 17, 2011 3:18 am

How come whenever i run it, it always stopped unexpectedly?
alfred112
 
Posts: 1
Joined: Sun Jul 17, 2011 3:15 am

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Mon Jul 18, 2011 1:07 pm

alfred112 wrote:How come whenever i run it, it always stopped unexpectedly?


Seems to me it is because it is built with a in between version of AndEngine. Since the working one has not been shared, I will try to figure out which version it was, if it is possible to download previous versions. Seems I figured that out finally. Revision 187 of the AndEngine should make this script work.

I am uploading all of it, for some reason I still cannot get it working in a stand alone example. Maybe because projects does not like having the AndEngine.jar deleted and replaced, and added to the path. I am hoping to make an example with only the minimum of stuff needed in it though, since this one is 42 mbytes. That is because it is the full AndEngine examples package from revision 187. If you load the TMX example, it will load this TMX collision map instead.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Mon Jul 18, 2011 2:04 pm

Here we go:
http://dionysus.dk/devstuff/downloads/a ... les187.zip

Load the TMX example in this and you will see the collision object TMX map example.

To check out the script, then look under src/org.anddev.andengine.examples/TMXTiledMapExample.java to find the script, and try experimenting with it. The original file I renamed to TMXTiledMapExample2.java. Also I made the script use player.png instead of hero.png since its the same file.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby cslwkah » Thu Jul 21, 2011 5:37 am

thanks for your tutorial but I found a fetal error at start.

May you please upload your whole project show we can test directly. You know, andengine has many version of jars and I can't easily identify the problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343): FATAL EXCEPTION: main
  2. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343): java.lang.NullPointerException
  3. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at org.anddev.andengine.opengl.texture.TextureManager.reloadTextures(TextureManager.java:106)
  4. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at org.anddev.andengine.engine.Engine.onResume(Engine.java:429)
  5. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:169)
  6. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
  7. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2001)
  8. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.view.View.dispatchWindowFocusChanged(View.java:3924)
  9. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:659)
  10. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1968)
  11. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.os.Handler.dispatchMessage(Handler.java:99)
  12. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.os.Looper.loop(Looper.java:130)
  13. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at android.app.ActivityThread.main(ActivityThread.java:3683)
  14. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at java.lang.reflect.Method.invokeNative(Native Method)
  15. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at java.lang.reflect.Method.invoke(Method.java:507)
  16. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  17. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  18. 07-21 11:34:09.224: ERROR/AndroidRuntime(13343):     at dalvik.system.NativeStart.main(Native Method)
  19.  
  20.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
cslwkah
 
Posts: 88
Joined: Sat Jan 22, 2011 1:38 pm
Location: HK

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Thu Jul 21, 2011 10:36 am

I've updated the file to use the latest release version of AndEngine, and I've zipped an entire test project for you to download and use. See the original post.

I have noticed that the FPS has dropped to about 20 now. There's only 2 tile layers in the TMX so that shouldn't be causing a big slowdown. Anyone know how to get the FPS up in this example?
Beer Freeze - Never leave your beer in the freezer again
Crundy
 
Posts: 96
Joined: Mon Jun 20, 2011 4:07 pm

PreviousNext

Return to Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 21 guests