I've noticed some very strange behaviour with a simple piece of code, I've stripped it down to remove any variables, time-step, acceleration, etc.
I have a shape on the screen, pressing the left side of the screen thrusts the shape right, pressing the right side thrusts left. For some reason the shape favours moving left (speed < 0). It seems to be that it decelerates properly when moving right but when moving left it finishes decelerating then continues for maybe 0.3 seconds before stopping.
float speed;
int x;
Update method for the shape:
speed *= 0.9f;
if (Math.abs(speed) < 0.1f)
speed = 0;
if (Gdx.input.isTouched()) {
if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
speed = 10;
} else {
speed = -10;
}
}
x += speed;
x should be defined as float. I used int because the viewport is set to the screen size but finally realised it needs to have the precision.
Related
I have recently started programming games in java. I come from a C# / XNA background and i already have experience in game development.
However I have a problem in my java game. I have a bouncing script which makes a in-game "particle" bounce upon collision with a solid surface, such as the ground.
Here is a snippet of the code that manages the bouncing.
private final void respondY(Rectangle r)
{
if(!r.intersects(getBounds())) // If not colliding, return
return;
if(Yvel > 0) // If falling...
{
if(r.y < Y + Height) //Another collision test
{
Y = r.y - Height; // Set Y to top of object
Yvel *= -bounce; // Bounce (Here is the problem)
onFloor = true;
}
}
else if(Yvel < 0) // If hit ceiling
{
if(Y < r.y + r.height) // Collision test
{
Y = r.y + r.height; // Set Y to bottom of object
Yvel = 0; // No bouncing here
}
}
}
The problem is that the object bounces upon the floor as it should, but after a while the object bounces constantly at the same height where I want it to stop bouncing when it got to that constant height.
NOTE:
Yvel is a int that is the vertical velocity of the object
Bounce is a float that controls how "bouncy" a object is. e.g. 0.5 means that it bounces half as high as it fell
Thanks in advance! Please note that this is my first post so if I make mistakes please point them out in a constructive manner.
The float (and double) types are imprecise. This especially causes problems when dealing with very small numbers - in fact there's a smallest representable number and a finite quantity of possible numbers that can be represented (and bit like quantum physics). The number stored by a float is actually the closest number possible to represent to the calculation result.
What's happening is the velocity calculation never reaches zero, because the result of multiplying the smallest number float can represent by a value >= .5 is itself.
I would force it to zero by putting a low threshold on the calculation result:
Yvel = Yvel * -bounce;
if (Yvel < .000001F)
Yvel = 0;
While practicing wanted to create basic game-kind-of(?) - there is just a basic shape (rectangle) and you move forward with one button, move backwards with another, and rotate it with 2 additional buttons, that shape as well should move to other direction accordingly to it's rotation. What i have done so far is:
Input handling:
if (input.isKeyDown(Input.KEY_UP)) {
rectX += Math.cos(Math.toRadians(rotation)) * (0.2*delta);
rectY += Math.sin(Math.toRadians(rotation)) * (0.2*delta);
}
if (input.isKeyDown(Input.KEY_DOWN)) {
rectX -= Math.cos(Math.toRadians(rotation)) * (0.2*delta);
rectY -= Math.sin(Math.toRadians(rotation)) * (0.2*delta);
}
if(input.isKeyDown(Input.KEY_LEFT)){
rotation-=(0.1*delta);
rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
if(rotation <= 0f) {
rotation = 360f;
}
}
if(input.isKeyDown(Input.KEY_RIGHT)){
rotation+=(0.1*delta);
rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
if(rotation >= 360f) {
rotation = 0f;
}
}
rectangle.setCenterX(rectX);
rectangle.setCenterY(rectY);
}
rectX, rectY are obviously center coordinates of rectangle (floats)
and rotation is... well rotation..
And I think that's it, other code isn't worth mentioning.
In my opinion this should work, but the thing is while movement direction is changin at normal speed, my rectangle is spinning wildly, probably about 100 times or so faster than its movement direction.
Delta is the number of milliseconds since the last call to update().
I'm trying to make a platform game. I have the collision code (almost) but there seems to be a bug. I try this code:
for (int i = 0; i < world.ground.size(); i++) {
if (!world.ground.get(i).intersects((int) x, (int) y, player_width, player_height + (int) dy)) {
y += dy;
if (dy < 4) {
dy += 0.1;
}
} else {
dy = 0;
jumped = false;
}
}
But sometimes my character's foot goes through the ground by 2 or 3 pixels. Is there a better way to do this? Please help, thanks.
It seems like you are using a posteriori (discrete) collision detection. This causes your object penetrate through a little every time becuse it activates only when touched or penetrated. You may think to convert this to a priori (continuous) collision detection. This way, it never penetrates the ground because it checks before the collision then adjusts speed or position in order to avoid penetration.
If you dont want to fiddle with this kind, you can just add a correction function that acts before painting.
void foo()
{
//check if below ground
//if yes, displecement upwards until it is above enough
//now you can paint
//return
}
I see you implemented this:
what_you_need_to_make_it_above_ground=(ground_y-feet_y);
//im not sure if dy is ground level so i added second boolean compare
if ((dy < 4)||(ground_y>feet_y)) {
dy += 0.1; // this isnt enough. should be equal to:
dy +=what_you_need_to_make_it_above_ground;
dy +=if_there_are_other_parameters_think_of_them_too;
}
I have given a diagram of my current small problem that I need help with. My main purpose is to keep the point from going outside the circle. Nothing else.
The center of the circle is positioned at (x, y).
I only solved a little bit of the problem, and that is the collision detection part of my problem, as given below:
public void bound(Point p, Circle c){
double distance = Math.hypot(p.x - c.x, p.y - c.y);
if (distance >= c.radius){
//Clueless from here on out.
}
}
The part where I left a comment is the spot I couldn't figure anything out. I did tried to set the point's velocityX and velocityY to 0, but I realized the point will just stay put whenever it touches the circle.
So, I'm sort of stuck.
I have resolved this issue.
public void reflect(Hole h){
//R = -2*(V dot N)*N + V
//N is normalized.
double nx = (this.position[0]+this.diameter/2) - (h.x+16);
double ny = (this.position[1]+this.diameter/2) - (h.y+16);
double nd = Math.hypot(nx, ny);
if (nd == 0)
nd = 1;
nx /= nd;
ny /= nd;
double dotProduct = this.speed[0]*nx+this.speed[1]*ny;
this.speed[0] += (float)(-2*dotProduct*nx);
this.speed[1] += (float)(-2*dotProduct*ny);
}
public void reflectResponse() {
for (int i = 0; i <= 1; i++) {
position[i] -= speed[i];
speed[i] *= 0.992f;
}
}
I tried Oli Charlesworth's method from the comments, but it made things more... "complicated" than I expected. Someone else mentioned I used a completely 100%, vector-based algorithm, since I'm relying a lot on vector-based movements.
TIPS TO THOSE WHO DO READ THIS:
If you're working on object movements and collisions with vectors, seek vector-based algorithms.
If you're working with angles (either degrees or radians), use Oli Charlesworth's method.
So i'm just trying to make a ball bounce around the screen which should slow down due to gravity and reflect (bounce) from the wall like a normal ball would.
Can someone give some basics and VERY simple implementation of this?
Other examples seem a bit "overdone" and seem to go beyond what I want to do.
I've tried this:
public void updateLogic() {
if (x < -1) {
xPos += (-x * gravity);
} else if (x > 1) {
xPos -= (x * gravity);
}
if (y > 1) {
yPos += (y * gravity);
} else if (y < -1) {
yPos -= (-y * gravity);
}
}
This is the closest I got by myself.
By the way the x and y values are from the accelerometer.
Any help would be much appreciated, thanks!
I think you'll need 3 things for this, forces (x and y, which you have), velocities (call them xVel and yVel) and positions (xPos and yPos which you also have). The position of the ball is updated (in the simplest way) by:
xPos += dt*xVel;
yPos += dt*yVel;
xVel += dt*x;
yVel += dt*y;
The variable 'dt' is the timestep, which controls how fast the ball will move. If this is set too large, though, the program will be unstable! Try dt = 0.001 or so to start and gradually set it higher.
Then, to get the ball to reflect from the walls, just flip the velocity if it hits a wall:
if (xPos > xMax) {
xPos = xMax;
xVel *= -1.0;
} else if (xPos < 0.0) {
xPos = 0.0;
xVel *= -1.0;
}
and the same for y. The 'xPos = ...' is just to stop the ball going off the edge of the screen. If you'd like the ball to bounce a little less every time it hits a wall, change the '-1.0' to '-0.9' or something (this is the coefficient of restitution).
Hopefully that should be all. Good luck!
Some comments on your actual code:
Both of these lines do exactly the same thing:
yPos -= (-y * gravity);
yPos += (y * gravity);
likewise, both of these lines do the same thing:
xPos += (-x * gravity);
xPos -= (x * gravity);
You are not handling the cases -1 <= x <= 1 or -1 <= y <= 1
Some comments on the concepts:
Start with one ball.
You want the ball to have two separate velocities (both either positive or negative), one in the x-direction and one in the y-direction. Every frame, add those velocities to the ball's position.
Then add collision detection. Check each frame if the center of the ball is outside the screen. If it is, make it bounce (the easiest way to do this is to make the ball's y-velocity positive when it goes off the most-negative y-value displayed on the screen; and do similarly for the other screen-edges).
Improve the collision detection: see if you can figure out how to check if any part of the ball it outside the screen (hint: to check if the ball is off the left-side of the screen, you only need to check the left-most coordinate of the ball)
Then take gravity into consideration - gravity will be a constant number subtracted from the y-velocity every frame, until the y-velocity hits 0.
Youtube video
Source code available at github.
Your x coordinate should not be affected by gravity.
I would advise against using accelerometers until you understand how the physics work for the simple case.
Break it up into stages. First, ensure that you can get the ball falling at a constant velocity.
Once you have that working, worry about the gravity causing the velocity to increase.
Once you have that working, worry about the intersection with walls and velocity changes. A simple algorithm for bouncing off the walls would be to multiply the x velocity by -1 when you hit a vertical wall, and multiply the y velocity by -1 when you hit a horizontal wall.