Java 鼠标移动太快,无法检查碰撞
我在玩滑块游戏时遇到了一个问题,因为当我试图以很快的速度猛击两个滑块时,碰撞代码会出错。但是,如果鼠标速度较慢,它就可以工作。有没有办法限制鼠标速度,或者修改我的代码,这样就不会发生这种情况Java 鼠标移动太快,无法检查碰撞,java,performance,mouse,drag,collision,Java,Performance,Mouse,Drag,Collision,我在玩滑块游戏时遇到了一个问题,因为当我试图以很快的速度猛击两个滑块时,碰撞代码会出错。但是,如果鼠标速度较慢,它就可以工作。有没有办法限制鼠标速度,或者修改我的代码,这样就不会发生这种情况 //when the mouse is dragged and component selected is not null, continue if (componentName != null) { //get the current mouse x and y assu
//when the mouse is dragged and component selected is not null, continue
if (componentName != null) {
//get the current mouse x and y assuming that it was clicked from the middle of the component
mouseX = e.getX() - carImage[Integer.parseInt(componentName)].getWidth() / 2;
mouseY = e.getY() - carImage[Integer.parseInt(componentName)].getHeight() / 2;
//get the direction of the selected component
direction = group.get(mapsIndex)[Integer.parseInt(componentName)].updown(Integer.toString(Integer.parseInt(componentName)));
//if the direction is horizontal, make sure that the object is not dragged off the screen (right and left)
if (direction == true) {
if (mouseX < 50) {
mouseX = 50;
} else if (mouseX > 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth()) {
mouseX = 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth();
}
//get the location of the mouse for the y axis
mouseY = carImage[Integer.parseInt(componentName)].getY();
//if the direction is vertical, make sure that the object is not dragged off the top and bottom of the screen
} else {
if (mouseY < 50) {
mouseY = 50;
} else if (mouseY > 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight()) {
mouseY = 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight();
}
//get the location of the mouse for the x axis
mouseX = carImage[Integer.parseInt(componentName)].getX();
}
//find the area that the selected object occupies
Rectangle or = carImage[Integer.parseInt(componentName)].getBounds();
//go through all other components
for (int x = 0; x < max; x++) {
//as long as the comparison is not made with itself or a nonexistant object, continue
if (x != Integer.parseInt(componentName) && carImage[x] != null) {
//get the area that the compared object occupies
Rectangle collide = carImage[x].getBounds();
//if the two areas intersect, make the selected car go back to where it was
if (or.intersects(collide)) {
mouseX = carImage[Integer.parseInt(componentName)].getX();
mouseY = carImage[Integer.parseInt(componentName)].getY();
}
}
}
//update the component's location to where the mouse is
carImage[Integer.parseInt(componentName)].setLocation(mouseX, mouseY);
}
}
//当拖动鼠标且所选组件不为空时,是否继续
if(组件名称!=null){
//获取当前鼠标x和y,假设它是从组件的中间单击的
mouseX=e.getX()-carImage[Integer.parseInt(componentName)].getWidth()/2;
mouseY=e.getY()-carImage[Integer.parseInt(componentName)].getHeight()/2;
//获取选定零部件的方向
direction=group.get(mapsIndex)[Integer.parseInt(componentName)].updown(Integer.toString(Integer.parseInt(componentName));
//如果方向是水平的,请确保对象未从屏幕上拖出(右侧和左侧)
如果(方向==真){
if(mouseX<50){
mouseX=50;
}else if(mouseX>50*7-carImage[Integer.parseInt(componentName)].getWidth(){
mouseX=50*7-carImage[Integer.parseInt(componentName)].getWidth();
}
//获取鼠标在y轴上的位置
mouseY=carImage[Integer.parseInt(componentName)].getY();
//如果方向是垂直的,请确保对象没有从屏幕的顶部和底部拖出
}否则{
如果(鼠标<50){
老鼠=50;
}else if(mouseY>50*7-carImage[Integer.parseInt(componentName)].getHeight(){
mouseY=50*7-carImage[Integer.parseInt(componentName)].getHeight();
}
//获取鼠标在x轴上的位置
mouseX=carImage[Integer.parseInt(componentName)].getX();
}
//查找选定对象所占据的区域
矩形或=carImage[Integer.parseInt(componentName)].getBounds();
//检查所有其他组件
对于(int x=0;x
我不会走你试图限制鼠标速度的道路——用户有自己的偏好
您可以做的是将用户交互与系统行为分开,以提高可靠性。按住并更新当前位置,并按指定的时间间隔计算背景线程中的碰撞-如果发生碰撞,请将用户移回。您应该能够以比用户注意到的更短的时间间隔运行它。您还可以通过使用更多的内核来提高性能,即Java 7 ForkJoin池是一个简单的解决方案。我不会走限制鼠标速度的道路-用户有自己的偏好
您可以做的是将用户交互与系统行为分开,以提高可靠性。按住并更新当前位置,并按指定的时间间隔计算背景线程中的碰撞-如果发生碰撞,请将用户移回。您应该能够以比用户注意到的更短的时间间隔运行它。您还可以通过使用更多内核来提高性能,即Java 7 ForkJoin池是一个简单的解决方案。使用更高效的碰撞检测,例如。当对鼠标进行采样时,鼠标从(x,y)到(u,v)的时间不到1/3秒,只需“画”中间的线,即分析这条线上的所有点。这个公式是基础数学。@MattBall:在这种情况下,您如何实现碰撞的四叉树?对不起,我在编程方面不是很在行…使用更有效的碰撞检测,例如。当对鼠标进行采样时,它从(x,y)到(u,v)的时间不到1/3秒,只需“画”中间的线,即分析这条线上的所有点。这个公式是基础数学。@MattBall:在这种情况下,您如何实现碰撞的四叉树?对不起,我在编程方面不是很先进。。。