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);
Related
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
}
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 use Box2D in my game, but I would like to have a nicely looking spring - wall to push the player when touched. It would look like this (in 3 frames):
Question: how to implement it? Can I attach a wall effect to an animated sprite?
The answer is: Prismatic Joint. I divided the image into 2 parts: static and dynamic (the moving bar). The below code is for creation of a prismatic joint in orientation like in the image in my question:
//prismatic joint
final Sprite springFrameT = new Sprite(pX, pY, mSpringFrameTRegion, getVertexBufferObjectManager());
final Sprite springBarT = new Sprite(pX, pY + mSpringFrameTRegion.getHeight()-mSpringBarTRegion.getHeight(),
mSpringBarTRegion, getVertexBufferObjectManager());
mMainScene.attachChild(springFrameT);
mMainScene.attachChild(springBarT);
mMapSprites.add(springFrameT);
mMapSprites.add(springBarT);
final Body springFrameBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springFrameT, BodyType.StaticBody, FIXTURE_DEF);
final Body springBarBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springBarT, BodyType.DynamicBody, SPRING_FIXTURE_DEF);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springFrameT, springFrameBody, false, false));
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springBarT, springBarBody, true, true));
final PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
prismaticJointDef.initialize(springFrameBody, springBarBody, springFrameBody.getWorldCenter(), // new Vector2(springFrameT.getWidth(), springFrameT.getHeight()/2),
new Vector2(0, 1.0f));
prismaticJointDef.lowerTranslation = -0.5f;
prismaticJointDef.upperTranslation = 0.5f;
prismaticJointDef.enableLimit = true;
prismaticJointDef.enableMotor = true;
prismaticJointDef.maxMotorForce = 100.0f;
prismaticJointDef.motorSpeed = 100000f;
prismaticJointDef.collideConnected = false;
this.mPhysicsWorld.createJoint(prismaticJointDef);
I am new in AndEngine. I use animatedSprite and Sprite Object. I want to move Sprite on the basis of accelerometer but i dont want to move animatedSprite also. I want physicsworld implementation
private void addBottomFace(final float pX, final float pY) {
myBottomSticFace = new Sprite(pX, pY, myBottomStickTextureRegion);
myBottomStickBody = PhysicsFactory.createBoxBody(this.myPhysicsWorld, myBottomSticFace, BodyType.DynamicBody, myFixtureDef);
this.myScene.attachChild(myBottomSticFace);
this.myPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(myBottomSticFace, myBottomStickBody, true, true));
}
#Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
myBottomSticFace.setPosition(myBottomSticFace,getX() + pAccelerometerData.getX(), myBottomSticFace,getY());
Vector2Pool.recycle(gravity);
}
With this code mBottomSticFace will move according to accelerometer but body does not moves.
You should create two different bodies and attach it to your sprites.
The Body you want to be moved should be set as BodyType.DynamicBody, and the other one should be BodyType.StaticBody.
To see accelerometer and body examples, check this:
http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/PhysicsExample.java
i have problem with gravity. I make map of my game with add some elements like boxes, brick etc. using this code:
private void addFace2(final Scene pScene, final float pX, final float pY, final int pWidth, final int pHeight, final String pType, final String Gbodytype) {
final Sprite face;
final Body body2;
BodyType bodytype;
face = new Sprite(pX, pY, pWidth, pHeight, this.mBoxTextureRegion);
bodytype = BodyType.DynamicBody;
body2 = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, bodytype, boxFixtureDef);
pScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body2, true, true));
boxy.add(face);
}
Everything is "ok" but the elements are bouncing!? I think the problem lies is in the this.mScene.registerUpdateHandler(this.mPhysicsWorld);.
I want to ask you how to stop bouncing not by removing the gravity?
http://s3.ifotos.pl/img/fail_hsrpxhe.png
Sorry for my english, I work on it..
Check your boxFixtureDef and the FixtureDef for whatever your using for the ground.
new FixtureDef(Density,Elasticity,Friction)
/** The friction coefficient, usually in the range [0,1]. **/