There are two images.
//1.
player = new Sprite(300, 670, this.mChaTextureRegion, this.getVertexBufferObjectManager());
//2.
body[i] = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball[i], BodyType.DynamicBody, FIXTURE_DEF);
ball[i].setUserData(body[i]);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball[i], body[i], true, true));
this.mScene.registerTouchArea(ball[i]);
this.mScene.attachChild(ball[i]);
ball is defined TouchArea, but player is not.
player is disappeared, when two images(player and ball) are overlapped and I touch the player.
The player should not disappeared.
How can I solve this problem??
Hopefully this could help: use
final ILayer layer = mEngine.getScene().getLayer(YOUR_LAYER);
layer.addEntity(ball);
mPhysicsWorld.registerPhysicsConnector(ball.getPhysicsConnector());
Don't register each ball, but use onSceneTouchEvent
then test for touch on ball:
for (Ball ball : balls) {
if (ball.contains(eventx, eventy)) {
//touching the ball
}
Related
I'm currently using AndEngine, to do a little box game with faces in it, so that the player spawns faces when touching the screen. So here I already declared the variable "onch" as a double and I made it a random number between 1 and 4 so that each time addFace is called, it generates a new number and so a new face. However I'm getting always the last face. I'm getting only this :
face = new AnimatedSprite(pX, pY, this.mort, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
Here is the full code :
private void addFace(final float pX, final float pY) {
onch = Math.floor((Math.random()*4)+1);
final AnimatedSprite face;
final Body body;
if(onch == 4) {
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (onch == 3) {
face = new AnimatedSprite(pX, pY, this.noel, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (onch == 2) {
face = new AnimatedSprite(pX, pY, this.sournois, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else {
face = new AnimatedSprite(pX, pY, this.mort, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
}
face.animate(200);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
}
Why do I only receive the one value?
i think it is because you set your variables with the
final
modifier, once you set an instance of a final variable it cannot be changed, so your random code works fine but since the variables have the final modifier, once they are set the first time they are stuck like that, you cannot change them
There are some problems with the code.
This line body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); appears in the code four times. You could take it out of your if statements because it's the same in all four.
As JRowan said, final should not be use in this case.
Now, if you are sure that the face is exactly identical at the end of this method, the problem is in one of these calls: AnimatedSprite()constructor might give you the same face no matter what arguments you put in, or animate() method of AnimatedSprite class changes the face and makes it look the same in the end.
This appears incorrect:
onch = Math.floor((Math.random()*4)+1);
Try:
onch = Math.floor((Math.random()%4)+1);
I was trying to change the velocity of a Physics Body that is attached to a rectangle with the movement of the accelerometer. I cannot get the body to change velocity, is it a permanent property once it is set?
this is in my populateScene:
rect = new Rectangle(220, -200, 24, 24, this.getVertexBufferObjectManager());
rect.setColor(Color.GREEN);
mScene.attachChild(rect);
ball = PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.DynamicBody, droppingBoxDef);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
rect, ball));
and this is where I try to change the velocity:
#Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
int accellerometerSpeedX = (int)pAccelerationData.getX();
// accellerometerSpeedY = (int)pAccelerometerData.getY();
//Log.v("Accelerometer X Y Z: ", ""+pAccelerationData);
ball.setLinearVelocity(accellerometerSpeedX, 0);
}
Without the second portion above the rectangle loads fine and has its physics body working correctly. It seems to disappear when I try to use:
ball.setLinearVelocity.
The Body object is a global variable in the class so it can be referenced in both methods. I have tried using a update handler inside of Populatescene and setting ball.setLinearVelocity in there, however that gave the same results.
Essentially my question is: Can the velocity of a Body be changed after it has been connected to the physics world?
Typicly in Box2D you do not setVelocities, but rather apply impulses or forces to a body to cause it to accelerate or decelerate.
For what you are describing above, you should not be using setLinearVelocity. Try using
ball.applyForce(new Vector2(accellerometerSpeedX, 0), ball.getWorldCenter());
or
boxBody.applyAngularImpulse(new Vector2(accellerometerSpeedX, 0));
I am trying to develop a live wallpaper using andengine gles2 anchor center , with some physics.But when i add a physics object it was moving upwards.instead of moving downward due to gravity
what are the mistakes i am making
please help me to sort out the issue
Here is my code
FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f,
0.5f);
mPhysicsWorld = new PhysicsWorld(new Vector2(0,
SensorManager.GRAVITY_EARTH), false);
final AnimatedSprite animatedSprite;
animatedSprite = new AnimatedSprite(500, 250,
this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, animatedSprite,
BodyType.DynamicBody, FIXTURE_DEF);
scene.attachChild(animatedSprite);
animatedSprite.animate(200);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
animatedSprite, body, true, true));
Just multiply SensorManager.GRAVITY_EARTH by -1.
Setting negative gravity did not respond properly to sensor data. By adding acceleration to the sensor data in the overridden method **onAccelerationChanged()**, could make the bject falling down with sensor flat.
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
/* Add constant value for vertical gravity*/
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY() + 4.0);
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
This will make your phy world in real gravity experience and you will get objects influenced by sensor data properly
I managed to load a tmx map now I would like to create the obstacle that the sprite can not move, I recovered the obstacle like this :
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener() {
#Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
/* We are going to count the tiles that have the property "cactus=true" set. */
if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
//TMXTiledMapExample.this.mCactusCount++;
//coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
}
}
});
How do I handle collisions with obstacles so as to prevent the player from walking through the obstacle (i.e., like a wall)?
I believe what you're asking is how do you implement collision handling. To be clear: Collision detection is the step where you determine that something is colliding(overlapping) with something else. Collision handling is where you, say, move one of those things such that it is no longer overlapping. In this case, I'm assuming we're past the collision detection and on to collision handling because you're in a method called "onTMXTileWithPropertiesCreated," which I'm guessing means the player is on such a tile. So here's the idea, put very simply:
When, due to the movement of the player (or some other sprite) you detect that the sprite is colliding with a sprite that you would like to be impassable -- "real" in your terms, you're going to want to move the sprite back the distance that would prevent it from overlapping.
Doing this with rectangles is very simple. Doing it with other shapes gets a little more complicated. Because you're working with a TMX tile map, rectangles will probably work for now. Here's a basic example with rectangles.
public boolean adjustForObstacle(Rect obstacle) {
if (!obstacle.intersect(this.getCollisionRect())) return false;
// There's an intersection. We need to adjust now.
// Due to the way intersect() works, obstacle now represents the
// intersection rectangle.
if (obstacle.width() < obstacle.height()) {
// The intersection is smaller left/right so we'll push accordingly.
if (this.getCollisionRect().left < obstacle.left) {
// push left until clear.
this.setX(this.getX() - obstacle.width());
} else {
// push right until clear.
this.setX(this.getX() + obstacle.width());
}
} else {
if (this.getCollisionRect().top < obstacle.top) {
// push up until clear.
this.setY(this.getY() - obstacle.height());
} else {
// push down until clear.
this.setY(this.getY() + obstacle.height());
}
}
return true;
}
What this is doing is calculating the overlapping rectangle and moving the sprite along the smallest dimension of overlap by the amount that will make it no longer overlap. Since you're using AndEngine, you can make use of the collidesWith() method in IShape, which detects collisions more elegantly than the above approach.
since I use this
if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
//TMXTiledMapExample.this.mCactusCount++;
//coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
//initRacetrackBorders2();
// This is our "wall" layer. Create the boxes from it
final Rectangle rect = new Rectangle(pTMXTile.getTileX()+10, pTMXTile.getTileY(),14, 14);
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
Have fun !
I'm trying to place some clickable sprites on the HUD of my ZoomCamera.
The sprites detect the touch event just fine, but only if the ZoomCamera is placed in its original position.
If I zoom in, or pan the camera, it seems like the touch areas remain in their area relative to the scene, and not to the camera. Therefore the action only works if I click on the area where the sprites existed in the original state.
This is most of the relevant code -
FloorSelectorButton firstButton = new FloorSelectorButton(0, 100, 80, 80,
this.getVertexBufferObjectManager(), 3, this);
FloorSelectorButton secondButton = new FloorSelectorButton(0, 180, 80, 80,
this.getVertexBufferObjectManager(), 2, this);
FloorSelectorButton thirdButton = new FloorSelectorButton(0, 260, 80, 80,
this.getVertexBufferObjectManager(), 1, this);
mScene = new Scene();
mScene.setOnAreaTouchTraversalFrontToBack();
HUD hud = new HUD();
mZoomCamera.setHUD(hud);
hud.attachChild(firstButton);
hud.attachChild(secondButton);
hud.attachChild(thirdButton);
hud.registerTouchArea(firstButton);
hud.registerTouchArea(secondButton);
hud.registerTouchArea(thirdButton);
this.mScrollDetector = new SurfaceScrollDetector(this);
this.mPinchZoomDetector = new PinchZoomDetector(this);
this.mScene.setOnSceneTouchListener(this);
this.mScene.setTouchAreaBindingOnActionDownEnabled(true);
return mScene;
FloorSelectorButton is just a class that extends Sprite and implements the onAreaTouch method.
I hope you guys could help my out!
Thanks!
Apparently my problem was really dumb. It seems that for some reason the APK wasn't updated on the phone and it stayed with the old code (because of this - http://groups.google.com/group/android-developers/browse_thread/thread/2eb92316b474fa00) where I set the registerTouchArea on the scene instead of the HUD.
So now it's fixed! :)
Thanks anyways!