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.
Related
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.
I try to run roll a ball game Unity 3d example in android device, The ball is sticking to the sidewalls and also ball is moving very slowly when the ball is in contact with sidewalls. Help me regarding this issue?
Here is my accelerometer code for ball moving
Screen.sleepTimeout = SleepTimeout.NeverSleep;
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 2);
GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 2);
Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
rigidbody.AddForce(movement * speedAc*2f);
Thanks In Advance
I had a similar problem when building a pinball game. I was not using accelerometer, but the ball behavior was the very same.
Just check out the physic material of yout objects. Ball, walls and either floor has to be checked. As I don't know exactly what kind of game you are building, I recommend you to try out every parameter.
I am using the libgdx framework to create a game. I'm trying create onscreen buttons/controls.
Currently, I have a
class LevelOne that implements Screen.
This class has a private variable world (From Box2d)
I want to add a
Table with Textbuttons or a Libgdx Touchpad to the Box2d world.
However, I'm not sure how to do this.
Next, I know I can add a table or a touchpad to a Libgdx Stage. Is there anyway to get the Libgdx stage and Box2d world to work together so, I can add a Touchpad or Table to the Box2d world.
For onscreen controls, you can do it like this:
Make a new cam which will be fixed for the controls:
OrthographicCamera guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);
Make a (libgdx) Rectangle for each control:
Rectangle wleftBounds = new Rectangle(0, 0, 80, 80);
Rectangle wrightBounds = new Rectangle(80, 0, 80, 80);
Create a new Vector3 to hold your unprojected touch coordinates:
Vector3 touchPoint = new Vector3();
Then you can poll the Input to see if the user is touching these rectangles:
//in render method
for (int i=0; i<5; i++){
if (!Gdx.input.isTouched(i)) continue;
guicam.unproject(touchPoint.set(Gdx.input.getX(i), Gdx.input.getY(i), 0));
if (wleftBounds.contains(touchPoint.x, touchPoint.y)){
//Move your player to the left!
}else if (wrightBounds.contains(touchPoint.x, touchPoint.y)){
//Move your player to the right!
}
}
Notice I'm checking the first 5 touch indexes, thats because you will surely want to have controls that are being used at the same time (i.e. Jumping while Moving Right).
Last but not least, you will want to draw some nice graphics over the controls:
batch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
batch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
If you want to include a HUD stage
Create HUD matrix (import com.badlogic.gdx.math.Matrix4):
HUDMatrix = camera.combined.cpy();
HUDMatrix.setToOrtho2D(0, 0, wwidth, wheight);
Then draw it HUD
batch.setProjectionMatrix(HUDMatrix);
batch.begin();
stage.draw(batch)
batch.end();
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.
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);