我的子弹在错误的地方产卵

时间:2016-12-28 16:41:32

标签: java game-physics affinetransform projectile

在我的游戏中,玩家可以使用箭头键旋转。根据玩家旋转的角度,当子弹发射时,它会朝向玩家所面向的方向移动。

然而,那部分有效,子弹在错误的地方产卵。下面是两张图片,显示了产卵的差异。在第一张照片中,子弹在红点上产生。但是,旋转时点不会相应移动,这会阻止子弹正确产生。

注意:红点仅用于视觉效果(基于子弹的产生坐标)

the bullet spawns correctly when facing upwards

the bullet spawns incorrectly when facing every other direction

以下是相关代码:

Main中的KeyListener:

$obj2 = new FooObject(['a' => 'alpha']);
//Access Property
var_dump($obj2['a']); //alpha
var_dump($obj2->offsetGet('a'));//alpha
var_dump($obj2->a); //alpha

//Serialization
var_dump(serialize($obj));// string 'C:11:"ArrayObject":41:{x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}}' (length=65)
var_dump($obj->serialize());// string 'x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}'
var_dump(serialize($obj) === $obj->serialize());// false !!!

//Setting Properties
$obj2['b'] = 'beta'; //OK
$obj2->c = 'gamma'; //OK
var_dump($obj2);
/* OBJECT DUMP
object(FooObject)[14]
  private 'storage' (ArrayObject) =>
    array (size=3)
      'a' => string 'alpha' (length=5)
      'b' => string 'beta' (length=4)
      'c' => string 'gamma' (length=5)
 */

//Property validation as array
var_dump(isset($obj2['a']));//true
var_dump(isset($obj2['b']));//true
var_dump(isset($obj2['c']));//true
//Property validation as object
var_dump(isset($obj2->a));//true
var_dump(isset($obj2->b));//true
var_dump(isset($obj2->c));//true

//Typecasting
var_dump((array)$obj2);
/*
array (size=3)
  'a' => string 'alpha' (length=5)
  'b' => string 'beta' (length=4)
  'c' => string 'gamma' (length=5)
 */

播放器:

if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                //Creates bullets when the spacebar is pressed, as long as the user is not exceeding 3 at a time
                if(player.amountOfBullets() >= 0 && player.amountOfBullets() < 10)
                {
                player.setNB(player.getGunX(), player.getGunY(), player.getAngle());
                }
}

子弹:

public class Player {
private BufferedImage spriteSheet;

private double x;
private double y;
private int height;
private int width;

private int gunX;
private int gunY;


private BufferedImage[] sprites = new BufferedImage[13];
private BufferedImage currentImg;
private int frame;
private double angle = 0;

private ArrayList<Bullet> nb = new ArrayList<Bullet>(10); 

public Player(){
    try { 
        spriteSheet = ImageIO.read(getClass().getResource("/Sprites.png"));
    } catch (IOException e) { 
        System.err.println("Sprites3.png could not be found");
    }

    for (int i = 0; i < sprites.length; i++){
        sprites[i] = grabImage(1, i+1, 100);
    }
    currentImg = sprites[0];
    x = 10;
    y = 10;
    width = currentImg.getWidth();
    height = currentImg.getHeight();

    gunX = (int) x + width - 15;
    gunY = (int) y;
}

public BufferedImage grabImage(int col, int row, int length){
    BufferedImage img = spriteSheet.getSubimage(((col-1) * length), ((row-1) * length), length, length);
    return img;
}

public void paint(Graphics2D g2d){

    for(int i = 0; i < nb.size(); i++)
    {
        if(nb.get(i).getTime()*10 >= 15)
        {
            nb.set(0, null);
            nb.remove(nb.get(0));//removes bullets based on the time they are in game
        }
    }

    double rotationRequired = Math.toRadians (angle);
    double locationX = currentImg.getWidth() / 2;
    double locationY = currentImg.getHeight() / 2;
    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

    for (int i = 0; i < nb.size(); i++)
    {
        nb.get(i).move();//moves the bullets
        nb.get(i).paint(g2d);
    }

    // Drawing the rotated image at the required drawing locations
    g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
    //g2d.fillOval(x, y, 10, 10);
    g2d.setColor(Color.RED);
    int x = (int) this.x;
    int y = (int) this.y;
    g2d.fillOval(gunX, gunY, 10, 10);
}

public void update(){
    currentImg = sprites[frame];
}

public void move(double xM, double yM){
    if (yM == 5) xM = -5;
    else xM = 5;
    x += xM * Math.sin(Math.toRadians(angle));
    y += yM * Math.cos(Math.toRadians(angle));

    gunY += yM * Math.cos(Math.toRadians(angle));
    gunX += xM * Math.sin(Math.toRadians(angle));

    if (frame < 12){
        frame++;
    }
    else frame = 0;
}

public void turn(double angle){    
    this.angle += angle;
}

public int getX() {
    return (int) x;
}

public int getY(){
    return (int) y;
}

public int getGunX() {
    return gunX;
}

public int getGunY(){
    return gunY;
}

public int getHeight(){
    return height;
}

public int getWidth(){
    return width;
}

public void setNB(int x, int y, double angle)//sets position and movement of bullet
{
    if (nb.size() > 10)
    {
        nb.set(0, null);
        nb.remove(nb.get(0));//removes the bullets from the game when tried to exceed 3
    }
    if (nb.size() < 10)//only 3 shots allowed at a time
    {
        nb.add(new Bullet(x, y, angle));//adds bullets to list when a new one is shot
    }
}

public ArrayList<Bullet> getNB()//sets position and movement of bullet
{
    return nb;
}

public void removeNB(int i)//removes bullet from game when it hits an asteroid
{
    nb.set(i, null);
    nb.remove(nb.get(i));
}

public int getBulletX(int i)//gets bullet X coord
{
    return nb.get(i).getX();
}

public int getBulletY(int i)//gets bullet Y coord
{
    return nb.get(i).getY();
}

public int getBulletWidth(int i)//gets bullet width
{
    return nb.get(i).getWidth();
}

public int getBulletHeight(int i)//gets bullet height
{
    return nb.get(i).getHeight();
}

public int getBulletAngle(int i)//gets bullet angle
{
    return nb.get(i).getAngle();
}

public double getAngle() {
    return angle;
}

public int amountOfBullets() //checks amount of bullets in game
{
    return nb.size();
}
}

1 个答案:

答案 0 :(得分:1)

好的继续你的新油漆移动和转动方法 - 你还需要转换枪的点:

public void paint(Graphics g2d){
    // Drawing the rotated image at the required drawing locations
    if(op!=null) g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
    else g2d.drawImage(currentImg, (int)x, (int)y, null);
    g2d.setColor(Color.blue);
    g2d.fillOval(gunX, gunY, 10, 10);
}


public void move(double xM, double yM){
    x += xM * Math.cos(Math.toRadians(angle));
    y += yM * Math.sin(Math.toRadians(angle));

    gunY += yM * Math.sin(Math.toRadians(angle));
    gunX += xM * Math.cos(Math.toRadians(angle));

    repaint();
}


public void turn(double angle){    
    this.angle += angle;
    double rotationRequired = Math.toRadians (this.angle);
    double locationX = currentImg.getWidth() / 2;
    double locationY = currentImg.getHeight() / 2;
    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    int gx= currentImg.getWidth() - 15;
    int gy=0;
    Point pt1=new Point(gx, gy), pt2=new Point();
    tx.transform(pt1, pt2);
    gunX=(int)x+pt2.x; gunY=(int)y+pt2.y;
    repaint();
}

你需要:

全局变量

AffineTransformOp op;
播放器中的