显示轨迹指示器

2024-02-06

从图像中您可以看到,在左侧发射的球在其后面发射,与计算的轨迹不符。我使用 SO 中的方程绘制球轨迹question https://stackoverflow.com/questions/10401644/mousejointdef-libgdx-draw-a-trajectory-line-like-angry-birds,对此进行修改以考虑每秒 30 帧的 box2d 步长。这确实计算出有效的轨迹,但与球的实际轨迹不匹配,球的轨迹更小。我正在对球施加 box2d 力,这也具有密度集和形状。形状半径根据球的类型而变化。我正在设置着陆事件中的起始速度。

public class ProjectileEquation {  

    public float gravity;  
    public Vector2 startVelocity = new Vector2();  
    public Vector2 startPoint = new Vector2();  
    public Vector2 gravityVec = new Vector2(0,-10f);

    public float getX(float n) {  
        return startVelocity.x * (n * 1/30f) + startPoint.x;  
    }  

    public float getY(float n) {
        float t = 1/30f * n;
        return 0.5f * gravity * t * t + startVelocity.y * t + startPoint.y;  
    }  



} 

@Override  
    public void draw(SpriteBatch batch, float parentAlpha) {  
        float t = 0f;  
        float width = this.getWidth();  
        float height = this.getHeight();  

        float timeSeparation = this.timeSeparation;  

        for (int i = 0; i < trajectoryPointCount; i+=timeSeparation) {  
            //projectileEquation.getTrajectoryPoint(this.getX(), this.getY(), i);
            float x = this.getX() + projectileEquation.getX(i);  
            float y = this.getY() + projectileEquation.getY(i);  

            batch.setColor(this.getColor());  
            if(trajectorySprite != null) batch.draw(trajectorySprite, x, y, width, height);  

           // t += timeSeparation;  
        }  
    } 



public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                if(button==1 || world.showingDialog)return false;
                touchPos.set(x, y);
                float angle = touchPos.sub(playerCannon.position).angle();
                if(angle > 270 ) {
                    angle = 0;
                }
                else if(angle >70) {
                    angle = 70;
                }
                playerCannon.setAngle(angle);
                world.trajPath.controller.angle = angle;
                float radians =  (float) angle * MathUtils.degreesToRadians;
                float ballSpeed = touchPos.sub(playerCannon.position).len()*12;
                world.trajPath.projectileEquation.startVelocity.x = (float) (Math.cos(radians) * ballSpeed);
                world.trajPath.projectileEquation.startVelocity.y = (float) (Math.sin(radians) * ballSpeed);
                return true;
            }



public CannonBall(float x, float y, float width, float height, float damage, World world,  Cannon cannonOwner) {
        super(x, y, width, height, damage, world);
        active = false;
        shape = new CircleShape();
        shape.setRadius(width/2);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 4.5f;
        if(cannonOwner.isEnemy) { //Enemy cannon balls cannot hit other enemy cannons just the player
            fd.filter.groupIndex = -16;
        }
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(this.position);

        body = world.createBody(bodyDef);
        body.createFixture(fd);
        body.setUserData(this);
        body.setBullet(true);
        this.cannonOwner = cannonOwner; 
        this.hitByBall = null;
        this.particleEffect = null;
    }

  private CannonBall createCannonBall(float radians, float ballSpeed, float radius, float damage)
    {
        CannonBall cannonBall =  new CannonBall(CannonEnd().x, CannonEnd().y, radius * ballSizeMultiplier, radius * ballSizeMultiplier, damage, this.world, this);
        cannonBall.velocity.x = (float) (Math.cos(radians) * ballSpeed);
        //cannonBall.velocity.x = (float) ((Math.sqrt(10) * Math.sqrt(29) *
            //  Math.sqrt((Math.tan(cannon.angle)*Math.tan(cannon.angle))+1)) / Math.sqrt(2 * Math.tan(cannon.angle) - (2 * 10 * 2)/29))* -1f;
        cannonBall.velocity.y = (float) (Math.sin(radians) * ballSpeed);
        cannonBall.active = true;
        //cannonBall.body.applyLinearImpulse(cannonBall.velocity, cannonBall.position);
        cannonBall.body.applyForce(cannonBall.velocity, cannonBall.position );
        return cannonBall;
    }


trajPath = new TrajectoryActor(-10f);
        trajPath.setX(playerCannon.CannonEnd().x);
        trajPath.setY(playerCannon.CannonEnd().y);
        trajPath.setWidth(10f);
        trajPath.setHeight(10f);
        stage.addActor(trajPath);

这是我在其他游戏中使用的代码,事实证明它非常精确。诀窍是将脉冲施加在物体上并读取初始速度。我计算出 0.5 秒内身体将出现的 10 个位置。该语言称为 Squirrel,它基于 Lua,具有类似 C/C++ 的语法。您应该能够了解那里发生了什么。 getTrajectoryPointsForObjectAtImpulse 返回的是一个包含 10 个位置的数组,球将在 0.5 秒内通过这些位置。

const TIMESTER_DIVIDOR = 60.0;

function getTrajectoryPoint( startingPosition, startingVelocity, n )
{
    local gravity = box2DWorld.GetGravity();
    local t = 1 / 60.0;
    local stepVelocity =  b2Vec2.Create( t * startingVelocity.x, t * startingVelocity.y );
    local stepGravity = b2Vec2.Create( t * t * gravity.x, t * t * gravity.y );

    local result = b2Vec2.Create( 0, 0 );
    result.x = ( startingPosition.x + n * stepVelocity.x + 0.5 * ( n * n + n ) * stepGravity.x ) * MTP;
    result.y = ( startingPosition.y + n * stepVelocity.y + 0.5 * ( n * n + n ) * stepGravity.y ) * -MTP;

    return result;
}

function getTrajectoryPointsForObjectAtImpulse (object, impulse) 
{
    if( !object || !impulse ) return [];

    local result = [];

    object.bBody.ApplyLinearImpulse( impulse, object.bBody.GetWorldCenter() );

    local initialVelocity = object.bBody.GetLinearVelocity();

    object.bBody.SetLinearVelocity( b2Vec2.Create(0, 0) );
    object.bBody.SetActive(false);

    for ( local i = 0.0 ; i < ( 0.5 * TIMESTER_DIVIDOR ) ; )
    {
        result.append( getTrajectoryPoint(object.bBody.GetPosition(), initialVelocity, i.tointeger() ) );

        i += ( (0.5 * TIMESTER_DIVIDOR) * 0.1 );
    }
    return result;
}

如果您不理解代码的任何部分,请告诉我,我会尽力解释。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示轨迹指示器 的相关文章

随机推荐

  • 如何提高从 2 个表中进行选择的 SQL 查询的性能

    请帮我 如何加快这个sql查询 SELECT pa FROM ParametrickeVyhladavanie pa SELECT p FROM produkty p WHERE p KATEGORIA IN categoryArray p
  • ASP.NET Core 2.1根据DB中的数据在布局中插入CSS

    我有一个正在努力解决的问题 一些背景 用户可以在我的应用程序中选择应用程序使用哪种配色方案 此选择存储在数据库中 我想做的是基于这个选择 在页面加载时 为用户所做的选择提供 CSS 文件 我一直在尝试做的是通过在 Layout cshtml
  • spring cloud aws多个sqs监听器

    我的项目中有2个sqs监听器 我希望其中之一具有相同的设置 而其中之一具有不同的设置 我想要更改的唯一值是 maxNumberOfMessages 最实用的方法是什么 我想为其中一个侦听器设置不同的 maxNumberOfMessages
  • ViewPager 内的 ListFragments

    我正在写我的第一个基于片段的应用程序并遇到了一些我无法使用 API 或 Stackoverflow 解决的严重问题 我正在使用一个浏览器在两个列表之间滑动 每个列表都有一个header按钮创建一个新的列表元素 类似于本机 Android 闹
  • 如何在页面加载时显示ajax加载gif动画?

    我尝试在我的网站中实现 AJAX 当单击 divchangepass 的内容时 它应该加载changepass template php 这是我为此使用的代码 function changepass click function block
  • 单元测试核心数据 - 异常退出,代码为 134

    我正在为我的核心数据应用程序设置单元测试 我在一个非常简单的测试中遇到了一个奇怪的问题 我收到的错误是 Developer Tools RunPlatformUnitTests include 451 0 Test rig Develope
  • asyncio create_task 永远运行

    我有以下代码 import asyncio loop asyncio get event loop async def while loop n 0 while True print f n await asyncio sleep 2 n
  • “角度未定义”的原因是什么

    我正在关注以下视频教程蛋头io http www egghead io 但在尝试效仿他创建工厂时的榜样 参见视频 我不断收到 角度未定义 参考错误 但我已经包含了角度脚本 这是我的 html 页面 div div div div
  • 如何使用 jackson 遍历生成的 json 模式并将自定义属性放入 json 模式

    type object properties name type string id type string i type integer p type object properties name type string id type
  • gnu screen:可以在最后调用的程序之后自动命名窗口吗?

    有没有办法自动让每个窗口将其名称更改为您从该窗口运行的最后一个程序的名称 这比手动重命名窗口更方便 是的 您可以使用 shelltitle 来执行此操作 假设您使用 bash 以下内容应该可以工作 将其添加到您的 screenrc shel
  • “英雄单位”是什么意思?

    英雄 一词是什么意思 为什么用它来命名网站 页面的 主要信息 具体来说 我想知道术语 英雄 或短语 英雄单位 是否是网页设计中使用的一些常见术语 但我却忽略了 英雄 一词是电影 电视道具设计师使用的 英雄道具 是为特写镜头而设计的道具 它有
  • 在 Mac 上未收到自定义记录区域的 CloudKit 推送通知

    我已设置自定义区域订阅以接收来自自定义记录区域的 静默 推送通知 我的 iOS 设备上一切正常 但我无法在 Mac 上接收通知 要注册通知 我正在注册通知类型applicationDidFinishLaunching NSApplicati
  • 我在我的博客页面上收到有关时区设置的警告[重复]

    这个问题在这里已经有答案了 正在显示PHP 日期选择器 http www triconsole com php calendar datepicker php在我的博客页面上使用简码 但低于警告 Warning date function
  • crypto.randomBytes 熵源耗尽

    我尝试使用以下命令生成大量 gt 1GB 伪随机数据crypto randomBytes 方法 但我无法为耗尽的熵源生成异常 以查看在出现这种可能的异常时我的应用程序的行为是什么 来自 Node JS 文档 注意 如果没有足够的数量 将抛出
  • Spring Data - MongoDB - JUnit 测试

    我有一个关于 Spring Data MongoDB 和 JUnit 测试的问题 RunWith SpringJUnit4ClassRunner class SpringApplicationConfiguration classes Us
  • moqing静态方法调用c#库类

    这似乎是一个很简单的问题 但我似乎找不到关键字来影响我的搜索 我试图通过模拟此方法调用中的所有对象来进行单元测试 我可以对我自己的所有创作执行此操作 除了这个 public void MyFunc MyVarClass myVar Imag
  • Rserve 服务器:如何终止阻塞实例(评估永远)?

    我需要执行Reval以多线程的方式 这是Rserve提供得相当好 但是 如果一个实例的评估时间太长 我需要能够关闭正在计算阻塞评估的实例 据我测试 给定的实例将拒绝关闭 直到评估完成 显然 它需要在再次监听之前获取结果 所以这是我的问题 有
  • 如何使用 API 从 Google 文档中提取标题

    目前正在尝试创建一个 python 脚本来检查 google 文档的各种 SEO 页面指标 谷歌文档 API 有一个好样本 https developers google com docs api samples extract text展
  • #如果忽略 DEBUG(VB.net 或 C#)

    我的代码中有几个到目前为止运行良好的代码 If DEBUG Then some code here End If 现在 我注意到 最近 If DEBUG then End If 中的代码 也在 释放模式 下执行 这很奇怪 以前没有发生过 可
  • 显示轨迹指示器

    从图像中您可以看到 在左侧发射的球在其后面发射 与计算的轨迹不符 我使用 SO 中的方程绘制球轨迹question https stackoverflow com questions 10401644 mousejointdef libgd