Java旋转矩形的碰撞检测?
我正在编写我的第一个 java 游戏,到目前为止:
我已经制作了一个可以使用 WSAD 四处走动的矩形,并且他始终面向鼠标指向的地方。此外,如果您单击,他会向您的鼠标指向的地方发射子弹(并且子弹会旋转以面向该方向)。我还制作了跟随你的敌人,他们旋转面向你的角色。我遇到的问题是,我编写的碰撞检测仅检测对象(角色、敌人和子弹)在旋转之前的碰撞(使用 .intersects())。这意味着绘制时它们身体的某些部分会重叠。
我一直在环顾四周,但没有找到任何我理解或可以适用于我的情况的解决方案。到目前为止,我一直在为每个对象旋转 Graphics2D 网格,因此它们实际上并没有被旋转,只是被绘制出来。有没有办法可以实际旋转它们的形状,然后使用类似 .intersects() 的东西?
任何帮助或建议表示赞赏。
这是我用来查看它是否会通过在 x 轴上移动而发生碰撞的方法:
public boolean detectCollisionX(int id, double xMove, double rectXco, double rectYco, int width, int height)
{
boolean valid=true;
//create the shape of the object that is moving.
Rectangle enemyRectangleX=new Rectangle((int)(rectXco+xMove)-enemySpacing,(int)rectYco-enemySpacing,width+enemySpacing*2,height+enemySpacing*2);
if (rectXco+xMove<0 || rectXco+xMove>(areaWidth-width))
{
valid=false;
}
if(enemyNumber>0)
{
for (int x=0; x<=enemyNumber; x++)
{
if (x!=id)
{
//enemies and other collidable objects will be stored in collisionObjects[x] as rectangles.
if (enemyRectangleX.intersects(collisionObjects[x])==true)
{
valid=false;
}
}
}
}
return valid;
}
I am writing my first java game and so far:
I've made a rectangle that can walk around with WSAD, and he always faces where the mouse is pointing. Also if you click, he shoots bullets where your mouse is pointing (and the bullets rotate to face that direction). I've also made enemies which follow you around, and they rotate to face towards your character. The problem i am having is that the collision detection I've written is only detecting the collision of the objects (character, enemies and bullets) before their rotation (using .intersects()). This means that some parts of their bodies overlap when drawn.
I've been looking around, and I haven't found any solutions that I understand or can apply to my situation. I've been rotating my Graphics2D grid for each of the objects so far, so they are not actually being rotated, just drawn out to be. Is there a way I can actually rotate their shapes and then use something like .intersects() ?
Any help or suggestions are appreciated.
Here is what I use to see if it will collide by moving on the x axis:
public boolean detectCollisionX(int id, double xMove, double rectXco, double rectYco, int width, int height)
{
boolean valid=true;
//create the shape of the object that is moving.
Rectangle enemyRectangleX=new Rectangle((int)(rectXco+xMove)-enemySpacing,(int)rectYco-enemySpacing,width+enemySpacing*2,height+enemySpacing*2);
if (rectXco+xMove<0 || rectXco+xMove>(areaWidth-width))
{
valid=false;
}
if(enemyNumber>0)
{
for (int x=0; x<=enemyNumber; x++)
{
if (x!=id)
{
//enemies and other collidable objects will be stored in collisionObjects[x] as rectangles.
if (enemyRectangleX.intersects(collisionObjects[x])==true)
{
valid=false;
}
}
}
}
return valid;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 AffineTransform 类来旋转各种对象,前提是这些对象是 Area 类型。
假设你有两个对象 a 和 b,你可以像这样旋转它们:
并且你有原始对象,以防万一这就是你想要的。使用 AffineTransform 可以轻松组合变换、反转变换等。
You can probably use the AffineTransform class to rotate the various objects provided the objects are of type Area.
Assume that you have two objects a and b, you can rotate them like this:
and you have the original objects just in case thats what you want. Using the AffineTransform makes it easy to combine transformations, inverse them etc.