日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

jbox2d_JBox2D和JavaFX:事件与力量

發(fā)布時(shí)間:2023/12/3 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jbox2d_JBox2D和JavaFX:事件与力量 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

jbox2d

在昨天的示例中,您看到了如何創(chuàng)建一個(gè)簡單的世界并使用WorldView進(jìn)行顯示,以及如何提供自定義渲染器。 現(xiàn)在,我們將添加一些用戶輸入。 我們將創(chuàng)建一個(gè)類似于彈球機(jī)中的鰭狀肢的控件。

為此,我們將創(chuàng)建一個(gè)關(guān)節(jié)。 在JBox2D中,“關(guān)節(jié)”用于將物體約束到世界或彼此約束。 我們將創(chuàng)建一個(gè)靜態(tài)圓形的Body,它將用作我們的鰭狀肢的軸,并通過RevoluteJoint將Box綁定到它。

為了簡化代碼,我們將首先定義一個(gè)JointBuilder基類和一個(gè)RevoluteJointBuilder:

public abstract class JointBuilder, T extends JointDef> {protected World world;protected T jointDef;protected JointBuilder(World world, T jointDef) {this.world = world;this.jointDef = jointDef;}public K bodyA(Body a) {jointDef.bodyA = a;return (K) this;}public K bodyB(Body b) {jointDef.bodyB = b;return (K) this;}public K userData(Object userData) {jointDef.userData = userData;return (K) this;}public K type(JointType type) {jointDef.type = type;return (K) this;}public K collideConnected(boolean coco) {jointDef.collideConnected = coco;return (K) this;}public Joint build() {return world.createJoint(jointDef);} }

這是RevoluteJointBuilder:

public class RevoluteJointBuilder extends JointBuilder {public RevoluteJointBuilder(World world, Body a, Body b, Vec2 anchor) {super(world, new RevoluteJointDef());jointDef.initialize(a, b, anchor);}public RevoluteJointBuilder enableLimit(boolean enable) {jointDef.enableLimit = enable;return this;}public RevoluteJointBuilder enableMotor(boolean motor) {jointDef.enableMotor = motor;return this;}public RevoluteJointBuilder localAnchorA(Vec2 localAnchorA) {jointDef.localAnchorA = localAnchorA;return this;}public RevoluteJointBuilder localAnchorB(Vec2 localAnchorB) {jointDef.localAnchorB = localAnchorB;return this;}public RevoluteJointBuilder lowerAngle(float lowerAngle) {jointDef.lowerAngle = lowerAngle;return this;}public RevoluteJointBuilder maxMotorTorque(float maxMotorTorque) {jointDef.maxMotorTorque = maxMotorTorque;return this;}public RevoluteJointBuilder motorSpeed(float motorSpeed) {jointDef.motorSpeed = motorSpeed;return this;}public RevoluteJointBuilder referenceAngle(float referenceAngle) {jointDef.referenceAngle = referenceAngle;return this;}public RevoluteJointBuilder upperAngle(float upperAngle) {jointDef.upperAngle = upperAngle;return this;}}

現(xiàn)在,我們可以像這樣修改HelloWorld-Example:

public class HelloWorld extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) {World world = new World(new Vec2(0, -2f), true);primaryStage.setTitle("Hello World!");NodeManager.addCircleProvider(new MyNodeProvider());new CircleBuilder(world).userData("ball").position(0.1f, 4).type(BodyType.DYNAMIC).restitution(1).density(2).radius(.15f).friction(.3f).build();final Body flipperBody = new BoxBuilder(world).position(0, 2).type(BodyType.DYNAMIC).halfHeight(.02f).halfWidth(.2f).density(2).friction(0).userData("flipper").build();Vec2 axis = flipperBody.getWorldCenter().add(new Vec2(.21f, 0));Body axisBody = new CircleBuilder(world).position(axis).type(BodyType.STATIC).build();new RevoluteJointBuilder(world, flipperBody, axisBody, axis).upperAngle(.6f).lowerAngle(-.6f).enableMotor(true).enableLimit(true).maxMotorTorque(10f).motorSpeed(0f).build();Scene scene = new Scene(new WorldView(world, 200, 400, 50), 500, 600);// groundnew BoxBuilder(world).position(0, -1f).halfHeight(1).halfWidth(5).build();primaryStage.setScene(scene);primaryStage.show();} }

這將顯示我們的場景,您將看到關(guān)節(jié)如何防止動態(tài)Box掉落到地面以及如何限制其運(yùn)動。

下一步是允許用戶對其進(jìn)行控制。 為此,我們將在用戶按下按鍵時(shí)施加力。 在場景實(shí)例化之后添加以下內(nèi)容:

scene.setOnKeyPressed(new EventHandler() {@Overridepublic void handle(KeyEvent ke) {if (ke.getCode()== KeyCode.LEFT) {flipperBody.applyTorque(-15f);}}});scene.setOnKeyReleased(new EventHandler() {@Overridepublic void handle(KeyEvent ke) {if (ke.getCode()== KeyCode.LEFT) {flipperBody.applyTorque(15f);}}});

現(xiàn)在就這樣。 在本教程的下一部分中,我們將做更多的自定義渲染并創(chuàng)建一些漂亮的自定義節(jié)點(diǎn)。

參考: Eppleton博客上來自JCG合作伙伴 Toni Epple的JBox2D和JavaFX的事件和影響 。


翻譯自: https://www.javacodegeeks.com/2012/05/jbox2d-and-javafx-events-and-forces.html

jbox2d

總結(jié)

以上是生活随笔為你收集整理的jbox2d_JBox2D和JavaFX:事件与力量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。