Body lost a velocity component when hitting a wall - java

I am creating a Breakout game using Box2D (of LibGdx if anyone interest). Everything works well until when the ball hits the wall when moving with a very small angle. Please look at the image for details:
I tried to set the wall friction to 0 and restitution to 1, as well as the ball's friction and restitution, but it still move along the wall (I have no World Gravity, and only gave the ball a starting Impulse when it hits the paddle). This also happens to the vertical wall if the angle is small enough, it will lose X velocity.
How can I move the ball the way I expected? If there is no friction, what caused the problem?

Try changing the velocity treshold in libgdx.
void World.setVelocityThreshold(float threshold);
float World.getVelocityThreshold();
http://www.badlogicgames.com/wordpress/?p=2030

Related

3D rotation ratios via mouse placement on screen - Java

I am making a 3D game in which the player can rotate their view point via the mouse to look around the environment. I firstly just did x and y rotation via vertical and horizontal movement of the mouse and z via another control. But after playing the game I realised it did not rotate correctly. NOTE: I have a global variable matrix which represents the player's angle (3x1), at 0,0,0 it seems to work correctly as up or down is a direct x axis rotation and right or left is a direct y axis rotation, but if I move my camera diagonally for example then left doesn't directly correlate to a y axis rotation anymore.
Visually on a unit circle the players viewpoint wouldn't travel the full circumference anymore and would travel in a circle that is smaller that the circumference. This is the current code (x and yRateOfRot is the ratio of how far away from the centre the cursor is in each direction between -1 and 1):
private static void changeRotation(){
angle.set(Matrix.add(angle.matrix,new double[][]{
{ROTATION_SPEED * camera.xRateOfRot()},
{ROTATION_SPEED * camera.yRateOfRot()},
{ROTATION_SPEED * camera.zRateOfRot()}}));
}
I have looked at this source http://paulbourke.net/geometry/rotate/ and understand how to rotate via an arbitrary axis which I could do but I am not sure how to correlate this into getting a ratio to find out what the x,y and z change would be for looking in a specific direction i.e. at 0,0,0 the ratio of looking up would be x:1, y:0, z:0 but then at another angle the ratios would be different as looking up no longer means only an x rotation. Any information would be appreciated, thanks!

How can I make a bouncy body bounce lower off another body?

I'm making a game using Box2D in libGDX. I have a "wall" object which is a static ChainShape body set around the screen, some "balls" which are dynamic CircleShape bodies and negative gravity. The balls should be very bouncy, so I set a big 1.1f restitution to them, but the wall shouldn't, and even if I set the wall's restitution to zero, balls bounce off it too high. I need something like setting negative restitution to the wall so the balls will bounce higher off each other and lower off the wall. Any ideas?
You could have 2 variables storing the direction of movement in both axis.
Example:
double xDirection,yDirection;
then in your update method you can add it to the current position of the object.
now if you want it to bounce off a surface you multiply it's value by -1.This will reverse the direction of the object.
For example, say a ball is moving at a speed of 2units in the x-axis.
When the ball hits the edge, xDirection will be multiplied by -1. Now xDirection is -2. Thus, the direction of the ball is reversed.
However, this will make the ball bounce back at the same speed. In order to fix this, you can multiply xDirection with 0.5.Now if we were to multiply the xDirection of the ball with 0.5, xDirection would be -1. Thus, the ball would move slower. Using the same concept you can make the ball move faster by multiplying by 1.5 instead of 0.5. And, you can do this with yDirection too.

Collision Response of ball at Corners of rectangle

i have a fixed rectangle. When ball hits a vertex of a triangle, i calculate tangent at point of contact(rectangle vertex). Then i rotate Velocity vector to coincide with the tangent, flip the y value of velocity and rotate it back. Here is my code for top-left corner:
double c1x=rec.x-ball.getX();
double c1y=rec.y-ball.getY();
if(c1*c1+c2*c2<ball.getRadius()*ball.getRadius())
{
double angle=Math.atan2(-c1x,c1y); //angle made by tangent
Vector2D v=ball.velocity.rotate(angle); //rotate velocity vector by 'angle'
if(v.y<0.0f) //ball is already moving away from corner
return;
v.setY(-v.y); //flip y
ball.velocity=v.rotate(-angle); //rotate back
return;
}
But this code doesnot works. When ball strikes the corner it gets stuck, moves along the top edge and then falls off, not bouncing back. Am i doing it correct?
Making some guesses about your conventions, I suggest this:
double angle=Math.atan2(-c1x,-c1y);
If that doesn't work, we can do some experiments to figure out what's actually going on.
EDIT:
All right, we must build up from simpler solutions. Try these experiments:
1) Set velocity to (0,1) and verify that the ball moves straight up after the collision.
2) Set velocity to (-1,1) and verify that the ball moves up and leftward.
If those work as expected, then pick a trajectory that you would expect to behave in a normal way, such as approaching from above and to the left, then rebounding almost straight up but slightly leftward, then run it through your code but print out all the values in the collision and tell us what they are. That's at least c1x, c1y, angle, and the values of velocity initially and after each rotation.You'll probably notice where things go screwy, and if you don't we will.

How would I create Horizontally centered “Gravity” ? - libGDX -

This is a seemingly simple game mechanic that I've been trying to figure out how to do.
To try and explain I will describe a idea (problem):
Basically we say there's a vertical line that is centered in the
screen.
We have a sprite object that changes it's horizontal velocity to
dodge missiles, however in doing that the object would just drift
away.
How can I add a strong gravity force to the horizontal "center line"
of my screen so that my sprite will "fall" back into it every time it
boosts its velocity outwards?
I could post my source code but it wouldn't be too helpful to solving the question in this particular situation.
I've searched around for days trying to figure this out so any help especially with code examples would be very helpful!
I've programmed this type of thing in the past. Gravity (in physics) is an acceleration, so
1) if the sprite is to the right of the line you subtract from its horizontal velocity every 1/n seconds, and
2) if the sprite is to the left of the line you add to its horizontal velocity every 1/n seconds.
Experiment with adding/subtracting a constant, or with adding/subtracting a number that increases the farther the sprite is from the center line.
Either way you do it, that's going to create a pendulum effect. You'll also have to add a dampening factor if you don't want that. One simple approach is that if the sprite is headed away from the center line, the value you add/subtract is larger than if the sprite is heading back towards the center line. So the "gravity" that pulls the sprite to a stop is greater than the gravitational acceleration that brings the sprite back to the center line.
As you are using libgdx you should also use camera. So you don't have to calculate verything in pixels. So for example you say my screen is 16 worldunits width and 9 world units height (16/9 aspect ratio). So you can say the center of gravity is in the center of that 16, so at 8.5 if i am not wrong. Now you can say: if (player.center.x < 8.5f) { player.xSpeed += GRAVITY_HORIZONTAL } and if (player.center.x > 8.5) { player.xSpeed -= GRAVITY_HORIZONTAL }. In this case the gravity is a constant value. But as #BrettFromLA said you can also let the value grow if the distance to the center grows.

Need better way of making the ball angle in pong game

I'm making a pong clone to practice my coding, and I've become stuck on making the ball be able to change angle when hit by the paddle.
My current implementation has a deltaX and deltaY for the ball, which moves with the game loop to move the ball. The way I have done it is that if you hit the ball while the paddle is moving, the deltaY is increased or decreased depending on the direction of the paddle, but this does not feel natural at all for the game.
Does anyone know a better way of doing this?
First thing I would do is change the deltaX and deltaY to ballAngle and deltaSpeed. That way you'd be moving from a rectangular coordinates system to a polar one. Due to the nature of the movement of the ball (Goes in a straight line and changes the angle of the line at each impact) this will make your work easier. From now on you'd only have to change ballAngle to update the ball's direction.
However you'll have to update the function that draws the balls for it to move back from polar to rectangular coordinates so you can display it on the screen. A little bit of high-school trigonometry will let you calculate screen position deltas depending on your angle and speed:
newPosition = oldPosition + movementVector
with:
movementVector.x = deltaSpeed*cos(ballAngle)
movementVector.y = deltaSpeed*sin(ballAngle)
Of course these equations might need some modification based on relative to what you measure the ball's angle.
Now to modify the ball's angle at each collision with the paddle, you only have to increment or decrement the angle of the ball depending on which part of the paddle it touches, and the math in the drawing function should take care of updating x and y positions realistically.
I hope this helps.

Categories