On a Java graphic representation, when an object arrives to it destination, it starts shaking [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I am creating a Buffet representation and based on the status of the client it has a destination, when the client statis is not "agafantplat" (taking dish) it is supossed to stay on a table at a certain coordenates, it stays, but starts shaking. I guess is because the client still tries to arrive at its destination despite already being there. I thought adding a boolean should solve the problem but it doesn't.
This is the code of the run that sets the coordinates:
public void run() {
double angle;
boolean haLlegado = false;
while (true) {
try {
Thread.sleep(10);
int index = this.getModel().getComensales().indexOf(this);
if (index <= 3) {
angle = Math.atan2(700 + 20 * index - y, 60 - x);
} else if (index <= 8) {
angle = Math.atan2(690 + 20 * (index - 4) - y, 200 - x);
} else if (index <= 13) {
angle = Math.atan2(670 + 20 * (index - 8) - y, 255 - x);
} else if (index <= 18) {
angle = Math.atan2(670 + 20 * (index - 13) - y, 400 - x);
} else if (index <= 23) {
angle = Math.atan2(670 + 20 * (index - 18) - y, 565 - x);
} else if (index <= 28) {
angle = Math.atan2(670 + 20 * (index - 18) - y, 690 - x);
} else if (index <= 33) {
angle = Math.atan2(600 + 20 * (index - 23) - y, 765 - x);
} else {
angle = Math.atan2(600 + 20 * (index - 23) - y, 790 - x);
}
x += 10 * Math.cos(angle);
y += 10 * Math.sin(angle);
if (this.getStatus().equals("agafantPlat")) {
if (!haLlegado) {
if (!RestaurantView.getComida_1().getText().equals("")) {
angle = Math.atan2(550 - y, 90 - x);
x += 10 * Math.cos(angle);
y += 10 * Math.sin(angle);
this.agafarplat();
if (this.getY() == 550 && this.getX() == 90) {
RestaurantView.getComida_1().setText("");
this.menjar(this);
System.out.print(this.status);
haLlegado = true;
}
} else if (!RestaurantView.getComida_2().getText().equals("")) {
angle = Math.atan2(500 - y, 300 - x);
x += 10 * Math.cos(angle);
y += 10 * Math.sin(angle);
this.agafarplat();
if (this.getY() == 500 && this.getX() == 300) {
RestaurantView.getComida_2().setText("");
this.setStatus("menjant");
haLlegado = true;
}
} else if(!RestaurantView.getComida_3().getText().equals("")) {
angle = Math.atan2(500 - y, 600 - x);
x += 10 * Math.cos(angle);
y += 10 * Math.sin(angle);
this.agafarplat();
if(this.getY() == 500 && this.getX()==600) {
RestaurantView.getComida_3().setText("");
this.setStatus("menjant");
haLlegado = true;
}
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Related

Unknown NullPointerException 10 ignore> [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
My code is:
public void move() {
moveX();
moveY();
}
public void moveX() {
if(xMove > 0) {
int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
}
} else if(xMove < 0) {
int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
}
}
}
public void moveY() {
if(yMove < 0) {
int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
}
}
if(yMove > 0) {
int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
}
}
}
protected boolean collisionWithTile(int x, int y) {
return room.getTile(x, y).isSolid();
}
but when I run it I get a NullPointerException with this StackTrace:
Exception in thread "Thread-0" java.lang.NullPointerException
at com.pixelandbitgames.entities.creature.Creature.collisionWithTile(Creature.java:69)
at com.pixelandbitgames.entities.creature.Creature.moveX(Creature.java:41)
at com.pixelandbitgames.entities.creature.Creature.move(Creature.java:27)
at com.pixelandbitgames.entities.creature.Player.tick(Player.java:19)
at com.pixelandbitgames.states.GameState.tick(GameState.java:23)
at com.pixelandbitgames.game.Game.tick(Game.java:72)
at com.pixelandbitgames.game.Game.run(Game.java:113)
at java.lang.Thread.run(Thread.java:748)
I do not see where this is coming from as collisionWithTile has all its parameters filled. If anyone can help me that would be appriciated. if anyone needs any thing else to help I will deliver. My other classes seem fine and it is just this one. This happens when I try to move. my code for Room.getTile is here:
public Tile getTile(int x, int y) {
if(x < 0 || y < 0 || x >= width || y >= height)
return Tile.floorTile;
Tile t = Tile.tiles[tiles[x][y]];
if(t == null)
return Tile.floorTile;
return t;
}
and this has no way to return null. Is solid is just a return true unless it is overridden.
public boolean isSolid() {
return false;
}
Either room is null, or room.getTile(x, y) returns null. You haven't pasted the relevant bits of those, making it impossible to give more information.

Java libgdx: limiting circular velocity (swinging entity)

I am currently working on a 2D side scroller and have implemented the techniques use in this article for a grapple hook, and it works really well. My problem is I want my player to be able to swing around the rope a little bit to gain a bit of momentum, but currently I can't stop the player from moving all the way up to 90 degrees either side. What techniques can be applied to force this limit?
I have tried using a separate player speed for swinging but this only slows the process down I can still swing up to 90 deg each side.
Here's my update function in the player
public void update(float dt){
//handle friction and air resistance
if(dx !=0){
if(touchingGround) {
// apply friction
if (dx > 0) {
dx -= retardation;
} else {
dx += retardation;
}
} else {
//applied air resistance
if (dx > 0) {
dx -= airResistance;
} else {
dx += airResistance;
}
}
}
// handle gravity
dy -= Constants.GRAVITY * dt;
if(dy < -terminalVelocity){
dy = -terminalVelocity;
}
/*
Handle Player movement
*/
if(right){
if(dx <= maxSpeed){
dx += acceleration;
}
dx = maxSpeed;
}
if(left){
if(dx <= -maxSpeed){
dx -= acceleration;
}
dx = -maxSpeed;
}
if(isGrappling){
//If we collide with something we need to stop grappling
if(hasCollided){
isGrappling = false;
} else {
// This algorithm from here:
// http://gamedev.stackexchange.com/questions/61596/player-rope-swing
float currentD = (float) Math.sqrt(((grappleX - x) * (grappleX - x)) + ((grappleY - y) * (grappleY - y)));
float prevX = getX(), prevY = getY();
if (currentD > grappleRadius) {
Vector2 hookPos = new Vector2(grappleX, grappleY);
Vector2 testPos = (new Vector2(x, y).sub(hookPos)).nor();
y = (hookPos.y + testPos.y * grappleRadius);
x = (hookPos.x + testPos.x * grappleRadius);
// s = d / t
dx += (x - prevX) / dt;
dy += (y - prevY) / dt;
}
}
}
/*
Collision Detection, handle last always!
*/
float oldX = getX(), oldY = getY();
boolean collisionX = false, collisionY = false;
// move on x
x += dx * dt;
// calculate the increment for step in #collidesLeft() and #collidesRight()
increment = collisionLayer.getTileWidth();
increment = getWidth() < increment ? getWidth() / 2 : increment / 2;
if(dx < 0) // going left
collisionX = collidesLeft();
else if(dx > 0) // going right
collisionX = collidesRight();
// react to x collision
if(collisionX) {
setX(oldX);
dx = 0;
}
// move on y
y += dy * dt;
// calculate the increment for step in #collidesBottom() and #collidesTop()
increment = collisionLayer.getTileHeight();
increment = getHeight() < increment ? getHeight() / 2 : increment / 2;
if(dy < 0) {
touchingGround = collisionY = collidesBottom();
// we can only jump 2 times before we have to touch the floor again
if(collisionY){
numberOfJumps = 2;
}
} else if(dy > 0) {
collisionY = collidesTop();
}
// react to y collision
if(collisionY) {
setY(oldY);
dy = 0;
}
hasCollided = collisionX || collisionY;
}
As I am not using any physics engine I chose to just emulate the physics by limiting the angle at which the player can apply force to the swing.
// check if angle permits movement
if(grappleAngle < Math.PI/9 && grappleAngle > -Math.PI/9) {
// handle momentum gaining on rope
if (right) {
dx += swingAcceleration * dt;
}
if (left) {
dx -= swingAcceleration * dt;
}
}

Image weirdness with slick2d graphics translation

I am making a game and I noticed whenever I use the graphics.translate function and after the translate this happens to the images.
Before Translation
After Translation
I was wondering if there is anyway to fix that or anyone else has the same issue. All these sprites are rendered from a spritesheet
EDIT: Translate code
public void translate(Graphics g, GameContainer container, int delta) {
g.translate(((container.getWidth() / 2) - this.x), ((container.getHeight() / 2) - this.y));
}
public void update(GameContainer container, int type){
if (type == 0) {
x = p.getX(); //p is the player
y = p.getY();
} else if (type == 1) {
x = player.x;
y = player.y;
}
if (offset) {
if (this.x - container.getWidth() / 2 < offsetMin[0]) {
x = offsetMin[0] + container.getWidth() / 2;
} else if (this.x + container.getWidth() / 2 > offsetMax[0]) {
x = offsetMax[0] - container.getWidth() / 2;
}
if (this.y - container.getHeight() / 2 < offsetMin[1]) {
y = offsetMin[1] + container.getHeight() / 2;
} else if (this.y + container.getHeight() > offsetMax[1]) {
y = offsetMax[1] - container.getHeight() / 2;
}
}
}
Try casting the x and y parameters for g.translate() to ints. That would eliminate any rounding errors where tiles don't end up on perfect pixel coords (IE 4, not 4.2).
Moved answer from comments to answer so it can be marked as accepted by OP

bilinear interpolation using java

I am trying to zoom an image twice using bilinear interpolation, but it's not working.It is giving errors like division by 0 , out of range value(the output color has exceeded 255). Here is my code :
int red1,green1,blue1;
int red2,green2,blue2;
int red3,green3,blue3;
int red4,green4,blue4;
int redf,greenf,bluef;
int x1,y1,x2,y2,x,y;
int a,b,c,d;
int var[][]=new int[4][4];
int inv[][]=new int[4][4];
//int cons[][]=new int[1][4];
//int col[][]=new int[1][4];
for(int i=0;i<height*2;i++)
{
//l=0;
for(int j=0;j<width*2;j++)
{
Color color=new Color(img.getRGB((int)(j/2),(int)(i/2)));
red=color.getRed();
green=color.getGreen();
blue=color.getBlue();
x=(int)(j/2);
y=(int)(i/2);
x1=(int)(j/2)+1;
y1=(int)(i/2)+1;
x2=(int)(j/2)-1;
y2=(int)(i/2)-1;
if(x1<0 || x1>=width || y1<0 || y1>=height)
{
//System.out.println("Yes");
red1=0;
green1=0;
blue1=0;
}
else
{
Color c1=new Color(img.getRGB(x1,y1));
red1=c1.getRed();
green1=c1.getGreen();
blue1=c1.getBlue();
}
if(x2<0 || x2>=width || y2<0 || y2>=height)
{
//System.out.println("Yes2");
red2=0;
green2=0;
blue2=0;
}
else
{
Color c2=new Color(img.getRGB(x2,y2));
red2=c2.getRed();
green2=c2.getGreen();
blue2=c2.getBlue();
}
if(x1<0 || x1>=width || y2<0 || y2>=height)
{
red3=0;
green3=0;
blue3=0;
}
else
{
Color c3=new Color(img.getRGB(x1,y2));
red3=c3.getRed();
green3=c3.getGreen();
blue3=c3.getBlue();
}
if(x2<0 || x2>=width || y1<0 || y1>=height)
{
red4=0;
green4=0;
blue4=0;
}
else
{
Color c4=new Color(img.getRGB(x2,y1));
red4=c4.getRed();
green4=c4.getGreen();
blue4=c4.getBlue();
}
if(x1<=0 || x1>width)
x1=0;
if(x2<=0 || x2>width)
x2=0;
if(y1<=0 || y1>height)
y1=0;
if(y2<=0 || y2>height)
y2=0;
var[0][0]=x1;
var[0][1]=y1;
var[0][2]=x1*y1;
var[0][3]=1;
var[1][0]=x2;
var[1][1]=y2;
var[1][2]=x2*y2;
var[1][3]=1;
var[2][0]=x1;
var[2][1]=y2;
var[2][2]=x1*y2;
var[2][3]=1;
var[3][0]=x2;
var[3][1]=y1;
var[3][2]=x2*y1;
var[3][3]=1;
inv=invert(var);
a=inv[0][0]*red1+inv[0][1]*red2+inv[0][2]*red3+inv[0][3]*red4;
b=inv[1][0]*red1+inv[1][1]*red2+inv[1][2]*red3+inv[1][3]*red4;
c=inv[2][0]*red1+inv[2][1]*red2+inv[2][2]*red3+inv[2][3]*red4;
d=inv[3][0]*red1+inv[3][1]*red2+inv[3][2]*red3+inv[3][3]*red4;
redf=a*x+b*y+c*x*y+d;
a=inv[0][0]*green1+inv[0][1]*green2+inv[0][2]*green3+inv[0][3]*green4;
b=inv[1][0]*green1+inv[1][1]*green2+inv[1][2]*green3+inv[1][3]*green4;
c=inv[2][0]*green1+inv[2][1]*green2+inv[2][2]*green3+inv[2][3]*green4;
d=inv[3][0]*green1+inv[3][1]*green2+inv[3][2]*green3+inv[3][3]*green4;
greenf=a*x+b*y+c*x*y+d;
a=inv[0][0]*blue1+inv[0][1]*blue2+inv[0][2]*blue3+inv[0][3]*blue4;
b=inv[1][0]*blue1+inv[1][1]*blue2+inv[1][2]*blue3+inv[1][3]*blue4;
c=inv[2][0]*blue1+inv[2][1]*blue2+inv[2][2]*blue3+inv[2][3]*blue4;
d=inv[3][0]*blue1+inv[3][1]*blue2+inv[3][2]*blue3+inv[3][3]*blue4;
bluef=a*x+b*y+c*x*y+d;
System.out.println(redf+" "+greenf+" "+bluef);//prints the value
Color nc=new Color(redf,greenf,bluef);
img1.setRGB(j, i, nc.getRGB());
}
}
Kindly help . I am trying to zoom by 2 , and using 4 neighbours(bilinear approach)
Sorry for being a bit late but for Bilinear Interpolation what you need to do is interpolation the results of two different interpolations. Basically it would look something like:
newX = x - 0.5
a1 = newX - (int) (newX)
newY = y - 0.5
a2 = newY - (int) (newY)
Red1 = getRed((int) (x - 0.5), (int) (y - 0.5))
Red2 = getRed((int) (x + 0.5), (int) (y - 0.5))
Red3 = getRed((int) (x - 0.5), (int) (y + 0.5))
Red4 = getRed((int) (x + 0.5), (int) (y + 0.5))
newRed = lerp(lerp(Red1, Red2, a1), lerp(Red3, Red4, a1), a2)
This could, of course be repeated for green and blue.
X and Y in this are source coords, so if you wanted to find the color at (3, 5) of your new image you would run this on (1.5, 2.5) aka your original coords divided by your scaling factor (2).
In case you don't know lerp() is generaly just
lerp(x, y, a)
return x * (1 - a) + y * a;
but if cycles really matter to you or you have a primitive FPU or any number of other special case the less precise
return a + f * (b - a);
can work.
*
Edit:
Since we're in java I've just used
(int)
to signify rounding down but it doesn't actually have to be an int there.

lwjgl converting mouseLook to keys

Hey guy's I'm working on a 3D project using lwjgl. At the moment the players camera view is moved by the mouse. Movement is done using the arrow keys & i'd like to convert the mouse view to w,a,s,d so you no longer have to use the mouse.
Why am I doing this? I've got an interface system that I can't use because the application currently grabs the mouse to move the view, so the player can not interact with the interfaces.
What I've got:
public void cameraView() {
if (Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX() * 0.8f * 0.16f;
float mouseDY = Mouse.getDY() * 0.8f * 0.16f;
if (rotation.y + mouseDX >= 360) {
rotation.y = rotation.y + mouseDX - 360;
} else if (rotation.y + mouseDX < 0) {
rotation.y = 360 - rotation.y + mouseDX;
} else {
rotation.y += mouseDX;
}
if (rotation.x - mouseDY >= -89 && rotation.x - mouseDY <= 89) {
rotation.x += -mouseDY;
} else if (rotation.x - mouseDY < -89) {
rotation.x = -89;
} else if (rotation.x - mouseDY > 89) {
rotation.x = 89;
}
}
}
If anyone can help me out that'd be awesome. Thanks in advance!
if(Keyboard.isKeyDown(Keyboard.KEY_A)) {rotation.y -= 1;}
if(Keyboard.isKeyDown(Keyboard.KEY_D)) {rotation.y += 1;}
This should do the same thing as Mouse.getDX()
Alternatively, you could use:
while(Keyboard.next()) {
if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
if(Keyboard.getEventKeyState()) {
Mouse.setGrabbed(!Mouse.isGrabbed());
Mouse.setCursorPosition(Display.getWidth()/2, Display.getHeight()/2);
}}}
if(Mouse.isGrabbed()) {
//Use mouse for moving
} else {
//Use mouse for interface
}

Categories