Unknown NullPointerException 10 ignore> [duplicate] - java

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.

Related

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

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();
}
}
}

Moving the player without touching it

I am working on a simple 2D infinite runner game. I am trying to get the player to move in any direction without needing to touch the player specifically. I want the game to find where the user touches and then allow the user to control the player from there. At the moment the player moves but only when the user is touching the player directly which makes it difficult for the user to see the actual player when playing and trying to dodge obstacles.
Here is the code I currently have:
public boolean recieveTouch(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int action = event.getAction();
int x = (int) event.getX(); // or getRawX();
int y = (int) event.getY();
int h2 = bitmap2.getHeight();
int w2 = bitmap2.getWidth();
float xRatio2 = (Constants.SCREEN_WIDTH / 2)-( w2 / 2);
float yRatio2 = (Constants.SCREEN_HEIGHT / 2)-( h2 / 2) + (Constants.SCREEN_WIDTH/4 + (Constants.SCREEN_WIDTH/6)- Constants.SCREEN_HEIGHT/10);
int h55 = bitmap55.getHeight();
int w55 = bitmap55.getWidth();
int h66 = bitmap66.getHeight();
int w66 = bitmap66.getWidth();
float xRatio55 = ((Constants.SCREEN_WIDTH / 4)-( w55 / 4));
float yRatio55 = (((Constants.SCREEN_HEIGHT / 2) + (h55 / 2)) + (Constants.SCREEN_HEIGHT/6));
float xRatio66 = ((Constants.SCREEN_WIDTH / 2)+( w66 / 4) + (Constants.SCREEN_HEIGHT/24));
float yRatio66 = (((Constants.SCREEN_HEIGHT / 2) + (h66 / 2)) + (Constants.SCREEN_HEIGHT/6));
//help
int h5 = bitmap4.getHeight();
int w5 = bitmap4.getWidth();
float xRatio5 = ((Constants.SCREEN_WIDTH / 4) - (w5 / 1));
float yRatio5 = (((Constants.SCREEN_HEIGHT / 8) - (h5 / 1)));
long pressTime = System.currentTimeMillis();
if (!gameOver && player.getRectangle().contains((int) event.getX(), (int) event.getY()))
movingPlayer = true;
if (gameOver && System.currentTimeMillis() - gameOverTime >= 0) {
if (x >= xRatio2 && x < (xRatio2 + bitmap2.getWidth())
&& y >= yRatio2 && y < (yRatio2 + bitmap2.getHeight())) {
if (GamePanel.Ad1 == 1) {
MainActivity.click.start();
MainActivity.mAd = MobileAds.getRewardedVideoAdInstance(mActivityRef.get());
loadAd();
MainActivity.mAd.show();
//Appodeal.show(mActivityRef.get(), Appodeal.REWARDED_VIDEO);
}
}
if (x >= xRatio55 && x < (xRatio55 + bitmap55.getWidth())
&& y >= yRatio55 && y < (yRatio55 + bitmap55.getHeight())) {
MainActivity.click.start();
reset();
gameOver = false;
orientationData.newGame();
}
if (x >= xRatio66 && x < (xRatio66 + bitmap66.getWidth())
&& y >= yRatio66 && y < (yRatio66 + bitmap66.getHeight())) {
MainActivity.click.start();
SceneManager.ACTIVE_SCENE = 4;
MainActivity.mInterstitialAd.loadAd((new AdRequest.Builder().build()));
MainActivity.mInterstitialAd.show();
//Appodeal.show(mActivityRef.get(), Appodeal.INTERSTITIAL);
if (GamePanel.YelowBird == 4) {
GamePanel.YelowBird = 2;
GamePanel.RedBird = 1;
}
reset();
gameOver = false;
}
if (x >= xRatio5 && x < (xRatio5 + bitmap4.getWidth())
&& y >= yRatio5 && y < (yRatio5 + bitmap4.getHeight())) {
if (GamePanel.Help == 0) {
GamePanel.Help = 1;
if (GamePanel.Help == 1) {
MainActivity.click.start();
orientationData.register();
}
} else if (GamePanel.Help == 1) {
GamePanel.Help = 0;
if (GamePanel.Help == 0) {
MainActivity.click.start();
orientationData.pause();
}
}
} return true;
}
break;
case MotionEvent.ACTION_MOVE:
if (GamePanel.NinjaBird == 4) {
if (!gameOver)
playerPoint.set((int) event.getX(), (int) event.getY());
} else {
if (!gameOver && movingPlayer)
playerPoint.set((int) event.getX(), (int) event.getY());
}
break;
case MotionEvent.ACTION_UP:
movingPlayer = false;
break;
}
return false;
}
if anyone else wants to do a similar thing:
i changed the following line
from this:
if (!gameOver && movingPlayer)
playerPoint.set((int) event.getX(), (int) event.getY());
to this:
if (!gameOver && movingPlayer)
playerPoint.set((int) event.getX(), (int) event.getY()+200);
so you move the player by touching 200 pixels under it.

How to make a Bresenham's line algorithm?

I wrote a small program, that draws a line from user inputs x1,y1 and x2,y2. For some reason line only works while horizontal, but once line should be vertical, it just shows me a black pixel not a line. I have checked everything and I am pretty sure that I got the algorithm right, but problem is still here.
int x1 = Integer.parseInt(this.jTextFieldX1.getText());
int y1 = Integer.parseInt(this.jTextFieldY1.getText());
int x2 = Integer.parseInt(this.jTextFieldX2.getText());
int y2 = Integer.parseInt(this.jTextFieldY2.getText());
int xi = x2 > x1 ? 1:-1;
int yi = y2 > y1 ? 1:-1;
int dx = Math.abs(x2-x1);
int dy = Math.abs(y2-y1);
int xn= x1;
int yn= y1;
int pn;
canvas.showBlackPixel(xn,yn);
if (dx > dy)
{
pn= 2*dy - dx;
while (xn != x2)
{
if (pn >0)
{
xn=xn + xi;
yn=yn + yi;
pn=pn + 2*dy - 2*dx;
}
else
{
xn = xn + xi;
pn = pn+ 2*dy;
}
canvas.showBlackPixel(xn,yn);
}
{
if (dy > dx)
{
pn= 2*dx - dy;
while (yn != y2)
{
if (pn > 0)
{
xn=xn + xi;
yn=yn + yi;
pn=pn + 2*dx - 2*dy;
}
else
{
yn = yn + yi;
pn = pn + 2*dx;
}
canvas.showBlackPixel(xn,yn);
}
}
}
}
}
How could I fix it?
The problem is with the termination of your statements, which is to say your braces {} are mixed up a little. To be specific, cut down to the relevant bits your code is:
if (dx > dy)
{
...
if (dy > dx)
{
...
}
}
If you fix your braces, then it looks like it should work better.
if (dx > dy)
{
...
}
if (dy > dx)
{
...
}
You'll still have an issue when dx == dy though.

MouseEvent getPoint(), Math.asin(), and coordinates problems

I'm having trouble with the MouseEvent getPoint() method, the Math.asin() method, and just in general with coordinates. I am trying to create multiple objects called "Laser" to get fired from the "playerRobot". A rectangle is created around the playerRobot and the tempR.getCenterX and tempR.getCenterY are used to construct the point at the center of the playerRobot's location. The MouseEvent getPoint() is used to get the location of the mouse relative to the focused component. In the Laser class, two methods called getPointX(int y) and getPointY(int x) return a point with the respective x and y values corresponding to the given y or x value. I.e., getPointX(int y) returns a point with y and its corresponding x value. These methods do this by finding the linear equation between the given point of the playerRobot and the mouse. Then, the Laser is supposed to be drawn at the playerRobot point and redrawn to follow its linear equation to the mouse's point and continue on the linear equation until it reaches the borders of the component. However, depending on the angle at which the Laser is fired, it does not seem to always do this. Also, sometimes the Laser does not fire at all, sometimes it fires in the wrong direction or at the wrong angle, and sometimes it is longer than 50 pixels, which is always supposed to be the length of the Laser.
TL;DR: The Laser does not always fire from the playerRobot and does not always follow the linear line to the mouse's point, sometimes fires in the wrong direction or at the wrong angle, and sometimes is longer than 50 pixels.
Here is the code in the GameCanvas class that creates a new Laser object, followed by the code in the Laser class:
GameCanvas:
package application;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.shape.Line;
import javax.swing.*;
public class GameCanvas extends JPanel implements ActionListener, KeyListener, MouseListener {
Timer t = new Timer(5, this);
double x = 0, y = 0, velX = 0, velY = 0;
boolean keyPress = false;
int code = 0;
long time = 1;
int currAIDelete = -1;
int currPlayerDelete = -1;
Graphics gr;
Point mousePoint = new Point();
AffineTransform oldTransform = new AffineTransform();
public GameCanvas() {
t.start();
addKeyListener(this);
addMouseListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
if(GameGUI.startScreen != null) {x = GameGUI.startScreen.imageLocationList.get(4).x; y = GameGUI.startScreen.imageLocationList.get(4).y; velX = 0; velY = 0;}
}
public void paintComponent(Graphics g) {
gr = g;
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(!GameGUI.getGameOver()) {
if(g2 != null) {
g2.drawImage(new javax.swing.ImageIcon(getClass().getResource(GameGUI.startScreen.imageList.get(7))).getImage(), (int) (x + 0.5), (int) (y + 0.5 + 64), GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(7).width, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(7).height, this);
g2.drawImage(new javax.swing.ImageIcon(getClass().getResource(GameGUI.startScreen.imageList.get(6))).getImage(), (int) (x + 0.5), (int) (y + 0.5 + 16), GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(6).width, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(6).height, this);
g2.drawImage(new javax.swing.ImageIcon(getClass().getResource(GameGUI.startScreen.imageList.get(5))).getImage(), (int) (x + 0.5), (int) (y + 0.5 + 16), GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(5).width, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(5).height, this);
g2.drawImage(new javax.swing.ImageIcon(getClass().getResource(GameGUI.startScreen.imageList.get(4))).getImage(), (int) (x + 0.5), (int) (y + 0.5), GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(4).width, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(4).height, this);
for(int i = 3; i >= 0; i--)
g2.drawImage(new javax.swing.ImageIcon(getClass().getResource(GameGUI.startScreen.imageList.get(i))).getImage(), GameGUI.startScreen.imageLocationList.get(i).x, GameGUI.startScreen.imageLocationList.get(i).y, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(i).width, GameGUI.startScreen.DEFAULT_SCALED_IMAGE_SIZE_LIST.get(i).height, this);
Color tempColor = g2.getColor();
g2.setColor(tempColor);
GameGUI.startScreen.healthBar();
tempColor = g2.getColor();
GameGUI.startScreen.healthBarGlyphVector = new Font("Tahoma", 0, 11).createGlyphVector(new FontRenderContext(null, true, false), String.valueOf(GameGUI.startScreen.playerHealth));
g2.setColor(GameGUI.startScreen.playerHealthBarColor);
g2.drawGlyphVector(GameGUI.startScreen.healthBarGlyphVector, (int) (x + 40 - GameGUI.startScreen.healthBarGlyphVector.getNumGlyphs() * (double) GameGUI.startScreen.healthBarGlyphVector.getFont().getSize() / 2 + 0.5), (long) (y + 0.5) - 20);
GameGUI.startScreen.playerHealthBar.setLocation((int) (x + 0.5), (int) (y + 0.5) - 10);
g2.draw(GameGUI.startScreen.playerHealthBar);
g2.fill(GameGUI.startScreen.playerHealthBar);
GameGUI.startScreen.healthBarGlyphVector = new Font("Tahoma", 0, 11).createGlyphVector(new FontRenderContext(null, true, false), String.valueOf(GameGUI.startScreen.aiHealth));
g2.setColor(GameGUI.startScreen.aiHealthBarColor);
g2.drawGlyphVector(GameGUI.startScreen.healthBarGlyphVector, (int) (GameGUI.gameResources.getAIRobot().getLocation().get(0).x + 40 - GameGUI.startScreen.healthBarGlyphVector.getNumGlyphs() * (double) GameGUI.startScreen.healthBarGlyphVector.getFont().getSize() / 2 + 0.5), GameGUI.gameResources.getAIRobot().getLocation().get(0).y - 20);
GameGUI.startScreen.aiHealthBar.setLocation(GameGUI.gameResources.getAIRobot().getLocation().get(0).x, GameGUI.gameResources.getAIRobot().getLocation().get(0).y - 10);
g2.draw(GameGUI.startScreen.aiHealthBar);
g2.fill(GameGUI.startScreen.aiHealthBar);
g2.setColor(tempColor);
if(GameGUI.gameResources.playerLaserList != null) {
for(int i = GameGUI.gameResources.playerLaserList.size() - 1; i >= 0; i--) {
tempColor = g2.getColor();
oldTransform = g2.getTransform();
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(10));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine((int) (GameGUI.gameResources.playerLaserList.get(i).getCurrentLine().getStartX() + 0.5), (int) (GameGUI.gameResources.playerLaserList.get(i).getCurrentLine().getStartY() + 0.5), (int) (GameGUI.gameResources.playerLaserList.get(i).getCurrentLine().getEndX() + 0.5), (int) (GameGUI.gameResources.playerLaserList.get(i).getCurrentLine().getEndY() + 0.5));
g2.setTransform(oldTransform);
g2.setColor(tempColor);
}
}
if(GameGUI.gameResources.aiLaserList != null) {
for(int i = GameGUI.gameResources.aiLaserList.size() - 1; i >= 0; i--) {
tempColor = g2.getColor();
oldTransform = g2.getTransform();
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(10));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine((int) (GameGUI.gameResources.aiLaserList.get(i).getCurrentLine().getStartX() + 0.5), (int) (GameGUI.gameResources.aiLaserList.get(i).getCurrentLine().getStartY() + 0.5), (int) (GameGUI.gameResources.aiLaserList.get(i).getCurrentLine().getEndX() + 0.5), (int) (GameGUI.gameResources.aiLaserList.get(i).getCurrentLine().getEndY() + 0.5));
g2.setTransform(oldTransform);
g2.setColor(tempColor);
}
}
}
}
else try {
if(GameGUI.startScreen.checkGameOver() && GameGUI.startScreen.getGameOverVector() != null) {
g2.drawGlyphVector(GameGUI.startScreen.getGameOverVector(), (int) (380 - GameGUI.startScreen.getGameOverVector().getNumGlyphs() * (double) GameGUI.startScreen.healthBarGlyphVector.getFont().getSize() / 2 + 0.5), 250 - 29);
GameGUI.startScreen.imageList = new ArrayList <String> (Arrays.asList());
}
} catch (GameResourceException ex) {
Logger.getLogger(GameCanvas.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void actionPerformed(ActionEvent e) {
if((code == KeyEvent.VK_UP || code == KeyEvent.VK_DOWN || code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_W || code == KeyEvent.VK_S || code == KeyEvent.VK_A || code == KeyEvent.VK_D) && keyPress && x >= 0 && x <= 680 && y >= 0 && y <= 400) {
x += velX;
y += velY;
GameGUI.gameResources.getPlayerRobot().setLocation(new Point((int) (x + 0.5), (int) (y + 0.5)), new Point((int) (x + 0.5 + 37), (int) (y + 0.5 + 99)));
}
else {
if(x < 0) x++;
if(x > 680) x--;
if(y < 0) y++;
if(y > 400) y--;
}
if(GameGUI.gameResources.playerLaserList != null) {
for(int i = GameGUI.gameResources.playerLaserList.size() - 1; i >= 0; i--) {
Rectangle tempRectAI = new Rectangle(GameGUI.gameResources.getAIRobot().getLocation().get(0).getLocation().x, GameGUI.gameResources.getAIRobot().getLocation().get(0).getLocation().y, GameGUI.gameResources.getAIRobot().getLocation().get(1).getLocation().x, GameGUI.gameResources.getAIRobot().getLocation().get(1).getLocation().y);
Line line = GameGUI.gameResources.playerLaserList.get(i).getCurrentLine();
if(GameGUI.gameResources.playerLaserList.get(i).getLocation().x + 50 < 0 || GameGUI.gameResources.playerLaserList.get(i).getLocation().x > 762 || GameGUI.gameResources.playerLaserList.get(i).getLocation().y + 50 < 0 || GameGUI.gameResources.playerLaserList.get(i).getLocation().y > 500 || tempRectAI.intersectsLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY()))
GameGUI.gameResources.playerLaserList.remove(i);
else {
GameGUI.gameResources.playerLaserList.get(i).move(time);
}
}
}
if(GameGUI.gameResources.aiLaserList != null) {
for(int i = GameGUI.gameResources.aiLaserList.size() - 1; i >= 0; i--) {
Rectangle tempRectPlayer = new Rectangle(GameGUI.gameResources.getPlayerRobot().getLocation().get(0).getLocation().x, GameGUI.gameResources.getPlayerRobot().getLocation().get(0).getLocation().y, GameGUI.gameResources.getPlayerRobot().getLocation().get(1).getLocation().x, GameGUI.gameResources.getPlayerRobot().getLocation().get(1).getLocation().y);
Line line = GameGUI.gameResources.aiLaserList.get(i).getCurrentLine();
if(GameGUI.gameResources.aiLaserList.get(i).getLocation().x + 50 < 0 || GameGUI.gameResources.aiLaserList.get(i).getLocation().x > 762 || GameGUI.gameResources.aiLaserList.get(i).getLocation().y + 50 < 0 || GameGUI.gameResources.aiLaserList.get(i).getLocation().y > 500 || tempRectPlayer.intersectsLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY()))
GameGUI.gameResources.aiLaserList.remove(i);
else {
GameGUI.gameResources.aiLaserList.get(i).move(time);
}
}
}
repaint();
}
public void up() {
velY = -1.5;
velX = 0;
}
public void down() {
velY = 1.5;
velX = 0;
}
public void left() {
velX = -1.5;
velY = 0;
}
public void right() {
velX = 1.5;
velY = 0;
}
public void keyPressed(KeyEvent e) {
keyPress = true;
code = e.getKeyCode();
if(code == KeyEvent.VK_UP || code == KeyEvent.VK_W) {
up();
}
if(code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S) {
down();
}
if(code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D) {
right();
}
if(code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A) {
left();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
keyPress = false;
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent evt) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent evt) {
mousePoint = evt.getPoint();
if(GameGUI.getGameOver()) GameGUI.gameResources.resetForNextLevel();
else {
GameGUI.gameResources.getPlayerRobot().setLocation(new Point((int) (x + 0.5), (int) (y + 0.5)), new Point((int) (x + 0.5 + 37), (int) (y + 0.5 + 99)));
Rectangle tempR = new Rectangle(GameGUI.gameResources.getPlayerRobot().getLocation().get(0).x, GameGUI.gameResources.getPlayerRobot().getLocation().get(0).y, GameGUI.gameResources.getPlayerRobot().getLocation().get(1).x, GameGUI.gameResources.getPlayerRobot().getLocation().get(1).y);
GameGUI.gameResources.playerLaserList.add(new Laser(new Point((int) (tempR.getCenterX() + 0.5), (int) (tempR.getCenterY() + 0.5)), evt.getPoint(), GameGUI.gameResources));
}
}
}
Laser:
package application;
import java.awt.Point;
import javafx.scene.shape.Line;
public class Laser {
private Point point1;
private Point point2;
private Point currentPoint;
private double angle;
private Line laserLine;
private Point farEnd;
private Point farStart;
private double x1, x2, y1, y2, m, y3, x3;
private GameResources gameResources;
public Laser(Point tempPoint1, Point tempPoint2, GameResources tempGR) {
gameResources = tempGR;
currentPoint = point1 = tempPoint1;
point2 = tempPoint2;
double leg1 = point2.x - point1.x;
double leg2 = point2.y - point1.y;
double hyp = Math.sqrt(Math.pow(leg1, 2) + Math.pow(leg2, 2));
angle = Math.asin(leg2 / hyp);
if(angle < 0) angle = 2 * Math.abs(angle);
farEnd = getPointX(460);
farStart = getPointX(0);
laserLine = new Line(farStart.x, farStart.y, farEnd.x, farStart.y);
x1 = tempPoint1.x;
y1 = tempPoint1.y;
x2 = tempPoint2.x;
y2 = tempPoint2.y;
m = (y2 - y1) / (x2 - x1);
x3 = 0;
y3 = 0;
}
public Point getInitialLocation() {
return point1;
}
public Point getLocation() {
return currentPoint;
}
public double getAngle() {
return angle;
}
public Line getLine() {
return laserLine;
}
public Line getCurrentLine() {
Line currentLine = new Line();
if(getAngle() < Math.PI && getAngle() > 0)
currentLine = new Line((int) (currentPoint.x + 0.5), (int) (currentPoint.y + 0.5), (int) (getPointX((int) (currentPoint.y + 0.5) + (int) (Math.sin(angle) * 50 + 0.5)).x + 0.5), (int) (currentPoint.y + 0.5) + (int) (Math.sin(angle) * 50 + 0.5));
else if(getAngle() > Math.PI && getAngle() < 2 * Math.PI)
currentLine = new Line((int) (currentPoint.x + 0.5), (int) (currentPoint.y + 0.5), (int) (getPointX((int) (currentPoint.y + 0.5) - (int) (Math.sin(angle) * 50 + 0.5)).x + 0.5), (int) (currentPoint.y + 0.5) - (int) (Math.sin(angle) * 50 + 0.5));
System.out.println(currentLine.getStartX() + " " + currentLine.getStartY() + " " + point1);
return currentLine;
}
public void setLocation(Point tempLocation) {
currentPoint = tempLocation;
}
public Point getPointY(int x) {
if(x1 == x2 && y1 != y2) return null;
else if(m < 0) y3 = ((y2 - y1) / (x2 - x1)) * (double) x - (x2 * ((y2 - y1) / (x2 - x1)) - y2);
else if(m > 0) y3 = ((y2 - y1) / (x2 - x1)) * (double) x + (x2 * ((y2 - y1) / (x2 - x1)) - y2);
else if(m == 0) y3 = ((y2 - y1) / (x2 - x1)) * (double) x;
return new Point(x, (int) (y3 + 0.5));
}
public Point getPointX(int y) {
if(x1 == x2 && y1 != y2) return new Point((int) (x1 + 0.5), y);
else if(m < 0) x3 = ((double) y + (x2 * ((y2 - y1) / (x2 - x1)) - y2)) / ((y2 - y1) / (x2 - x1));
else if(m > 0) x3 = ((double) y - (x2 * ((y2 - y1) / (x2 - x1)) - y2)) / ((y2 - y1) / (x2 - x1));
else if(m == 0) x3 = (double) y / (((y2 - y1) / (x2 - x1)));
return new Point((int) (x3 + 0.5), (int) (y + 0.5));
}
public Line move(double time) {
//old algorithm
/*double x = 0, y = 0;
if(y2 > y1) {
y = currentPoint.y + gameResources.getLaserSpeed() * time;
currentPoint = getPointX((int) y);
}
else if(y2 < y1) {
y = currentPoint.y - gameResources.getLaserSpeed() * time;
currentPoint = getPointX((int) y);
}
else if(y2 == y1 && x2 > x1) {
x = currentPoint.x + gameResources.getLaserSpeed() * time;
currentPoint = getPointY((int) x);
}
else if(y2 == y1 && x2 < x1) {
x = currentPoint.x - gameResources.getLaserSpeed() * time;
currentPoint = getPointY((int) x);
}*/
//new algorithm
if(getAngle() < Math.PI / 2 && getAngle() > 0) {
System.out.println("(1) First quad");
setLocation(getPointY((int) (getLocation().x + 1 + 0.5)));
}
else if(getAngle() < Math.PI && getAngle() > Math.PI / 2) {
System.out.println("(2) Second quad");
setLocation(getPointY((int) (getLocation().x - 1 - 0.5)));
}
else if(getAngle() > Math.PI && getAngle() < 3 * Math.PI / 2) {
System.out.println("(3) Third quad");
setLocation(getPointY((int) (getLocation().x - 1 - 0.5)));
}
else if(getAngle() > 3 * Math.PI / 2 && getAngle() < 2 * Math.PI) {
System.out.println("(4) Fourth quad");
setLocation(getPointY((int) (getLocation().x + 1 + 0.5)));
}
else if(getAngle() == 2 * Math.PI || getAngle() == 0)
setLocation(new Point((int) (getLocation().x + 1 + 0.5), (int) (getLocation().y + 0.5)));
else if(getAngle() == Math.PI)
setLocation(new Point((int) (getLocation().x - 1 - 0.5), (int) (getLocation().y + 0.5)));
else if(getAngle() == Math.PI / 2)
setLocation(new Point((int) (getLocation().x + 0.5), (int) (getLocation().y + 1 + 0.5)));
else if(getAngle() == 3 * Math.PI / 2)
setLocation(new Point((int) (getLocation().x + 0.5), (int) (getLocation().y - 1 - 0.5)));
return getCurrentLine();
}
public void setGameResources(GameResources tempGR) {
gameResources = tempGR;
}
}

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

Categories