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]. **/
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);
Hello everybody I'm trying to make a sprite fall in my game. I searched all over the web and I did this:
scene = new Scene();
main = new Sprite(sX, sY, mainTextureRegion);
main.setScale(1);
main.setFlippedHorizontal(true);
scene.attachChild(main);
mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
final Body body = PhysicsFactory.createBoxBody(mPhysicsWorld, main, BodyType.DynamicBody, objectFixtureDef);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(main, body, true, true));
final Vector2 gravity = new Vector2(0, 5f);
mPhysicsWorld.setGravity(gravity);
scene.registerUpdateHandler(new IUpdateHandler() {
#Override
public void onUpdate(float pSecondsElapsed) {
mPhysicsWorld.onUpdate(pSecondsElapsed);
}
#Override
public void reset() {}
});
But the when i launch the game the sprite doesn't fall !! Why ?? Please I'm desperate !!
Sprite doesn't use physics, but body does. You should use Physics connector (which is connecting your sprite with your body):
Set the body variable:
Body yourBody = PhysicsFactory.createBoxBody(mPhysicsWorld, main, BodyType.DynamicBody, objectFixtureDef);
and then use this physics connector:
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(main, yourBody, true, true));
Sorry for my English.
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'm developing a simple game by andengine.
I have 10 balls which are moving randomly on screen.i'm importing the balls as picture in sprites.if they move at the same coordinate , they pass though their own insides.but i want: if they move at the same coodirnates ,they should change their directions.so they cannot pass through their insides.how can i do that?
private Runnable mStartCircle = new Runnable() {
public void run() {
int i = circleNumber++;
Scene scene = Level1Activity.this.mEngine.getScene();
float startY = -64.0f;
float startX = randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
float a= randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
circles[i] = new Sprite(startX, startY, textRegCircle[i]);
circles[i].registerEntityModifier(
(IEntityModifier) new SequenceEntityModifier (
new MoveModifier(10.0f, circles[i].getX(), a,
circles[i].getY(),CAMERA_HEIGHT+64.0f)));
}
scene.getLastChild().attachChild(circles[i]);
if (circleNumber < 10){
mHandler.postDelayed(mStartCircle,1000);
}
}
};
Each object(ball) requires a bounding box, or in your case a bounding circle, which is equal to the size of your sprite.
When the game updates and any balls position changes, you have to test for collisions.
Circle to circle collision testing is the simplest type to do.
if distance between (ball1.pos + ball2.pos) is less than (ball1.radius + ball2.radius) = collision.
You then handle the collision by reversing the velocities or calculating new momentums or something. (You also need to move the objects apart so they are no longer colliding)
Just apply a physical connector between balls:
so it will collide and bounce back.
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f);
final Body ballBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, circles[i],BodyType.DynamicBody, boxFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circles[i], ballBody, true, true));
this.mScene.attachChild(circles[i]);