Reflecting a body with same speed in Box2d after collision AndEngine - android

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.

Related

Preventing body from too high acceleration

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?

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 OpenGL ES 2.0 : Translation, collision and control over framerate

I try to design my own game engine on Android, with OpenGL ES 2.0.
So far, I have a sphere moving along the x axis with the equation x(t) = v*(t-t0) + x0. Let's say the radius of the sphere is r. I use the fonction SystemClock.uptimeMillis() to compute the time t.
There are two walls, at x=1 and x=-1. When the sphere touches one of these two walls (i.e. when the distance between the sphere and one wall is less than r), then it bounces back.
My calculations are done in the onDrawFrame() method of my renderer.
Therefore, the calculations are only done when a frame is rendered. So, the frequency of the "collision check" depends on the framerate of the application. Alas, sometimes the next frame takes too long to be rendered and the translation takes the sphere behind the wall, like some kind of quantum particle :).
Is it possible to have some kind of control over the framerate of my application ? Do you know a method to avoid such discontinuities in the trajectory of my sphere ?
Thank you very much for your held !
"Tunneling" through walls is a consistent problem in video games. Much time is spent combating such behavior. When checking your collisions, you really need to know the previous position and the new position. Then you check to see if there is a wall in between those two positions, and if so, you re-orient your object as though it had bounced off of the wall.
A collision check that looks for distance between sphere and wall is not enough.

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.

Categories

Resources