0
votes

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

}

1
What type of game is this? 2D viewed from above? Is the player always centered on the screen? Are mx,my,px,py all pixel coordinates on the screen? With the little information you supplied nobody can answer the question...BDL
It is a 2d view from above game. The character is not always centered on the screen. Mx,my,px,py are all positions in the game.Max
@BDL I forgot too do the @ thingy.Max

1 Answers

0
votes

For some reason while in a different position at the start you have to multiply the sx and sy equation by a -1.

public Bullet(double mx, double my, double px, double py){
    if(x==640 && y== 320){
    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));

}
    if(x!=640 && y!= 320){
    sx = px-mx;
    sy = my-py;
    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));

}

}