[英]How to get collision using Pixmap Libgdx
I am making a game where a block that is black is falling down in a white background. 我正在做一个游戏,其中黑色的方块在白色背景中掉落。 My avatar, which is red is dodging the black blocks but if it hits the black block, the game is over.
我的头像是红色,正在躲避黑色方块,但是如果击中了黑色方块,则游戏结束。 Can someone give me a hand in starting this?
有人可以帮我开始吗? I just want the avatar to check if the pixel at the location of the block is black or white.
我只希望头像检查该块位置处的像素是黑色还是白色。 Thanks.
谢谢。
You can read pixel value from Pixmap by using getPixel()
method: 您可以使用
getPixel()
方法从Pixmap中读取像素值:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html#getPixel-int-int- https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html#getPixel-int-int-
So if you can make some approximation of your object that needs detection then this can be used. 因此,如果您可以对需要检测的对象进行某种近似,则可以使用它。 Ie if your object is (almost) square or circle or something like that so you can set like 4 points representing it.
也就是说,如果您的对象是(几乎)正方形或圆形或类似的东西,那么您可以设置代表它的4个点。 Then you can check those 4 points and if any of them is in collision state then your object is also in collision state.
然后,您可以检查这四个点,如果其中任何一个处于碰撞状态,则您的对象也处于碰撞状态。
But if you need "pixel precision" and your sprite is ie 200x200 pixels then you would need to check for 200x200 = 40 000 pixels, which is of course not possible so you should go for some other option. 但是,如果您需要“像素精度”并且子画面为200x200像素,则需要检查200x200 = 40 000像素,这当然是不可能的,因此应选择其他选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.