[Tutorial] Collision objects from TMX map

  ... tutorials on how to use AndEngine.

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Wed Jul 06, 2011 10:06 am

I tried loads of ways of doing it like that and couldn't find a way that worked well. The character would either get stuck on the tile, or would bounce back and forth against it, which looked terrible.

If you do find a better way of doing it like that then definitely post it here, because it would be a lot more lightweight. I think I'm going to stick to this method now as it gives me easy collision detection for other objects (as everything is attached to a physics world) and it seems to run at an acceptable pace.
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 » Thu Jul 07, 2011 9:45 pm

Impressive work :) I have been playing around with the same thing about, although mostly trying to work out the theory.

I have been programming games in several other languages although always on a beginner level. Still I remember having the exact same problem on Commodore 64 or maybe it was in Pascal, and it seems you had to make sure that you stop your sprite, before it moves into the object / tile that is supposed to block it. Otherwise you will get the problem with the sprite stuttering a lot when trying to walk into or through a wall.

I will play around with your example a bit, and see if I can find a way to make tiles, or a layer of tiles, act as a wall. I guess the problem you might have had as I expect to have, is finding out how to make sure the sprite does not make the move that puts it through the wall. Or at least doing so without being heavy on the CPU. I am thinking about making and if statement that checks where the sprite is going to be, before it allows the sprite to move. I am using digital controls at the moment, and have already experimented with having several sprites being controlled by it, and I can control several of them at the same time. Seems perfect as a test method for this.

I might be completely wrong though, but your problem just sounds very familiar. But using Box2D to make borders, also has the benefits of being pixel perfect, and that might come in very handy, as well as having Box2D ready for other game applications.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Thu Jul 07, 2011 11:39 pm

I have been testing out some different ideas. So far the problem seems to be that the sprite controls uses velocity, which makes it very hard to predict where it is going to be. I tried just reversing whatever it was doing when it was moving outside the screen or into somewhere with no tile, but the function:

"physicsHandler.setVelocity(pValueX * 100, pValueY * 100);"

is moving the sprite as it is used executed, updating the screen and everything. And it therefore seems that to make it possible to completely stop it from going outside the screen we would have to change something in AndEngine, or use Box2D, as you did. Solving the problem, since the physics engine supports pixel perfect collision detection.

Thanks for the script :) I want to see how well it works with a* pathing some day.

Oh, and was this the thing that inspired you?
http://methodin.tumblr.com/post/5988580 ... -beginners

Says something about making physics out of tiles / objects on the TMX map, but does not seem as complete as your method.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Fri Jul 08, 2011 10:07 am

Sounds like you're having the same problems I had. I decided to stick with the box2d model for a few reasons, firstly because it instantly stops the stuttering problem, secondly because you don't have to worry about the collisions you just let the physics engine do it for you, and thirdly you can change the elasticity and friction of the boxFixtureDef for the sprite / boxes so that instead of the player stopping dead when they have the slightest collision they can "slide" round it, if that's a desirable effect.

I also went with an object layer instead of tiles for collisions because in my tileset I have some tiles where only a small part should be "unwalkable", and with objects you can make them smaller than the tile so the player can walk over the sections of the tile that look like they should be able to.

I did see the tutorial you linked to, but I couldn't get it to work for my project and it doesn't give a complete example so when I finally got something working that involved using a controller to move a sprite round a TMX map with collisions I thought I'd post it as a complete example for others to use.
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 08, 2011 11:21 pm

Crundy wrote:I did see the tutorial you linked to, but I couldn't get it to work for my project and it doesn't give a complete example so when I finally got something working that involved using a controller to move a sprite round a TMX map with collisions I thought I'd post it as a complete example for others to use.


And it is very nice to see how you did it :) Especially since it also shows how you add several layers and other things.

And using Box2D physics makes sense, although I would like to know the alternative if there is one. I also like seeing your way of using controls.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

I use this with the digital control, to move the sprites around. Because it was how it was done in all the tutorials. I think your method might have more control over when and how the sprite moves, but I guess it might also be needed to use it with Box2D maybe?

I think I have run into the problem with not having the newest version of AndEngine. Thought I could work around it, but I cannot figure it out so far. I am excited to be moving into Box2D soon. Sure has much potential for games to use it. I hope it wont be too heavy on the performance though, but I have not even tried optimizing my code yet, my focus is mainly what works, and what does not. I am also trying to see if I can make it possible to switch between the digital and the analog control method. I am pretty sure it is possible, I think I just need merge the two methods some more somehow, to make them use the same graphics, and some way to switch the method of control.

I hope I can make a tutorial of my own soon. I am hoping to show of some simple game examples, and some explanations for them. But it is so hard to decide what to try out first :) So many possibilities with AndEngine.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Sun Jul 10, 2011 8:48 pm

sscape wrote:
rioneye wrote:Wow, thats impressive Crundy. Although I am having a problem compiling your code (Line 144). I am getting an error for updateChaseEntity, which says "The method updateChaseEntity() is undefined for the type BoundCamera." I'm not sure what could cause that, since I have everything imported.


Nicholas submitted some updates about 4 hours ago: "Camera.updateChaseEntity and made it publicly accessible, because it is often used to prevent the Camera from 'lagging' one frame behind the Entity it is chasing." so grab a new version of the code.


Hmmm, I am stuck here. I always just grabbed the andengineexamples, and used the andengine.jar from that. But it seems it is not new enough. Can someone share one that has the change this script needs, or at least link to somewhere that would explain how to compile it? I guess it can be done with Eclipse somehow, but I just never needed it before, and cannot find much about it.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby kblood » Mon Jul 11, 2011 8:56 pm

Now I got a newer andengine.jar, and with it, it says there is no errors in this script. But it crashes when I run it, saying there is errors in the source code. I have tried it with revision 285 and 286. 285 should be a default build, and 286 is using compressed textures or something like that. Both can run on their own, both are from yesterday, but none of them can execute this script without errors.

I have no idea what part of the script to look for the error in, or if there is any.

http://dionysus.dk/devstuff/downloads/c ... lision.zip

Here is the package with with the source, and all the libraries. When debugging all I can find out is how it says there is source code not found in line 2497. I am quite stuck with this. Maybe the changes you are using have not been compiled and uploaded to the andengine examples? Or are the most recent changes now in conflict with your code?
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby Crundy » Mon Jul 11, 2011 9:43 pm

Try commenting out line 144 for now using the andengine.jar from the examples repository. The map will stutter a bit when scrolling but at least you'll be able to see the general concept in action until the examples jar is updated.
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 » Mon Jul 11, 2011 11:50 pm

Crundy wrote:Try commenting out line 144 for now using the andengine.jar from the examples repository. The map will stutter a bit when scrolling but at least you'll be able to see the general concept in action until the examples jar is updated.


I found out what I was doing wrong :oops: I had made the Android Project for Android 1.6 because I was used to doing so, even though it has been 2.2 for a while. The newest andengine.jar in the examples repository does have the fix needed to use your script.

I still have some trouble making an andengine script that works as it should, without replacing the TMX example in the AndEngineExamples with your example. But it is working great :) I will try experimenting with it tomorrow.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Collision objects from TMX map

Postby rioneye » Wed Jul 13, 2011 4:47 am

After messing around with this I was wondering if anyone managed to get the fps higher than 20?
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

PreviousNext

Return to Tutorials

Who is online

Users browsing this forum: Dudi, Exabot [Bot] and 37 guests