I am new to libgdx and Box2d engine and I'm developing a game using same .I want to detect collision between two bodies to perform some function.But I'm not aware about the optimum way to do that and also want get point of collision.Kindly provide some suggestion with code.I have already implemented ContactListener but to no avail.
I'm using this code as a reference.
Thanks
You already did it the right way to create and set a ContactListener... (for a general setup, the libgdx wiki is great: https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners)
If you now want to handle the specific contacts, you for exmaple need to add some implementation in the beginContact(); method of your listener. The beginContact(); method contains a Contact instance, which holds all you need:
FixtureA - the first fixture of a contact
FixtureB - the fixture, which FixtureA has collided with
WorldManifold - an object that holds the collision points etc
Through the fixtures you can access the bodies and the actors which you are drawing. The connection to your Actor can be done through the body.setUserData(actor); method.
Now you need to decide on how to find out the correct collisions. You can either work with sensors, which are box2d fixtures that just act as a sensor. That means that when an object collides with a sensor, it won't bounce, but fall through it instead. But you then are able to detect this contact within the listener.
Also, it might be a good idea to add some kind of GameObjectType to your actors. Imagine you create a jumping game where the player jumps from platform to platform with water below. Then you would create your actors with types like PLAYER, WATER, PLATFORM ... through the getUserData() method of the box2d bodies yu can now access the Actors and compare their types.
E.g. when an Actor of type PLAYER collides with one of type WATER, he will drown...
Hope it helps...
Related
I have a fairly basic question that I cannot for the life of me find the answer to online (most definitely due to not really knowing what I am looking for).
Suppose I have multiple (for the most part static) objects that are stored inside one VBO and drawn to the screen. Each object will have images and text/external data associated with it. I need to be able to navigate this "map" of objects and on-tap, access the corresponding information.
My question is, what is the best practice when it comes to storing this corresponding data and linking it to its respective drawing? I figured you create a "parallel" array of custom objects that each references its drawing and holds all the data... Although it seems quite elementary and thought there might be a better way. Considering also that there will potentially be thousands of these objects on the "map".
You can Use model class and put all common data intro one entity than use extends. You can use yours custom object who will consider whole VBO or opengles program data. About identity make ID for each elementary object or "draws".
-I understand your problematic. In opengl/es procedural programming is actually on scene.
Remember you are still in android envelopment you can use any java/android methodology.
-When you say "tap" did you mean click? If you so than see "Raycast" thema.
-This is interest file . It is JS not android but you can use same logic methods to make your object based app.
https://github.com/zlatnaspirala/visualjs/blob/master/project/project_instance_webgl2/lib/matrix-world.js
You can see lines like this :
App.scene[squareObject.name] = squareObject;
I have a global object App.scene . I put here all object buffer data. It is a key access but works like array.
I wanna say your idea about arrays is good. But not in parallel order. Procedural part works with no problem you need draw function for each element entity.
Look at draw methods :
https://github.com/zlatnaspirala/visualjs/blob/master/project/project_instance_webgl2/lib/matrix-draws.js
For example one of my draw entity is App.operation.draws.cube function.
I use this method to draw any cube but each cube is uniq object with uniq data inside.
I'm developing an Andengine-based game and I'm stucked in a collision issue. I have several sprite classes with the respective physic body and I want to ensure that collision only happens with specific bodies. Let's say that I have in my scene bodies A B C and D at the same time but I want to configure collision for only two of them, for example A and C. How do I do that?
Thank you very much for your patience in advance! I'll appriciate any tip!
First of all you have to set proper userData for every type of body:
bodyA.setUserData("bodyA");
bodyC.setUserData("bodyC");
then you can disable collisision like this:
List<Fixture> fixtureList = bodyA.getFixtureList();
for(Fixture fixture : fixtureList){
if(fixture.getUserData().equals("bodyC")
fixture.setSensor(true);
}
It takes all fixture connected to bodyA and iterate through them. If any of fixture has userData set to "bodyC" it will disable collision detection for it. You can turn it on by setting false instead of true (true means 'disable collision')
Probably it's not the best way to do it but it works.
PS I can't test it now so probably u might have to change someting (add !=null or something) But i think you got the point and you will be able to do it:)
So I am currently designing a game where the main idea is that you choose your attack and then an animation plays out based on the attack that you picked (think pokemon). The game is turn-based as well.
My question is whether scene2d would be easier to use than implementing a custom solution for handling the animation part of the game. From what I've read (and I've found it difficult to find good information on scene2d), it sounds like scene2d would make designing the UI for the buttons/menu extremely easy, but I'm not sure how I can roll that into making the actors move. Is it as simple as handling the touch event on the button and calling the corresponding actor's action method based on the player's choice?
In what I have in mind, the actors never actually move (except during their animation and they don't move across the screen, they merely go through their animations in place). During the animation, there will also be particle effects (the attack) which, if using scene2d, would need to be their own actors. Would the synchronization of the actors and the attack be difficult to produce?
Actors do movee..
actor.addAction(Actions.moveTo(posX, posY, 5)));
by this your actor moves to posX, posY and 5 is the time duration ..
using scene2d would be a good idea in my opinion..
Scene2D would be better since you would have to manually implement the listener for actions such as Click when you use Sprite whereas Scene2D provides the functions to set Listeners.
you may already know this, but let me try to answer your question:
Scene2d has a very handy action system, which basically allows the following:
do any of the provided actions
fine tune them with the many provided interpolations
make new actions with Actions.run()
chain existing actions forming sequences
Like this:
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.Action;
Action a1 = Actions.sequence(Actions.fadeOut(0), Actions.fadeIn(3, Interpolation.bounce));
Action a2 = Actions.moveTo(100, 200, 3, Interpolation.pow2Out);
Runnable r = new Runnable() {
#Override
public void run() {
setColor(1, 0, 0, 1);
System.out.println("now I'm a red actor");
}
};
And then combine then, for example like this:
addAction(Actions.sequence(Actions.parallel(a1, a2), Actions.run(r)));
This allows you to profit from scene2d's built-in sequencer saving you the half of the work, at least. So, answering your question, I think it is very possible indeed to easily implement fixed as well as reactive/randomized animations using this system. It also allows you to easily encapsulate simpler actions into complexer ones, and has the following advantages:
Very readable and maintainable code
Tradeoff CPU/Mem: much more memory-efficient than storing plain sequences or even videos
Reactivity: this way you can program your animations to be slightly different each time
On the other hand, this developing system can become very time consuming if you constantly want "uncovered" things, like the following:
Implement time-based actions yourself that aren't built-in (like camera travelling)
Make your own interpolations if the built-in ones don't fit your goals
Work with many little granular elements (for that sake I would use the ParticleEditor).
Which I don't think is your case. As a last remark, you should take a look at the spine animation engine. I don't use it myself but may be useful for what you have in mind.
Is there any way to use the native flash TransformGestureEvent.GESTURE_ZOOM with non-"flash InteractiveObject"? For example, is there any way to add this event to stage? I need to zoom the world that was build by Axel Game Library, but it doesn't support this kind of events. Should i use some invisible sprite to put all axgl world to it? Or is there any way to write my own gesture_zoom event that would apply to the stage?
I would like to use Ax.zoom axgl property to zoom the world by this event, so i need to only catch it.
If the non interactive object is a DisplayObject, you could consider add it as a child of an InteractiveObject. You can then capture guesture event from the InteractiveObject, and apply it to your target object.
If the non interactive object is not a DisplayObject, you can create an interactive object that represents the display region of your non interactive object, then translate any of the guesture event from the InteractiveObject to your target object.
Oh that's kinda late answer, but still... Try to use my gesture recognition library Gestouch — it can work with non-"flash InteractiveObject" and can do bunch of other things.
I'm working on an arcade shoot-em-up game for Android similar to Ikaruga. The problem I'm facing is that it's proving quite difficult to robustly create move and shoot patterns for the enemies. At the moment I've created two abstract classes EnemyShip and FlightPath from which each different enemy and move pattern derive from respectively. When the World
is created it instantiates a LevelManager which stores level info in the form of:
waveInfos.add(new WaveInfo(3, 3f)); // new WaveInfo(NumberOfGroups, spawn interval)
enemyGroups.add(new EnemyGroup(8, EnemyGroup.TYPE_SCOUT_SHIP, EnemyGroup.F_PATH_INVADERS));
enemyGroups.add(new EnemyGroup(1, EnemyGroup.TYPE_QUAD_SPHERE, EnemyGroup.F_PATH_QUAD_SPHERE_L, World.BLACK));
enemyGroups.add(new EnemyGroup(8, EnemyGroup.TYPE_SCOUT_SHIP, EnemyGroup.F_PATH_INVADERS));
// new EnemyGroup(NumberOfEnemies, EnemyType, FlightPathType)
// new EnemyGroup(NumberOfEnemies, EnemyType, FlightPathType, ShipColour)
waveInfos.add(new WaveInfo(2, 0.33f));
enemyGroups.add(new EnemyGroup(1, EnemyGroup.TYPE_QUAD_SPHERE, EnemyGroup.F_PATH_QUAD_SPHERE_L, World.WHITE));
enemyGroups.add(new EnemyGroup(1, EnemyGroup.TYPE_QUAD_SPHERE, EnemyGroup.F_PATH_QUAD_SPHERE_R, World.WHITE));
totalWaves = waveInfos.size();
The levels are split into waves of groups of enemies and right now the EnemyGroup class takes care instantiating, adding the specified FlightPath to the newly created enemy and passing that enemy to the ArrayList in LevelManager for storage until spawned into the world at the time needed.
Once spawned the FlightPath componant takes over and starts giving instructions based on it's own stateTime and since each FlightPath has a reference field to its EnemyShip owner it can access the ship's functions and members it's controlling.
The EnemyShip class has a few functions for easy instruction such as moveTo(float x, float y, float duration) and shoot() but even with these the FlightPath derivatives are diffcult to make especially when I want different enemies in the same group to have slightly different paths and slightly different time arrivals.
I created a few fields in the FlightPath to keep track of keyFrames:
public int currentKeyFrame = 0;
public int totalKeyFrames;
public KeyFrame[] keyFrames; // Stores duration of instruction to be done, the spreadTime, totalFrameTime and enemyIntervalTime
public int shipNumber; // Stores which ship out of group this FlightPath is attached to
public int totalShips; // Stores total number of ships in this EnemyShip's group
public float stateTime = 0;
KeyFrame.spreadTime is my attempt to control the time between the first enemy in group to begin moving/shooting and the last.
KeyFrame.totalFrameTime = KeyFrame.duration + KeyFrame.spreadTime
KeyFrame.enemyIntervalTime = KeyFrame.spreadTime / Number of enemies in this group
While this setup works great for very simple linear movement, it feels quite cumbersome.
Thanks for reading this far. My question is how do I implement a more streamlined pattern control which would allow for complex movement without hordes of if() statements to check what other enemies in the group are doing and the like.
I hope I've provided enough information for you to understand how the enemies are handled. I'll provide any source code to anyone interested. Thanks in advance for any light you can shed on the subject.
Marios Kalogerou
EDIT: I found a page which very much describes the kind of system which would be perfect for what I want but I'm unsure how to correctly implement it with regards to overall group keyFrames
http://www.yaldex.com/games-programming/0672323699_ch12lev1sec3.html
FlightPath should not control any objects. It's a path, not a manager. However, it should be able to give coordinates given any keyframe or time. For example: flightPath.getX(1200) -> where should I be in the X-coordinate at 1200ms?
Each EnemyShip should maintain a possession of a FlightPath instance. EnemyShip checks where it should be in the path every frame.
EnemyGroup then controls the spawning of each EnemyShip. If you have 8 EnemyShips in one EnemyGroup, all possess the same FlightPath type, then you can imagine that EnemyGroup would spawn each ship around 500ms apart to create the wave.
Finally, you translate all the EnemyShip coordinates relative to the world/screen coordinate, which traditionally moves slowly in the vertical direction.
There are different approaches:
You can add random intervals before shooting, and set slightly random arrival times. Like currentEnemyArrivalTime += (X - rand(2*X)).
You can control movement of group of enemies. Each enemy in the group tries to maintain it's position relative to the center of the group.
For really complex patterns may be better to develop some simple scripting engine. It can be very simple (like array of coefficients for spline), or something more complex. I believe, in such games behavior is done by scripts.