我正在处理中的2D游戏。
每个游戏循环:
因此,基本上,您总是以与鼠标到播放器(屏幕中心)的距离成比例的速度向鼠标移动。
到目前为止,我的所有工作原理都可以正常工作,只是我的世界中的物体绘制方式存在一个错误。当我移动时,物品会根据方向而略微移动,但它们的动作很平滑,就好像它与我的速度有关。它看起来非常像拖曳效果。
我添加了一个世界边界,以及世界上一些随机的圆圈。边框似乎停留在正确的位置,但是如果我沿与墙相反的方向移动,则边缘附近的圆圈可能会越过边缘。
任何想法可能是什么原因造成的?这是相关的代码(对不起,有一点点..)
import java.util.ArrayList;
//constants
int ROCKET_WIDTH = 32;
int ROCKET_HEIGHT = 52;
float PLAYER_MAX_ACC = 0.4;
float PLAYER_MAX_VEL = 12;
float MOVEMENT_RESISTANCE = 0.05;
//global variables
Rocket player;
ArrayList<PVector> balls;
float offsetX;
float offsetY;
void setup()
{
size(800,600);
frameRate(30);
background(0);
player = new Rocket();
balls = new ArrayList<PVector>();
offsetX = width/2 - player.pos.x;
offsetY = height/2 - player.pos.y;
for (int i = 0;i < 100;i++)
{
PVector vec = new PVector(random(-width,width*2),random(-height,height*2));
balls.add(vec);
}
}
void draw()
{
background(0);
drawWorldEdges();
updateMechanics();
for (PVector b:balls)
{
ellipseMode(CENTER);
ellipse(b.x + offsetX,b.y + offsetY,10,10);
}
drawPlayer();
}
void drawWorldEdges()
{
//top,right,bottom,left
strokeWeight(1);
stroke(255);
line(-width + offsetX,-height + offsetY,(width * 2) + offsetX,-height +offsetY);
line((width*2) + offsetX,-height + offsetY,(width*2)+offsetX,(height*2)+offsetY);
line(-width+offsetX,(height*2)+offsetY,(width*2)+offsetX,(height*2)+offsetY);
line(-width+offsetX,-height+offsetY,-width+offsetX,(height*2)+offsetY);
}
void updateMechanics()
{
offsetX = width/2 -player.pos.x;
offsetY = height/2 - player.pos.y;
updateFacing();
getMoveInput();
updatePhysics();
}
void updatePhysics()
{
if (player.pos.x < width*2 && player.pos.x >= -width)
{
player.pos.x += player.vel.x;
}
if (player.pos.y < height*2 && player.pos.y >= -height)
{
player.pos.y += player.vel.y;
}
player.vel.x += -player.vel.x * MOVEMENT_RESISTANCE;
player.vel.y += -player.vel.y * MOVEMENT_RESISTANCE;
}
void getMoveInput()
{
PVector vel = new PVector(mouseX - width/2,mouseY - height/2);
float distMult = dist(mouseX,mouseY,width/2,height/2)/(Math.min(width,height) / 2);
vel = vel.normalize().setMag(PLAYER_MAX_ACC * distMult);
PVector newVel = new PVector(player.vel.x,player.vel.y);
newVel.add(vel);
if (newVel.mag() <= PLAYER_MAX_VEL)
{
player.vel.add(vel);
}
}
void updateFacing()
{
float x = mouseX;
if (x == 0)
{
x = 0.1;
}
player.facing = PI/2 + atan((mouseY-(height/2))/(x-(width/2)));
if (x < width/2)
{
player.facing += PI;
}
}
void drawPlayer()
{
pushMatrix();
translate(width/2,height/2);
rotate(player.facing);
stroke(255);
strokeWeight(2);
fill(127);
rect(-ROCKET_WIDTH/4,0-ROCKET_HEIGHT/2,ROCKET_WIDTH/2,ROCKET_HEIGHT*0.75);
rect(-ROCKET_WIDTH/2,0,ROCKET_WIDTH/4,ROCKET_HEIGHT*0.5);
rect(ROCKET_WIDTH/4,0,ROCKET_WIDTH/4,ROCKET_HEIGHT*0.5);
popMatrix();
}
火箭班:
class Rocket
{
PVector pos;
PVector vel;
float facing; //radians
boolean pressingW,pressingA,pressingS,pressingD,mouseLClicked = false;
public Rocket()
{
pos = new PVector(0,0);
vel = new PVector(0,0);
facing = 0;
}
public Rocket(int x,int y)
{
pos = new PVector(x,y);
vel = new PVector(0,0);
facing = 0;
}
}
将来,请尝试发布MCVE,而不是整个项目。将其缩小到尽可能少的代码行。就您而言,您只需要显示两个圆圈,即可删除任何其他代码,例如显示飞船。
话虽如此,您的问题是由多种因素引起的:
PVector
,并将其存储为float
值。offsetX
和offsetY
也是float
价值观。int
值!(半个像素实际上没有任何意义。)发生的情况是,您的起点移动的间隔略有不同,您的眼睛察觉到它是零星的运动。这是由于值在不同时间float
达到下一个int
值而引起的。
假设您有两个职位:一个在[0, 10]
,另一个在[.6, 20]
。最初,它们具有相同的像素X值0
,因为像素始终是int
值。现在,假设您xOffset
是.5
,然后将其添加到您的职位中以获得[.5, 10]
和[1.1, 20]
。现在,他们有不同的像素的X值0
和1
,这使得它看起来像第二个移动,但第一个也没有!这就是造成您的问题的原因。
有很多方法可以解决此问题,但最简单的方法可能是将星星位置初始化为int
值:
PVector vec = new PVector(int(random(-width,width*2)), int(random(-height,height*2)));
这也是为什么您的墙看起来不错的原因:实际上这只是您要更新的一个位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句