简体   繁体   English

Java-旋转矩形以进行碰撞

[英]Java - Rotating a rectangle for collisions

I am using Java Slick StateBaeedGame and want to rotate my rectangles for my collisions, I know this is possible to do for visual purposes using the Graphics or Graphics2D object but that does not modify the rectangle itself, the rectangle that is originally listed with the variables and called for in the graphics method does not rotate, to make things more clear here is some code: 我正在使用Java Slick StateBaeedGame并希望为发生冲突而旋转矩形,我知道可以使用Graphics或Graphics2D对象实现可视化目的,但不会修改矩形本身,矩形最初是用变量列出的并且在图形方法中要求不旋转,为了使事情更清楚,这里是一些代码:

    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(460 + buckyPositionX, 50 + buckyPositionY, 100, 100);

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      worldMap.draw(buckyPositionX,buckyPositionY); //draw the map at 0,0 to start
      bucky.draw(shiftX,shiftY); //draw bucky at 320, 160 (center of the screen)


    g.rotate(460 + buckyPositionX, 50 + buckyPositionY, 40);
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
      }

The rectangle rectTwo will be shown as rotation when I load my GUI but it is not actually rotated, if I test for a collision the rectangle is still at 0 degrees. 当我加载我的GUI时,矩形rectTwo将显示为旋转,但实际上并没有旋转,如果我测试碰撞,则矩形仍为0度。

So, how do I get my rectangle variable to change its angle? 那么,如何获取矩形变量以更改其角度?

Generally, you can't. 通常,您不能。

What you can do, is transform the path of the shape... 您可以做的就是改变形状的路径...

PathIterator pathIterator = shape.getPathIterator(AffineTransform.getRotateInstance(Math.toRadians(33.5)));

This isn't much use to you right now, but you can then use a Path2D#append to append the path back into a Shape object... 现在这对您没有多大用处,但是您可以使用Path2D#append将路径附加回Shape对象中...

GeneralPath path = new GeneralPath();
path.append(pathIterator, true);

Which would allow you to paint it... 那会让你画...

((Graphics2D)g).fill(path);

This, of course, assumes that your Graphics context is a Graphics2D instance. 当然,这假定您的Graphics上下文是Graphics2D实例。

This also means, that you can't maintain a direct reference to a Rectange2D , but would need to use Shape instead. 这也意味着,您不能维护对Rectange2D的直接引用,而需要使用Shape

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

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