如何在不牺牲帧的情况下提高 Java 处理应用程序中对象的速度

问题描述 投票:0回答:1

好吧,我正在学习如何用java制作游戏和其他东西,我正在使用处理,因为我试图生成一个在发射时会不断移动的椭圆,我的动画效果是在draw()函数中我检查条件当它应该被触发以及当条件为真时,我绘制椭圆并不断更改它的新坐标。目前我的游戏以 30 fps 运行,当我小幅度更改椭圆坐标时,它移动缓慢,但由于球将生成的后续坐标非常接近,因此它将生成接近的动画,从而提供缓慢但平滑的动画,而不是使其更快我必须增加坐标,以提供快速但断断续续的动画。我很困惑我们如何在不影响动画质量的情况下让它变得更快。

我的结构看起来像这样

public float speed = 100;
public static final int FPS = 30;

public void draw() {
  // conditions to check
  if (tank.isTankfired()) {
    removeprojectile(tank.projectilestartX(), tank.projectilestartY());
    fill(255, 0, 0); // Set the fill color to red
    ellipse(tank.projectilestartX(), tank.projectilestartY(), 10, 10); 
    tank.setprojectilestartX(tank.projectilestartX()+speed/FPS);
    tank.setprojectilestartY(tank.projectilestartY()-speed/FPS);
  }
}

使用这种方法,它会不断地改变 x 和 y 坐标,如果我增加速度,它会使得下一个椭圆之间的距离会被绘制得更多,而不是更快地生成它,并且由于程序以 30 fps 运行,所以现在看起来不稳定。我是新的学习处理者,因此任何有关如何实际处理它的指导将不胜感激。

java performance processing game-physics draw
1个回答
0
投票

Star
示例代码中,他们使用
frameCount / someDouble
。我的猜测是你应该能够做类似的事情。

示例代码

/**
 * Star
 * 
 * The star() function created for this example is capable of drawing a
 * wide range of different forms. Try placing different numbers into the 
 * star() function calls within draw() to explore. 
 */

void setup() {
  size(640, 360);
}

void draw() {
  background(102);
  
  pushMatrix();
  translate(width*0.2, height*0.5);
  rotate(frameCount / 200.0);
  star(0, 0, 5, 70, 3); 
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 400.0);
  star(0, 0, 80, 100, 40); 
  popMatrix();
  
  pushMatrix();
  translate(width*0.8, height*0.5);
  rotate(frameCount / -100.0);
  star(0, 0, 30, 70, 5); 
  popMatrix();
}

void star(float x, float y, float radius1, float radius2, int npoints) {
  float angle = TWO_PI / npoints;
  float halfAngle = angle/2.0;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius2;
    float sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}

首次运行值:

rotate(frameCount / 400.0);
-
rotate(frameCount / 400.0);
-
rotate(frameCount / -100.0);

第二次运行值:

rotate(frameCount / 20.0);
-
rotate(frameCount / 75.0);
-
rotate(frameCount / -50.0);

© www.soinside.com 2019 - 2024. All rights reserved.