简体   繁体   English

LibGDX ShapeRenderer 拒绝渲染

[英]LibGDX ShapeRenderer refusing the render

Currently i am trying to render a rectangle between the mouse and a body the catch is I want the line to have a maximum length.目前我正在尝试在鼠标和身体之间渲染一个矩形,但我希望线条具有最大长度。

Meaning when the distance between the 2 points on the screen is less than a certain amount the rectangle should be between the mouse and body.意思是当屏幕上两点之间的距离小于一定量时,矩形应该在鼠标和身体之间。 If not the rectangle should be between a radius(point on the line from body to mouse) and the body.如果不是,矩形应该在半径(从身体到鼠标的线上的点)和身体之间。

I am using some vector logic to calculate the point to draw but when I seem to go inside my if statement the line which is drawn in under 200 distance just disappears.我正在使用一些向量逻辑来计算要绘制的点,但是当我似乎进入我的 if 语句时,在 200 距离内绘制的线就消失了。

 ShapeRenderer sr = new ShapeRenderer();
 sr.setColor(Color.WHITE);
 sr.begin(ShapeRenderer.ShapeType.Filled);

 if (ballPosition.dst(mousePos) > 200) {
     System.out.println("Entered If!");

     //Calculate point a distance away from ballPosition
     Vector2 cloneMousePos = new Vector2(mousePos);
     Vector2 dir = cloneMousePos.sub(ballPosition);
     dir = dir.nor().scl(100);

     Vector2 test = ballPosition.add(dir);
     mousePos = test;
 }


 System.out.println("MousePos: " + mousePos.x + ", " + mousePos.y);
 sr.rectLine(ballPosition, mousePos, 4f);
 sr.end();

This is inside a Screen class I find it strange since when the distance is less than 200 the line draws perfectly, though from printing the x,y coordinates of the vectors it seems to be checking out.这是在 Screen 类中,我觉得很奇怪,因为当距离小于 200 时,线条绘制得很好,尽管从打印矢量的x,y坐标来看,它似乎正在检查。

Prints of the x,y coordinates of mousepos before moving a distance 200 out of body and after在离开身体 200 距离之前和之后打印mousepos的 x,y 坐标

MousePos: 213.0, 325.0
Entered If!
MousePos: 305.3836, 357.63123

EDIT: On suggestion in the comments I have added some pictures.编辑:根据评论中的建议,我添加了一些图片。

距离小于 200 时绘制的线

Here a line between the ball and mouse is being drawn since the distance is under 200.由于距离小于 200,这里正在绘制球和鼠标之间的一条线。

距离超过 200 时不绘制线

while here the distance becomes over 200 and we enter the if statement no line is drawn anymore unless we return to under 200.而这里的距离超过 200,我们输入 if 语句,除非我们回到 200 以下,否则不再画线。

Thanks!谢谢!

Vector2 has a limit method to limit the length if greater than a certain value.如果大于某个值, Vector2有一个limit方法来限制长度。

Vector2 dir = new Vector(mousePos).sub(ballPosition)
dir.limit(200f)
sr.rectLine(ballPosition, dir.add(ballPosition), 4f);

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

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