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));
Related
I am new to Unity and I am trying to learn the basics, I learn physics at school(ten grade).
What I have done so far - added a ball to my project and used gravity on it with RigidBody.
I want to make the ball jump suddenly on air when there is some touch input, for example - flappy bird.
My script is basic:
void Update()
{
if (Input.touchCount == 1)
{
GetComponent<Rigidbody>().AddForce(new Vector2(0, 10), ForceMode.Impulse);
}
}
With this script, the ball is falling(gravity) and when I touch the script, is Y coordinate changes but it happens by sudden (no animation) and it changes by like ~1 and continue falling(I can't keep the ball on screen) , also I can make it jump only once, if I press multiple times it will jump only once, as you can see here:
https://vid.me/aRfk
Thank you for helping.
I have created same scene in Unity3D Editor and played a little with same setup you have. And yes I had similar problems adding force on Update and also (but less) on FixedUpdate. But adding force on LateUpdate seems to work okay.
Here is my code:
public Rigidbody2D rb2dBall;
void LateUpdate()
{
if(Input.GetKeyUp(KeyCode.Space))
{
rb2dBall.AddForce (Vector2.up * 10f, ForceMode2D.Impulse);
}
}
And also I turned on interpolation:
I cant say why the physics bahaves like this, could be some bug in Unity3D 5.X since on FixedUpdate it should work fine.
Firstly, when working with physics in Unity, it's highly recomended to use the FixedUpdate method, instead of the Update. You can check in this link http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
The second thing is that maybe you are not applying so much force at the ball, the force needed to give a quite impulse will depends of the mass of your ball's rigidbody. So try to do something like that:
GetComponent<Rigidbody>().AddForce(Vector2.up * 100, ForceMode.Impulse);
Change the fixed value 100 to adjust your needs.
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'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.
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.
I'm using Flash CS 5 and Flex 4, both to build an AIR application for android. I would like to know how to allow the user to move content(image or text) up and down(like a map,in this case only vertically).
There are no touch UI controls available yet, so you need to implement it yourself. Here's a little bit of code that might help get you started. I wrote it on the timeline so that I could test it quickly. You'll need to make a couple adjustments if you're using it in a class.
The variable content is a MovieClip that is on the stage. If it is larger than the height of the stage, you'll be able to scroll it by dragging it with the mouse (or with your finger on a touch screen). If it is smaller than the height of the stage, then it won't scroll at all because it doesn't need to.
var maxY:Number = 0;
var minY:Number = Math.min(0, stage.stageHeight - content.height);
var _startY:Number;
var _startMouseY:Number;
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
_startY = content.y;
_startMouseY = mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler, false, 0, true);
}
function stage_mouseMoveHandler(event:MouseEvent):void
{
var offsetY:Number = mouseY - _startMouseY;
content.y = Math.max(Math.min(maxY, _startY + offsetY), minY);
}
function stage_mouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
}
Alternatively, you could use the scrollRect property. That one is pretty nice because it will mask the content to a rectangular region for you. If you just change y like in the code above, you can draw other display objects on top of the scrolling content to simulate masking. It's faster than scrollRect too.