画一条平滑的线

2024-03-24

我正在使用 libgdx 开发游戏,我想使用形状渲染器绘制一条平滑的线条。

        shaperenderer.begin(ShapeType.Line);
        shaperenderer.line(fisrstVec2,secondVec2);
        shaperenderer.end();

我努力了多样本抗锯齿 http://www.badlogicgames.com/wordpress/?p=2071来自 libgdx 博客。 我也经历过libgdx 中的抗锯齿字段形状 https://stackoverflow.com/questions/18186375/anti-aliasing-filled-shapes-in-libgdx但不幸的是,这些行不在最新版本的 libgdx 中。

   Gdx.gl.glEnable(GL10.GL_LINE_SMOOTH);
   Gdx.gl.glEnable(GL10.GL_POINT_SMOOTH); 

在配置中启用抗锯齿:

对于桌面:

    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.samples = 2;
    new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config);

对于安卓:

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.numSamples = 2;
    initialize(new MyGdxGame(null), config);

或者您可以创建一个 1x1 的白色像素图,并用它来创建一个精灵并使用该精灵绘制线条,我个人更喜欢这种方法而不是 ShapeRenderer (请注意,此方法中没有旋转):

/**
 * draws a line on the given axis between given points
 *
 * @param batch       spriteBatch
 * @param axis        axis of the line, vertical or horizontal
 * @param x          x position of the start of the line
 * @param y          y position of the start of the line
 * @param widthHeight width or height of the line according to the axis
 * @param thickness thickness of the line
 * @param color       color of the line, if the color is null, color will not be changed.
 */
public static void line(SpriteBatch batch, Axis axis, float x, float y, float widthHeight, float thickness, Color color, float alpha) {
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    if (axis == Axis.vertical) {
        sprite.setSize(thickness, widthHeight);
    } else if (axis == Axis.horizontal) {
        sprite.setSize(widthHeight, 1);
    }
    sprite.setPosition(x,y);
    sprite.draw(batch);
    sprite.setAlpha(1);
}

通过对前面的方法进行一些修改,您可以想出此方法来绘制旋转的线。

public static void rotationLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float thickness, Color color, float alpha) {
    // set color and alpha
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    // set origin and rotation
    sprite.setOrigin(0,0);
    sprite.setRotation(getDegree(x2,y2, x1, y1));
    // set position and dimension
    sprite.setSize(distance(x1,y1,x2,y2),thickness);
    sprite.setPosition(x1, y1);
    // draw
    sprite.draw(batch);
    // reset rotation
    sprite.rotate(0);
}

public static float getDegree(float x, float y, float originX, float originY) {
    float angle = (float) Math.toDegrees(Math.atan2(y - originY, x - originX));
    while (angle < 0 || angle > 360)
        if (angle < 0) angle += 360;
        else if (angle > 360) angle -= 360;
    return angle;
}

public static float distance(float x, float y, float x2, float y2) {
    return (float) Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

画一条平滑的线 的相关文章

随机推荐

  • 在 Elixir 中部署时更改后端/模块?

    如何实现一个可替换的后端 或基本上任何部分或模块 以便可以在 Elixir 中的配置 部署时进行替换 我的具体情况是一个简单的网络应用程序 在本例中使用 Phoenix 但我猜这个问题也适用于其他情况 我有一个非常简单的后端 使用Agent
  • 从 iPhone 上的我的应用程序调用官方 *设置* 应用程序

    在我的应用程序中的某一时刻 我想将用户重定向到官方Settings应用程序 如果可以的话我也想直接去Network内的部分Settings app 我想我需要的是Settings应用程序的 url 方案和构建我的请求的格式 但我怀疑调用这样
  • WebActivatorEx 与 OwinStartup

    例如 在 Web API 应用程序中 有什么区别 assembly OwinStartup typeof MyClass MyMethod and assembly WebActivatorEx PreApplicationStartMet
  • 当下载发生在新选项卡中时,如何在 Selenium (Java) 中无头下载文件?

    我有一个网页 当我单击按钮时 它会打开另一个选项卡 然后在其中几秒钟后下载 csv 文件 我试图无头自动化 但我无法做到这一点 我正在使用下面的代码 但我认为以下解决方案适用于在同一窗口中进行下载 我如何调整它以适合我的情况 代码工作正常
  • 如何使用 bluez API 扫描低功耗蓝牙设备

    我是使用 BlueZ 进行低功耗蓝牙编程的新手 我想开发一个本机代码 c 来使用 BlueZ API 在 Android 4 0 3 下扫描附近的低功耗蓝牙设备 有人知道我应该使用哪些 BlueZ 方法吗 如果您想编写本机 C 代码来扫描
  • Drupal 7:如何更改图像字段小部件“alt”或标题“标签

    我正在尝试更改节点添加表单上的图像小部件中的 alt 和 标题 标签 我已经尝试过这两个钩子 hook field widget form alter hook form alter 我无法找到需要去哪里才能成功更改标签 有人可以指导我找到
  • Spring 的 LoadTimeWeaver 代理未启动

    我正在尝试使用 Spring 和 AspectJ 实现加载时编织 据我所知 我已正确配置所有内容 但当我尝试运行集成测试时 我不断收到错误 org springframework beans factory BeanCreationExce
  • React Navigation v5:如何与 Branch.io 一起使用

    我们有一个利用 Branch io 通用链接的应用程序 按照以下文档 https help branch io developers hub docs react native read deep link https help branc
  • 如何使用反射获取对象的属性?

    我知道我能做到 foreach PropertyInfo property in myobject GetType GetProperties if property DeclaringType ToString myobject GetT
  • Cuda 计算模式和“CUBLAS_STATUS_ALLOC_FAILED”

    我的集群中有一台主机 有 8 个Nvidia K80我想将其设置为每个设备最多可以运行 1 个进程 以前 如果我在主机上运行多个作业并且每个作业都使用大量内存 它们都会尝试访问同一设备并失败 我将所有设备设置为计算模式 3 E Proces
  • ActivityThread:找不到源

    我正在开发一个 Android 应用程序来比较两个静态图像 但是当我尝试调试该项目时 我在 ActivityThread 中找不到源代码 这是我的代码 查找器类 package com example testmatching import
  • Laravel 中的 Associate()

    我有 2 个模型 一个用户和一个地址 自从我读到它以来 我仍在尝试 Associate https laravel com docs 5 6 eloquent relationships updating belongs to relati
  • 为Windows应用程序实现webrtc数据通道[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 有谁知道如何向用 C 构建的 Windows 应用程序实现 WebRTC DataChannel API 以便在 NAT 后面的客户端
  • Logstash 配置:检查布尔字段是否存在

    使用Logstash 1 4 2 我有一个字段myfield这是我的 JSON 文档中的布尔值 为了检查它是否存在 不关心布尔值 我使用了 if myfield exists else doesn t exist 测试该条件语句的结果是 m
  • 如何将换行符放入 R 中 xtable 的列标题中

    我有一个数据框 我正在将其放入sweave http www stat uni muenchen de leisch Sweave 使用 xtable 的文档 但是我的列名之一很长 我想将其分成两行以节省空间 calqc table lt
  • Weblogic:调用不带架构名称的 DB2 存储过程(属性 currentSchema)

    我有一个在 Weblogic 上运行的 Java 应用程序 应用程序需要访问 DB2 数据库中的存储过程 因此 JDBC 数据源是通过其 JNDI 名称来配置和访问的 数据源 ClassDriver com ibm db2 jcc DB2D
  • XSLT 生成 UUID

    如何使用纯 XSLT 生成 UUID 基本上是寻找一种使用 XSLT 创建独特序列的方法 该序列可以是任意长度 我正在使用 XSLT 2 0 这是一个好例子 http www pure xml com cblog index php arc
  • 如何使用 EPPlus 在 C# 中逐行读取 Excel 文件

    在我的 MVC 控制器中 我试图逐行读取 Excel 但是有一个问题 我希望能够将其映射到列表 模型类包含29个字段 public class GroupMembershipUploadInput public string chpt cd
  • 在 Visual Studio 中发布程序

    我是 Visual Studio 的新手 我正在尝试弄清楚如何 发布 我的程序 以便我可以将其移动到其他计算机并从那里运行它 我不确定这是否有什么不同 但我的解决方案中有三个项目 如果我发布它 我是否仍然能够继续开发原始文件等 多谢 好的
  • 画一条平滑的线

    我正在使用 libgdx 开发游戏 我想使用形状渲染器绘制一条平滑的线条 shaperenderer begin ShapeType Line shaperenderer line fisrstVec2 secondVec2 shapere