使用 BufferedImage 的 getRGB 进行每像素碰撞(Java2D Api)
你好,我目前正在开发一款 2D 平台游戏。 我希望我的播放器(矩形)和自由地形(使用斜坡、BufferedImage)之间发生逐像素碰撞。
我对检查矩形的任何部分的概念有点困惑 与地形发生碰撞。
目前我正在尝试查看地形的一部分是否包含不透明像素。 我将其与矩形中的每个坐标进行比较,看看它们是否相遇,但是我运气不佳。
这是我的代码:
public boolean rgbCollide () {
int a = terrain.getRGB(x, y);
System.out.println(a);
// Per-pixel Bitwise collision check
for (int i =0; i < width; i++) {
for (int j =0; j < height; j++) {
//Hmm what to do here...?
}
}
return false;
}
其中:地形是我的 bufferedImage x、y、宽度和高度是我的玩家的矩形坐标
Hello I'm currently working on a 2D platformer game.
I would like to have per-pixel collisions between my player (a Rectangle) and a freeform terrain (uses slopes, a BufferedImage).
I am a bit confused on the concept to check if any part of my rectangle
collides with the terrain.
Currently I'm trying to see if a part of my terrain contains a non-transparent pixel.
I compare this to each coordinate in my rectangle and see if they meet, however I am not having luck.
Here is my code:
public boolean rgbCollide () {
int a = terrain.getRGB(x, y);
System.out.println(a);
// Per-pixel Bitwise collision check
for (int i =0; i < width; i++) {
for (int j =0; j < height; j++) {
//Hmm what to do here...?
}
}
return false;
}
where: terrain is my bufferedImage
x,y,width, and height are my player's Rectangle coordinates
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你的游戏没有滚动功能?
你至少需要
玩家的位置 - 比如说矩形左下角的 x,y
玩家的宽度、高度
地形位图
所以它会是这样的:
我不知道java2d api,所以也许方法 width() 和 height() 的调用方式不同。检查“如果不透明”也取决于api,所以它留给学生作为练习:)。
I assume your game doesn't have scrolling?
You need at least
position of player - say x,y of the left bottom corner of the rectangle
width, height of player
terrain bitmap
So it will be sth like that:
I don't know java2d api, so maybe the methods width() and height() are called differently there. Checking "if not transparent" also depends on api, so it is left as exercise to the student :).
假设您使用 ARGB 格式,最高 8 位代表 alpha(透明度)通道。
像素完美碰撞
矩形碰撞
可碰撞界面
Assuming you are using the ARGB format the highest 8 bits represent the alpha (transparency) channel.
Pixel Perfect Collision
Rectangle Collision
Collidable Interface