libGDX游戏开发之按轨迹移动(十一)

23 篇文章 8 订阅
23 篇文章 3 订阅

libGDX游戏开发之运动轨迹绘制(十一)

libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm…国内用libgdx比较少,多数情况需要去官网和google找资料,相互学习的可以加我联系方式。

按轨迹移动的绘制需要用到数学函数,通过不断变动x,y坐标达到效果

1、圆形轨迹移动

经典:按圆形轨迹移动,回顾一下正弦和余弦:
在这里插入图片描述
当给定固定左边时是这样的:
在这里插入图片描述
我们看到A点的坐标可以根据 θ 角度正弦余弦求出,渲染中通过变化 θ 达到圆形运动,实现代码如下:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * @author lingkang
 */
public class RunTest extends ApplicationAdapter {
    private Texture texture;
    private SpriteBatch batch;
    private float R = 100; // 半径
    private float originX = 200, originY = 200; // 原点
    private float Pi = 3.1416f; // π 这里我们不用双精度,3.1416够了  PI = 3.14159265358979323846 ....
    private float angle = 1f; // 旋转的角度

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("plane/player1.png"));
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // 清除屏幕

        //正弦函数 sinθ=y/r  角度 π不需要太过精确
        float a = 2 * Pi / 360 * angle;
        float x = originX + (float) Math.sin(a) * R;
        float y = originY + (float) Math.cos(a) * R;
        // 角度增加
        if (++angle > 360f) {
            angle = 1f;
        }

        // 绘制
        batch.begin();
        batch.draw(texture, x, y);
        batch.end();
    }
}

效果如下:
在这里插入图片描述
不清除屏幕效果如下:
在这里插入图片描述

2、按正弦轨迹移动

正弦函数如下:
在这里插入图片描述

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * @author lingkang
 */
public class RunTest02 extends ApplicationAdapter {
    private Texture texture;
    private SpriteBatch batch;
    private float originX = 0, originY = 200; // 原点
    private float Pi = 3.1416f; // π 这里我们不用双精度,3.1416够了  PI = 3.14159265358979323846 ....
    private float angle = 0f; // 旋转的角度
    private float r = 100;

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("plane/player1.png"));
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // 清除屏幕
        if (++originX > 648) { // 坐标移动
            originX = 0;
        }
        float a = 2 * Pi / 360 * angle;
        float y = originY + (float) Math.sin(a) * r; // r 为顶峰幅度值
        // 角度增加
        if (++angle > 360f) {
            angle = 0f;
        }
        // 绘制
        batch.begin();
        batch.draw(texture, originX, y);
        batch.end();
    }
}

效果如下:
在这里插入图片描述
不清除屏幕效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌康ACG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值