2016-05-23 63 views
0

在使用libgdx开发的小型游戏中,我将矛作为游戏对象。 我使用Scene2D作为Actor的一个子类实现了它们。长矛可以旋转90度。我想让我的形象中的“speartip”部分作为“伤害”球员的一部分,杆身应该是无害的。所以当我构建我的矛对象时,我还构造了两个矩形,它们覆盖了矛的尖端和轴。但是当我的演员使用setRotation()旋转时,矩形显然不会,因为它们没有“附加”到矛状物体上。如何在libgdx中获得旋转角色的区域

你对如何处理这种东西有一些建议吗?下面的代码。

public TrapEntity(Texture texture, float pos_x, float pos_y, float width, float height, Vector2 center, 
        float rotation, Rectangle hurtZone, Rectangle specialZone) { 
    super(texture, pos_x, pos_y, width, height, center, rotation); 

    this.hurtZone = new Rectangle(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight()); 
    this.specialZone = new Rectangle(specialZone.getX(), specialZone.getY(), specialZone.getWidth(), specialZone.getHeight()); 

} 

和render方法在同一个类中。我用它来渲染“hurtZone”矩形的界限:

@Override 
public void draw(Batch batch, float alpha){ 
    super.draw(batch, alpha); 
    batch.end(); 
    sr.setProjectionMatrix(batch.getProjectionMatrix()); 
    sr.setTransformMatrix(batch.getTransformMatrix()); 
    sr.begin(ShapeRenderer.ShapeType.Line); 
    sr.setColor(Color.RED); 
    sr.rect(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight()); 
    sr.end(); 
    batch.begin(); 

} 

回答

1

矩形不能旋转,我不得不做类似的事情,并找到解决办法挣扎。我发现的最佳解决方案是使用多边形。你会做这样的事情;

//Polygon for a rect is set by vertices in this order 
    Polygon polygon = new Polygon(new float[]{0, 0, width, 0, width, height, 0, height}); 

    //properties to update each frame 
    polygon.setPosition(item.getX(), item.getY()); 
    polygon.setOrigin(item.getOriginX(), item.getOriginY()); 
    polygon.setRotation(item.getRotation()); 

然后,检查一个点是否在旋转的多边形内使用;

polygon.contains(x, y); 
+1

我在我的搜索中遇到过多边形,但想知道是否有一个“更容易”的解决方案。另外,你可能有建议如何检查我的纹理的哪一部分,例如感动?不,我有一个矩形的长矛,以及两个长方形的杆身和尖端。在我将其作为多边形实现之前,是否有更好的方法来检查是否触摸了“小费”,例如即使纹理被旋转,将触摸坐标转换为纹理坐标? – Cuddl3s

+0

你可能只需要几个'Vector2'即可。您可能需要将纸笔放在纸上,但您大概可以使用符合矛的起源的'Vector2'和大致沿着矛尖的另一个'Vector2',然后尝试一些自定义逻辑以获得所需的结果。例如(让我们称它们为'originVec2'&'tipVec2'),如果'tipVec2'在你的播放器内,但'originVec2'大于到播放器的一定距离,和/或'originVec2'和'tipVec2 '在一定范围内,等等。这种东西只是归结为剧烈的搞乱和调整。 – hamham