简体   繁体   English

制作自定义形状JavaFX

[英]Make Custom Shape JavaFX

I am making a 2D game in JavaFX and when detecting collisions, I am getting rather inaccurate results due to the player sprite being set as the fill of a rectangle and therefore not having the intended borders. 我正在用JavaFX制作2D游戏,当检测到碰撞时,由于将玩家角色设置为矩形填充,因此没有预期的边框,结果却变得非常不准确。 Is there a way I could make my own shape so thatI could get as accurate as possible? 有什么方法可以使自己的形状更精确吗?

Another idea I had is checking if the pixel that collided was transparent and then not ending the game if it was. 我的另一个想法是检查碰撞的像素是否透明,如果不透明则不结束游戏。 Does anyone know of a way I can get the coordinates of the pixel that collides so that from there I can use PixelReader to check? 有谁知道一种获取碰撞像素的坐标的方法,以便可以从那里使用PixelReader进行检查?

If anyone knows a better way, please let me know! 如果有人知道更好的方法,请告诉我!

Thanks, Ethan 谢谢,伊桑

There are different ways to do this. 有不同的方法可以做到这一点。 Here is one way I have used with good success. 这是我成功使用的一种方法。 I would make hit boxes, that were themselves rectangles. 我会打一些盒子,它们本身就是矩形。 Then during collision detection, I would iterate through all the hit boxes to see if they collided with the flying projectile's hit boxes. 然后在碰撞检测过程中,我将遍历所有命中盒,以查看它们是否与飞行弹丸的命中盒相撞。

What this allows you to do is fill in complex shapes with smaller rectangles. 这允许您执行的操作是用较小的矩形填充复杂的形状。 For example a plane would have one long horizontal rectangle and one smaller rectangle crossing at the middle. 例如,一个平面在中间将有一个长的水平矩形和一个较小的矩形交叉。

Currently I am using libGDX. 目前,我正在使用libGDX。 In libGDX I use their Polygon object as stated here. 在libGDX中,我按此处所述使用其Polygon对象。 https://stackoverflow.com/a/28540488/1490322 I have not seen similar functionality in JavaFX, but it would not be hard to copy what libGDX is doing into JavaFX code... their code is open sourced. https://stackoverflow.com/a/28540488/1490322我没有在JavaFX中看到类似的功能,但是将libGDX所做的事情复制到JavaFX代码中并不难……它们的代码是开源的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM