Collision Filtering with Andengine - How to? - android

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:)

Related

What's the BEST WAY to check (and do something) for a touch event in libgdx?

I have a newbie question. I just started learning about libgdx and I'm a bit confused. I read the documentation/wiki, I followed some tutorials like the one from gamefromscratch and others and I still have a question.
What's the best way to check and do something for a touch/tap event?
I'm using Scenes and Actors and I found at least 4 ways (till now) of interacting with an Actor, let's say:
1) myActor.addListener(new ClickListener(){...});
2) myActor.setTouchable(Touchable.enabled); and putting the code in the act() method
3) verifying Gdx.input.isTouched() in the render() method
4) overriding touchDown, touchUp methods
Any help with some details and suggestions when to use one over the other, or what's the difference between them would be very appreciated.
Thanks.
I've always been using the first method and I think from an OOP viewpoint, it's the "best" way to do it.
The second approach will not work. Whether you set an Actor to be touchable or not, Actor.act(float) will still be called whenever you do stage.act(float). That means you would execute your code in every frame.
Gdx.input.isTouched() will only tell you that a touch event has happened anywhere on the screen. It would not be a good idea to try to find out which actor has been hit by that touch, as they are already able to determine that themselves (Actor.hit()).
I'm not sure where you'd override touchDown and touchUp. Actors don't have these methods, so I'm assuming you are talking about a standard InputProcessor. In this case you will have the same problem like with your 3rd approach.
So adding a ClickListener to the actors you want to monitor for these kind of events is probably the best way to go.

If statement in App Inventor

I'm designing a spaceship game in App Inventor. I have a label (lblScore) update when the ship is hit each time. When the ship is hit 3 times, I want the code inside that to execute yet it doesn't work. I've tried multiple variations of this, such as setting it to lblScore.Text instead. Any idea's on how I can address the issue?
Is the lblscore a label?
If it is all you need to do is have a collision block saying whenever the spaceship gets hit, set lblscore = lblscore + 1
This should fix your issue but I would like to see all of your blocks
Do you increment your lblScore in the Ship.CollidedWith event?
If yes, you should move your if statement there, but instead of using the lblScore component as currently, you should better use the lblScore.Text property instead.
Probably it would help us to help you, if you provide a screenshot of your Ship.CollidedWith event...

Detect collision between two bodies in Box2d and libgdx(android)

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...

Google direction and voice navigation

I have been working on a android project.
Then I came up with 2 question.
Q1. how to implement navigation drive ?
My logic and some work
- I am be able to draw path between 2 address. And my thought is that, use the onLocationChanged(current) method then call https://maps.googleapis.com/maps/api/directions/output?parameters with the current location and destination which through some method to draw the path on the map.
Upon every onLocationChanged() method call, I redraw the path on the map again.
" Is it how we would do it for navigation ? "
Q2. how to implement voice navigation to work with Q1 ?
- Did some research, can't find anything that seems clearly helpful. All I know its that, in the return JSON from the /api/directions, there are direction instruction in it.
" Do I use it for voice from the return JSON ? Or there is a better way ? "
Would be very helpful with some link or example in details.
Thanks in adavnce
Here is what I know, hope it helps you out.
Regarding the first question:
After retrieving the directions and the necessary data, you have to draw the direction once and only once! yes, you have to use the onLocationChanged() but not to redraw the whole thing again.. if you notice in most of the navigation application they still keep the main route, they don't remove the passed parts... but you have to use onLocationChanged() to check if the user is out of the drawn path (by maybe 100m) so you have to re-calculate and redraw it again... redraw the path every time the user move is a costly operation it is better to be avoided...
For the second question:
As you said, the data retrieved already has the navigation commands.. so what you have to do is create a class to map the command with the voice.. and if you notice within the legs -> steps tags, there is a start and ending coordinates for each sub-path, so you can use these data to calculate the distance between them on each 200m say the command that "how far the user is to turn left" for example.
Hope this gives you a general idea of how it works. Good luck and happy programming.

How do you check to see if a sprite collides with any other object (Android AndEngine)?

When the user touches the screen, a sprite is created and is able to be dragged around until the user lifts his finger. At this point, a physics body is then attached to the sprite so that it will fall to the bottom of the screen. However, when the sprite collides with another sprite/body I need it to stop being able to move. How do I check for a collision between the sprite created and any other body/sprite (the walls and any others created)?
You are looking for information on Collision Detection.
It's relatively simple to implement depending on your situation.
The simplest way would be to hold all your objects in an array/list and every time you move it you need to calculate its current position + area vs every other object. This will be very inefficient, but if you have a small number of objects you are probably ok.
Here is the first collision detection tutorial I did. It is for C, but the concepts are what you are looking for.
There is a rather simple collision detection implemented in AndEngine. See method collidesWith(IShape pOtherShape) Someone improved it so that it works with transparent areas, search for pixel perfect collision detection.
You can also make use of the physics engine, see sensor.
Although calculating the area would have worked, I was dealing with so many collisions that I needed a better way. It took quite a bit of time, but I finally figured out how to use a ContactListener (which was really what I was getting after. Why do these kinds of questions always get down voted when all someone really has to do is explain a bit to a novice???) Anyway, for reference to anyone else that might see this question, in your ContactListener, you can use code like this:
public void beginContact(Contact contact) {
if(fillerNum>-1 && contact.getFixtureA().getBody().getUserData()!=null && contact.getFixtureB().getBody().getUserData()!=null){
String x1 = (String) contact.getFixtureA().getBody().getUserData();
String x2 = (String) contact.getFixtureB().getBody().getUserData();
if((x1.equals("fill")&&!x2.equals("dest"))||(x2.equals("fill"))&&!x1.equals("dest")){ //If a filler hits a stationary
//Log.e("Contact",x1 + " " + x2);
createStationaryFiller();
}
if((x1.equals("dest")&&x2.equals("fill"))||(x1.equals("fill")&&x2.equals("dest"))){ //If a destroyer hits a filler
destroyFiller();
}
}
}
Set growing bodies to have the user data "fill" and stationary ones to have the user data "stat". I also had some other bodies with the user data "dest". This way, you can see which two collides and react accordingly. Message me if you don't understand. I have no problem helping people new to this kind of thing.

Categories

Resources