我正在尝试进行一些碰撞检测.对于这个测试,我使用简单的矩形Shape,并检查它们Bound,以确定它们是否发生碰撞.虽然检测不能按预期工作.我尝试过使用不同的方法移动对象(relocate,setLayoutX,Y)以及不同的绑定检查(boundsInLocal,boundsInParrent等),但我仍然无法使其工作.如您所见,检测仅适用于一个对象,即使您有三个对象,也只有一个检测到碰撞.这是一些演示问题的工作代码:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.ArrayList;
public class CollisionTester extends Application {
private ArrayList<Rectangle> rectangleArrayList;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("The test");
Group root = new Group();
Scene scene = new Scene(root, 400, 400);
rectangleArrayList = new ArrayList<Rectangle>();
rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.GREEN));
rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.RED));
rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.CYAN));
for(Rectangle block : …Run Code Online (Sandbox Code Playgroud) 抱歉,我还是不明白。我的问题是我对物理一无所知,但我的老师分配给我这个项目。
private void shoot() {
Group group = new Group();
double angle = cannon.getRotate();
double speed = slider.getValue();
double x = cannon.getLayoutX();
double y = cannon.getLayoutY();
double v0X = Math.cos(angle)*speed;
double voY = Math.sin(angle)*speed;
Circle c = new Circle(x, y, 8, Color.BLACK);
/*t is the time, but I don't know its
value or has it the same value of the KeyFrame duration? */
double x2 = x + voX*t;
double y2 = y + v0Y * t - 0.5 * …Run Code Online (Sandbox Code Playgroud)