When I shoot in the position the character starts at the bullet goes in the right direction. But if I move to a different position the bullet does not go in the right direction. To clarify things mx is mouse x position, my is mouse y position, px is player x position, and py is player y position.
package Player;
import java.io.IOException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import Game.Main;
public class Bullet {
public double x = 0, y = 0;
public double sx, sy;
public Texture texture;
public Bullet(double mx, double my, double px, double py){
sx = mx-px;
sy = py-my;
x = px;
y = py;
sx = sx/Math.sqrt((mx-x)*(mx-x) + (my-y)*(my-y));
sy = sy/Math.sqrt((mx-x)*(mx-x) + (my-y)*(my-y));
}
public void logic(){
renderBullet();
x+=sx * 8;
y+=sy * 8;
}
public void renderBullet(){
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Color.white.bind();
texture.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2d(x,y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2d(10+x,y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2d(10+x,10+y);
GL11.glTexCoord2f(0,1);
GL11.glVertex2d(x,10+y);
GL11.glEnd();
}
}