Box2d body setLinearImpulse() function stability - android

I'm developing a game by Andengine for Android.
Game: There is a body(let's say small box). And I want: the player collision with it, player will jump. It is jumping but not jumping the same height(in my code 100px) in every collision! My codes are here:
if (footFake.collidesWith(this))
{
player.getBody().applyLinearImpulse(new Vector2(0, 100/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT), player.getBody().getWorldCenter());
}
And the box body features:
final FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0);
this.body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this, BodyType.StaticBody, fixtureDef);
this.body.setUserData("jumpBox");
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this, this.body, true, true));
And player:
final FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.1f);
this.body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this, BodyType.DynamicBody, fixtureDef);

Applying an impulse will not always give the same height jump, because the existing velocity of the body may be different. You could use SetLinearVelocity to set the vertical velocity of the body to make sure that the starting velocity is constant every time.
Of course like Singhak has said, you should also make sure that there are no other influences (like collisions) on the body that will mess up the velocity that you want.

Instead of applying impulse you can transform body gradually upto height you want.
And in you case body is not jumping at same height because when body collide many force applied on it. some time value of forces is same and some time it is different.

Related

Box2d in Andengine - attaching child bodies - is it possible

I have a problem with Andengine Box2d Extension.
I have 2 rectangles: base and fuelStation.
fuelStation is a child of base. When I'm rotating base with setTransform method, fuelStation sprite is rotating too, but body remains in the same position.
base = new Rectangle(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2 - 200, 200, 200, vbom);
fuelStation = new Rectangle(base.getWidth() / 2, 0, 500, 10, vbom);
this.attachChild(base);
base.attachChild(fuelStation);
final FixtureDef objectFixtureDef1 = PhysicsFactory.createFixtureDef(1, 0.0f, 0.5f);
final FixtureDef objectFixtureDef2 = PhysicsFactory.createFixtureDef(1, 0.0f, 0.5f);
baseBody = PhysicsFactory.createBoxBody(physicsWorld, base, BodyType.StaticBody, objectFixtureDef1);
baseBody.setUserData("base");
fuelStationBody = PhysicsFactory.createBoxBody(physicsWorld, fuelStation, BodyType.KinematicBody, objectFixtureDef2);
fuelStationBody.setUserData("station");
physicsWorld.registerPhysicsConnector(new PhysicsConnector(base, baseBody, true, true));
// physicsWorld.registerPhysicsConnector(new PhysicsConnector(fuelStation, fuelStationBody,
// true, true));
When I remove comment from the last line - sprite position changes but still not working properly (body stands still).
How to connect properly these 2 bodies?
You have to keep in mind that all the transformations that you do to the entities (sprites) will only affect the "visual" content, in other words, will never affect the physic bodies. If you want to affect the entities and the bodies you will only achieve that by applying forces to the physic bodies and, because they are connected with a PhysicsConnector, those transformations will affect both the physic body and the the sprite ("visual body").
So, having the fuelStating as a child of base will affect nothing. To affect two bodies you need a "connection" between them and you can achieve that by creating a joint that will connect the two bodies: http://www.iforce2d.net/b2dtut/joints-overview
Hope it helps.

set angular velocity not working in andengine

I am trying to do flappy bird like game in andengine. I achieved everything and everything worked out but the rotation is not working. setangularvelocity is not working
my code is as follows
onscenetouch I am giving linear velocity as follows
body.setLinearVelocity(new Vector2(2, 2));
float omegaFactor = (float) -1.0;
body.setAngularVelocity((float) ( (Math.PI) * omegaFactor) );
please suggest me what I am doing wrong.
The physics connector has a boolean paramter which states the update of body rotation.
Unfortunately it was false in my game. I changed it to true and it worked.
physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
to
physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, true));

Box2d fixture definition parameters

I'm develepoing a game via Andengine for Android. In my game, there a player and a moving box. This box moving in loop from (0,0) to (200,0) points.The problem is: when this box is changing its moving side, the player, which is on this box at the moment, is sliding. I don't want sliding! Here is the codes:
Player:
final FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.1f);
this.body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this, BodyType.DynamicBody, fixtureDef);
Box:
final FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(1, 0.1f, 0.5f);
this.body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this, BodyType.KinematicBody, fixtureDef);
I don't think that only changing fixture definition help with this problem.
You can use prismatic joint and join box and player while player standing on box. When player want move just destroy joint, and again create join when player stop moving on box.

Andengine Box2D physics body is not rotating when increase weight

Andengine Box2D physics body is not rotating when increase weight otherwise its perfectly rotating.I am making a game in which the player can throw the bomb, when the player throws the bomb with bombsBody's natural weight its rotating perfectly but when i increase the weight of bomb the bomb does not rotate.i m stuck here..plz help.
Thank you.
if (bomb)
{
mScene.detachChild(target);
target = null;
bombFire = new Sprite(mBall2.getX()+mBall2.getWidth()/2,mBall2.getY(),bombFireRegion);
mScene.attachChild(bombFire);
bombBody = PhysicsFactory.createCircleBody(mPhysicsWorld, bombFire,BodyType.DynamicBody, bombFixDef);
bombBody.setMassData(bombMass);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bombFire, bombBody, true, true));
Vector2 v = new Vector2((xpt-mBall2.getX()),(ypt-mBall2.getY()));
bombBody.applyLinearImpulse(v,bombBody.getWorldCenter());
}
Changing the mass of an existing body is best done by scaling the existing massData of that body. This keeps the center of mass in the right place and also ensures that the mass and rotational inertia match correctly.
b2MassData massData;
body->GetMassData(&massData);
float scaleFactor = desiredMass / massData.mass;
massData.mass *= scaleFactor;
massData.I *= scaleFactor;
body->SetMassData(&massData);
One thing to be aware of is that this does not affect the density of the fixtures on the body. If you add or remove any fixtures from the body after this, the mass data will be recalculated from the fixtures, not from your mass data. So you would have to do this again after changing fixtures to restore your desired mass.

Projection in AndEngine

I want to move my Sprite in AndEngine in a projectile motion.
Can any one help me in this matter with a piece of code? Unfortunately I didn't see an example about projections in AndEngine.
What you're looking for is the UpdateModifier, I suspect.
How do you want your Sprite to move? Do you want it to find a path to the destination or are you just trying to keep something on the screen? Are you using a Camera? All of that will determine how exactly you should approach getting your Sprite to move.
This is a link for the tut about UpdateModifiers:
Update Handlers - Using their Power!
Is that what you were looking for?
for a projectile move , you can use the physicshandler and then you can manipulate the acceleration and velocity to make it move like projectile...
projectile=new Sprite(X,Y, ProjectileTextureRegion);
physprojectile = new PhysicsHandler(projectile);
projectile.registerUpdateHandler(physprojectile);
physprojectile.setVelocityX(Xvelocity);
physprojectile.setVelocityY(Yvelocity);
pyhsprojectile.setAcceleration(Accelration) //negative acceleration
Basically you can use registerEntityMofidier on a sprite (or on any Entity): Andengine EntityModifierExample
Example:
sprite.registerEntityModifier(new MoveModifier(TIME_IN_SECOND, fromX, toX, fromY, toY));
stakesprite.registerEntityModifier(new RotationModifier(0.1f, 0.0f,(float) ((360.0f/Math.PI)*Math.atan(velY/velX))));
stake=PhysicsFactory.createCircleBody(this.mPhysicsWorld, stakesprite, BodyType.DynamicBody, FIXTURE_DEF);
//stake = PhysicsFactory.createBoxBody(this.mPhysicsWorld, stakesprite, BodyType.DynamicBody, FIXTURE_DEF);
stake.setBullet(true);
stake.setLinearVelocity(new Vector2(velX, velY));
stake.setSleepingAllowed(true);
mScene.attachChild(stakesprite);

Categories

Resources