Sprite vibrating on straight lines - java

My problem is when I move my sprite either directly upwards or directly sideways the sprite vibrates. I have tried some things but cant get it fixed. Here is my current code. The framework is libGDX. I have tried moving the sprite exactly to the mouse if its within 1 pixel range and other things but nothing works for me. Why does the sprite vibrate on a straight line and how can I stop it? Thanks!
float speed = 4; // the speed of the sprite
Vector3 mousePosition = new Vector3();
if (Gdx.input.isTouched())
{
camera.unproject(mousePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0));
if(mousePosition.x > 0 && mousePosition.x < Screen.WIDTH )
{
if(mousePosition.y > 0 && mousePosition.y < Screen.HEIGHT)
{
if(mousePosition.x > player.x + (player.width /3))
{
player.x+=speed;
}
else if(mousePosition.x < player.x + (player.width /3))
{
player.x -=speed;
}
if(mousePosition.y > player.y + (player.height /2))
{
player.y+=speed;
}
else if(mousePosition.y < player.y + (player.height /2))
{
player.y-=speed;
}
}
}
}
batch.draw(player.sprite, player.x, player.y);

Consider the case where you are moving the player vertically up. In this case mousePosition.x remains constant and mousePosition.y increases. mousePosition.y increases in such a way that player.y never exceeds it, and so the sprite follows the mousePosition in the Y direction. But remember that mousePosition.x remains constant. So according to your code player.x will increase till it satisfies the condition mousePosition.x > player.x + (player.width /3), and once it exceeds that value, player.x will start decreasing till it satisfies mousePosition.x < player.x + (player.width /3). That means player.x is increasing and decreasing, causing it to vibrate in X direction. Same case occurs when you move horizontally. But in this case, the sprite will vibrate it Y direction

Related

Collision Detection between player and tiles almost works

I have been stuck on making some collision detection in my game (its kind of like Terraria) for a while but i made this code and... well, it kind of works. It works if the collision is above or on the left of the player, but if the collision is on the right, or below, instead of bouncing back, the player accelerates through the blocks until there is empty space. Here is the code that i made:
private void checkCollision() {
for(int x = (int) (xpos-1); x <= xpos+1; x++){
if(x < 0 || x > main.mw-1) continue;
for(int y = (int) (ypos-2); y <= ypos+1; y++){
if(y < 0 || y > main.mh-1) continue;
if(main.map[x][y] == null) continue;
if(!main.map[x][y].solid) continue;
if(main.map[x][y].blocktype == -1) continue;
double distance = Math.sqrt((xpos-x)*(xpos-x) + (ypos-y)*(ypos-y));
if(distance > 1.0){
continue;
}else{
double x_overlap = Math.max(0, Math.min(xpos + 16, x + 16) - Math.max(xpos, x));
double y_overlap = Math.max(0, Math.min(ypos + 32, y + 16) - Math.max(ypos, y));
double overlapArea = x_overlap * y_overlap;
if(overlapArea > 0){
if(x_overlap > y_overlap){
yblock += y_overlap/2;
}
if(x_overlap < y_overlap){
xblock += x_overlap/2;
}
//guessing i need to do something here to make player
go other way if block is on other side
}
}
}
}
}
So how would i make the player bounce back if the block that he is colliding with is on the right or below. Also is there any way i can make this smoother - right now the player be bouncing all over the place. Thanks! :)
What you want to do is keep track of the player's location, and if the location after moving is out of bounds you can reset the player's position to be right on the edge of the limit.
That's how I dealt with collision detection, I answered another question similar to this one though some folk decided to downvote the answer, go figure.

Libgdx Sprite crossing screen edge

I want to implement a smooth transition when my sprite is exiting the screen. This means that if my sprite exits the screen from the left edge, it should appear on the right edge. The main problem that I am having is that I don't want to simply set the new coordinates of the sprite from one edge to another. For example, if the sprite is half through the left edge, the other half should appear on the right edge (smooth transition). Can anyone give any advice for this problem?
Pseudocode!
if(sprite.pos.x > screen.width)
{
sprite.pos.x -= screen.width;
}
else if(sprite.pos.x < 0 - sprite.width)
{
sprite.pos.x += screen.width;
}
if(sprite.pos.x + sprite.width > screen.width && sprite.pos.x < screen.width)
{
batch.draw(sprite.texture, sprite.pos.x - screen.width, sprite.pos.y);
}
else if(sprite.pos.x < 0 && sprite.pos.x + sprite.width > 0)
{
batch.draw(sprite.texture, sprite.pos.x + screen.width, sprite.pos.y);
}
batch.draw(sprite.texture, sprite.pos.x, sprite.pos.y);

Collision detection in Java Swing

I'm trying to get the ball bounce of the bat in my mini tennis game. I'm not sure what methods to implement. Currently the ball bounces and both bats move the aim of the game is for a player to get the ball past the oppositions. Is there any special methods?
For implementing the game boundary's simply create a method that checks collisions(say your boundarys are 150 by 150) so
if(x < 0 | x > 150 | y < 0 | y > 150) { // if ball is within boundary's
xa = xa * -1; //switch direction of ball(bounce)
ya = ya * -1;
score += 1;
}
So when the ball hits the boundary the score goes up one and the ball bounces the ball back, but you can also reset the ball with another method.
Btw you can print text on the screen with
g2d.drawString("Score: " + score, 150, 150); // prints score on screen
EDIT this kind of explains collisions for a basic pong game, it might help a little in your case.
Heres the full code http://www.dreamincode.net/forums/topic/172211-programing-an-applet-game-of-pong/
public void checkCollision(){
//remember, our ball is 10*10 and the x and y positions are the
//top-left corners of the ball. If the top left corner y position
//is 0 or 290, we reverse the y- direction that the ball was
//travelling in by multiplying ball.dy by -1
if(ball.getY() == 0 || ball.getY() == 290){
ball.dy = (ball.dy * -1);
}
//if the ball is at the right-hand edge of the human paddle's
//domain and the boolean method hitPaddle() is true, then we
//reverse the dx position of ball by multiplying ball.dx by -1
if((ball.getX() == 40) && hitPaddle()){
ball.dx = (ball.dx * -1);
}
//we already know that the computer paddle can't miss, so if
//the ball reaches the left-hand edge of the paddle, we can make the
//dx switch directions without any additional checks
if(ball.getX() == 460){
ball.dx = (ball.dx * -1);
}
//if the ball is missed by the human paddle and reaches the
//left-hand edge of the applet window, then reset the ball
//and increment the score
if(ball.getX() == 0){
pRight.setScore(pRight.getScore() + 1);
ball.reset();
}
}
public boolean hitPaddle(){
boolean didHit = false;
//this just checks if the ball is lined up between the top and
//bottom right-hand corners of the human paddle
if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY()){
//sets didHit to true
didHit = true;
}
return didHit;
}

Java / LWJGL Pong AI issue

I am struggling with what must be a basic concept, but can you have a look at my issue?
I have the code where: ai moves the player bat, HEIGHT = total height of Display, and batHeight is the size of the pong paddle/bat:
public void ai(int bally, int HEIGHT, int batHeight) {
if (bally < this.y + ySize / 2) {
if (this.y <= 0) {
System.out.println("Upper Bound");
y = 0;
} else {
y -= 2;
}
}
if (bally > this.y + ySize / 2) {
if (this.y >= HEIGHT - batHeight) {
System.out.println("Lower Bounds");
y = HEIGHT - batHeight;
} else {
y += 2;
}
}
}
The above does exactly what I want it to do. Pong Bat moves up, and when it hits the top of the screen, it prints the console line, and stops the Bat. Exactly the same happens at the bottom of the screen. It prints the console, and stops the bat. It does this every time with no issues.
Now, if I modify the code slightly:
public void ai(int bally, int HEIGHT, int batHeight) {
if (bally < this.y + ySize / 2) {
if (this.y <= 0) {
System.out.println("Upper Bound");
y = 0;
} else {
if(rand.nextInt(2)+1 == 1){
y -= 2;
}else{
y -=3;
}
}
}
if (bally > this.y + ySize / 2) {
if (this.y >= HEIGHT - batHeight) {
System.out.println("Lower Bounds");
y = HEIGHT - batHeight;
} else {
y += 2;
}
}
}
It iterates once, stopping at the top bound, but then it loses itself, and forgets the bounds and the bat moves off the screen. I have Console printing the Bat y position, and it tracks with no issue, accurately displaying its y co-ord, but after the first iteration, it goes to negative y and greater that screen height.
I did have the theory that you cannot nest a IF inside an ELSE statement, so i tried moving it around so that it read:
if(this.y != 0){
if(rand.nextInt(2) + 1 == 1){
//move the paddle at speed 1
} else {
//move paddle at speed 2
}
}else{
//do not move the paddle
}
But that made no difference.
The idea behind the code was to add some chance for the AI bat. Sometimes its fast, and other times it is slower.
Thanks in advance,
Your code from far away looks like this:
for a given time:
if the ball is below the paddle {
if the paddle is below the screen, put it back
else move it down 2 or 3 units
}
if the ball is above the paddle {
if the paddle is above the screen, put it back
else move it up 2 units
}
Imagine the case where the ball is at y = 1 and the paddle is at y = 2. The first if statement will be triggered (1 < 2), the paddle is not outside (2 > 0), so it moves down 2 or 3 units. Let's say 3, for argument's sake. Now, paddle is at y = -1, the ball is still at y = 1. Now, the condition for the second big if is true! So we enter it: the paddle's not above, and we move it up two units. Now, the paddle is at y = 1...
It is clear that it should not have entered the second loop. So, stick an else in front of it, because it should only ever enter one :)

Java 2D Collision?

Hey guys i'm making a 2D java game and i'm trying to figure out how to make a good collision code. I am currently using the following code:
public void checkCollision() {
Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32);
for(Wall wall : walls) {
Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32);
if (player_rectangle.intersects(wall_rectangle)) {
Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle);
if (player.xspeed > 0) {
player.x -= intersection.getWidth();
}
if (player.yspeed > 0) {
player.y -= intersection.getHeight();
}
if (player.xspeed < 0) {
player.x += intersection.getWidth();
}
if (player.yspeed < 0) {
player.y += intersection.getHeight();
}
Print(Integer.toString(intersection.width) + ", " + Integer.toString(intersection.height));
}
}
}
With this code it works fine if you are press one button but if press down and left for example the player will fly off in some random direction.
Here is a picture of the types of maps I have:
Your main problem is in assuming that the player is running directly into the wall. Consider the case where there is a wall rect (100,100,32,32) and the player is at (80,68,32,32). The player is moving down and to the left, so player.xspeed < 0 and player.yspeed > 0; say the next position for the player is (79,69,32,32). The intersection is then (100,100,11,1).
Note that although the player is moving left (as well as down) the wall is actually to the right of the player. This line:
if (player.xspeed < 0) {
player.x += intersection.getWidth();
}
... causes player.x to be set to 90 in a sudden jump.
One thing you could do is check that the player's left-hand side was contained in the intersection, i.e.
if (player.xspeed < 0 && player.x >= intersection.x) {
player.x += intersection.getWidth();
}
Obviously a similar thing needs to be done for the other directions too.

Categories