Preventing body from too high acceleration - android

I am currently creating an Andengine game which features a ball and a blocker just like pong, except in my game the world and blocker aren't squares and are instead circles.
Right now when the ball bounces off of the blocker all physics seem to check out alright but when I move the blocker really fast to the destination and the blocker hits the ball the ball goes flying off the screen at insane speeds.
How would I go about keeping the ball's velocity at the same speed all the time while keeping current physics?

Don't remember exact code but the logic is:
in onManageedUpdate check the speed of your object, then if speed exceed some value you are happy with set the linear speed to this good value.
Other solution could be that you use ContactListener and in postSolve or endContact you set velocity to desired one.
Also check why this speed after bounce is that fast. Is it because a blocker is moving really fast? Does it also happen when blocker is moving slowly?

Related

I've played around with the various physics settings, but objects still feel too "floaty"

I'm using the Corona SDK with the Box2D engine, and I'm trying to make a Flappy Bird style game just to get familiar with the physics engine. I've tried increasing the density of the main character, increasing gravity, changing the scale of the physics stage, etc, but the main character still feels too "floaty".
Increasing the gravity came as close as I could to feel right, but there are still issues. If the user taps the screen quickly in succession, the momentum builds up quickly and the character goes flying up off the screen at high speed. Here is my code right now:
physics.setGravity( 0, 60 ) --default is 9.8
function screenTap()
flappy.isFixedRotation = true
flappy:applyLinearImpulse( 0, -300, flappy.x - 3, flappy.y )
flappy.isFixedRotation = false
end
In the original Flappy Bird game, it seems to have normal gravity, but rapid taps to the screen wouldn't make the bird fly up too quickly. It almost had the feel of something "heavy" like a bowling ball, but obviously in this world objects of all masses fall at the same speed. I don't have much experience with physics engines, so I would appreciate any suggestions.
If you have played flappy bird recently, you should have observed that when you tap the bird in rapid succession, its vertical velocity does not increase! The flappy bird developer seems to have made the bird's vertical velocity set to a certain value whenever it is tapped rather then have it accelerate vertically.
So, my advice would be to simply set the character's vertical velocity to a set value whenever the screen is tapped instead of accelerating it.

How to implement movement speed of object correctly?

So I am coing an android game and have managed to make a ball roll over the screen in the direction you tilt your phone. However I would like to make the ball roll faster the more you tilt your screen.
But what is the best way to implement this? Taking bigger steps is obv not good, it makes collisions hard to calculate. I want to move more steps per second instead.
So lets say you have a tiled board and you implement speed as tiles/millisecond. But that is problematic also speed will not be continous. You'd perhaps move 1 step every 10th time in a loop instead of every time in the loop. So you would move, then be still, then move, etc instead of continously moving. But maybe that is as good as it gets?
So this problem applies generally for any kind if computer graphics I guess. How do you implement this the best way? I'm specifically interested in what applies to Android.
The natural way of implementing speed and position problems is to have position calculated with the speed that way :
position = speed * dt
with dt constant, adapted for your implementation.
So basically the natural way is to increase the step. You say it's obviously bad for collision detection but with a limited speed and a small dt I don't really see why.

Android Game Development: Framerate & Collisions

I've created a simple collision detection script, which works this way:
When the distance between the hero and an object is x pixels, the hero can "walk" x pixels, when he would not collide with an object (hero + 3px = no collision) he moves by 5 pixels.
But I also have to consider the framerate and therefor multiply his speed with the elapsed time /20
My problem is, when the framerate at some time is very low or high, he just moves by an additional pixel (1px) ..the chance is very small, but it still can happen.
so what can I do to prevent this?
Add a position correction to the end of post-collision-check or add a velocity correction to the end of pre-collision-check.
Post-collision: object is translated back to the collision point.
Pre-collision: object speed is altered temporarily so in the next frame it will be on the point of collision.
Example:
Your object moves 75 pixels and tunnells through the wall. What to
do? You need a position history of 1-iteration-back. Looking to
history, you see it was actually behind the wall, then the current
location-->it is now passed the wall xx pixels. Then you would set
its new position next to wall before painting.
You cannot know when it will lag your android: better algortihm needed to make it independent of fps. How? you may just pause whole world a bit until fps is steady again or just store the next few iteration before painting then calculate things before painting.

Bullet movement

I'm new at games development and now stalled at one problem.
Trying to make tower defence type game, For now i have made "tower" with some functions, and where is problem with firing bullets.
Idea: touch somewhere on screen and bullet fire that direction. (bullet starting pos in middle of screen, speed must be a constant)
Maybe somebody knows how to calc that direction which bullet should fire.
I think, should calc angle or something to get that direction...
i.e.
this.mPhysicsHandler.setVelocity(angle*SPEED, angle*SPEED);
Thanks.
It depends a little bit if it's 2D or 3D but the idea is basically the same.
I would start by making a Vector class which has to variables, x and y. I would then add a few method for adding and subtracting Vectors. What you would have to do then would be to subtract from the touchPosition Vector the firingPosition Vector which would give you the direction Vector towards which you have to shoot.
If you need more help with this just comment.

Reflecting a body with same speed in Box2d after collision AndEngine

Is there any way to reflect a physics body in Box2d in AndEngine with the same speed after collision.
Suppose if a ball hits the wall with speed X and reflects, then the speed after reflecting must be X again. Is that possible? If so please tell me how can I do this.
What you're looking for is the second parameter of
PhysicsFactory.createFixtureDef(pDensity, pElasticity, pFriction)
If you set the restitution coefficient (elasticity) to 1, the body should be perfectly elastic thus having the same speed after it bounces off the wall. That is in theory. In practice, the body will still be losing a little bit of momentum due to the limited precision of calculations.

Categories

Resources