[Tutorial][數(shù)學向]從零開始的MC特效(六 | 坐標系的旋轉(zhuǎn))

目錄:

  • 導讀
  • Location點的旋轉(zhuǎn)
  • 坐標系的修正與在玩家背部建立坐標系
  • 制作簡易翅膀

導讀

導讀
本教程需要讀者有一定的空間想象能力(因為我也懶得畫圖了233)
本教程使用的 PaperSpigot1.12.2-R0.1-SNAPSHOT 核心
在閱讀之前請確保你具有高中數(shù)學必修4和和Java基礎的知識

<To初中生>: 如果你是初中的話,別慌,你有趨向的概念就可以讀懂本教程(應該吧...)
<To高中生>: 如果你還未學到關于上面的那本書,別慌學到了再來看也行233 (霧
<To大學生>: 沒什么好說的...


Location點的旋轉(zhuǎn)

首先我們引入平面上點圍繞另一個點進行旋轉(zhuǎn)的公式 (數(shù)學上)
平面中,一個點(x,y)繞任意點(x0,y0)逆時針旋轉(zhuǎn)a度后的坐標

dx = (x - x0)*cos(a) - (y - y0)*sin(a) + x0 ;
dy = (x - x0)*sin(a) + (y - y0)*cos(a) + y0 ;

那么我們寫入代碼看看是怎么樣的

/**
 * 在二維平面上利用給定的中心點逆時針旋轉(zhuǎn)一個點
 * 
 * @param location 待旋轉(zhuǎn)的點
 * @param angle    旋轉(zhuǎn)角度
 * @param point    中心點
 * @return {@link Location}
*/
public static Location rotateLocationAboutPoint(Location location, double angle, Location point) {
    double radians = Math.toRadians(angle);
    double dx = location.getX() - point.getX();
    double dz = location.getZ() - point.getZ();

    double newX = dx * Math.cos(radians) - dz * Math.sin(radians) + point.getX();
    double newZ = dz * Math.cos(radians) + dx * Math.sin(radians) + point.getZ();
    return new Location(location.getWorld(), newX, location.getY(), newZ);
}

總所周知,在mc坐標內(nèi),玩家走動的二維平面,其實是影響x軸和z軸的內(nèi)容,所以我們上方的代碼就套用x,y

坐標系的修正與在玩家背部建立坐標系

在我們之前的教程中,我們都會發(fā)現(xiàn),我們在做一些讓特效出現(xiàn)在玩家面前時,會出現(xiàn)特效出現(xiàn)在另外一邊,這其實就是我們沒有經(jīng)過玩家朝向的修正,而發(fā)生的情況,比如下面這一張圖

愛心.png

那么我們可以重新建立一個修正過后的坐標系,用的方法就是利用Location點的旋轉(zhuǎn)

import org.bukkit.Location;

/**
 * 自動修正在平面上的粒子朝向
 * 
 * @author Zoyn
 */
public class PlayerFixedCoordinate {

    private Location zeroDot;
    private double rotateAngle;

    public PlayerFixedCoordinate(Location playerLocation) {
        // 旋轉(zhuǎn)的角度
        rotateAngle = playerLocation.getYaw();
        zeroDot = playerLocation.clone();
        zeroDot.setPitch(0);
        // 重設仰俯角, 防止出現(xiàn)仰頭后旋轉(zhuǎn)角度不正確的問題
    }

    public Location getZeroDot() {
        return zeroDot;
    }

    public Location newLocation(double x, double z) {
        return rotateLocationAboutPoint(zeroDot.clone().add(-x, 0, z), rotateAngle, zeroDot);
    }
    
        /**
     * 在二維平面上利用給定的中心點逆時針旋轉(zhuǎn)一個點
     * 
     * @param location 待旋轉(zhuǎn)的點
     * @param angle    旋轉(zhuǎn)角度
     * @param point    中心點
     * @return {@link Location}
     */
    public static Location rotateLocationAboutPoint(Location location, double angle, Location point) {
        double radians = Math.toRadians(angle);
        double dx = location.getX() - point.getX();
        double dz = location.getZ() - point.getZ();

        double newX = dx * Math.cos(radians) - dz * Math.sin(radians) + point.getX();
        double newZ = dz * Math.cos(radians) + dx * Math.sin(radians) + point.getZ();
        return new Location(location.getWorld(), newX, location.getY(), newZ);
    }
}

首先我們來分析這個類是怎么寫的,首先我們要旋轉(zhuǎn)一個點,就需要旋轉(zhuǎn)的角度,那么這時候 location 里的 yaw 就可以幫助我們完成這個工作,所以我在構造器里將 yaw 記錄為 rotateAngle

之后我們看newLocation這個方法,需要填入兩個參數(shù)分別是 x, y (為了方便理解,我其實直接將其設計為數(shù)學上的平面直角坐標系(右手坐標系))

而我們在看

zeroDot.clone().add(-x, 0, z)

這行代碼, 首先它是 rotateLocationAboutPoint 方法里的待旋轉(zhuǎn)的點,那么我們?yōu)槭裁匆猘dd呢?
因為啊, zeroDot 就是我們坐標系的原點,經(jīng)過add之后就可以得到新的x,y了,

比如說,zeroDot是(0, 0),方法填入3, 2, 那么add完之后就得到 (3, 2) 這個點

那么為什么是-x呢???
因為啊,在Mc中的坐標系是遵循左手坐標系來設計的,所以它的x軸我們要乘以一個-1才能按照我們平常理解的右手坐標系來繪圖

之后我們套用上這個修復過的坐標系來看看效果


jdfw3.gif

完整代碼:

Player player = ........
PlayerFixedCoordinate coordinate = new PlayerFixedCoordinate(player.getLocation());

double radius = 10;
for (double t = -1; t <= 1; t += 0.001) {
    double x = radius * Math.sin(t) * Math.cos(t) * Math.log(Math.abs(t));
    double y = radius * Math.sqrt(Math.abs(t)) * Math.cos(t);

    Location loc = coordinate.newLocation(x, y);
    loc.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, loc, 1, 0, 0, 0, 0);
}

下面分享一個PlayerBackCoordinate為了讓讀者能夠舉一反三,希望讀者能夠自行添加 z 軸的變化(思考:z軸的變化在右手坐標系中是如何變化,又應該如何將其轉(zhuǎn)換至MC坐標系內(nèi))

繪圖思考可以參照這張圖:


PlayerBackCoordinate的思考
import org.bukkit.Location;

/**
 * 將玩家背后轉(zhuǎn)換為一個平面直角坐標系
 * 
 * @author Zoyn
 */
public class PlayerBackCoordinate {

    private Location zeroDot;
    private double rotateAngle;

    public PlayerBackCoordinate(Location playerLocation) {
        // 旋轉(zhuǎn)的角度
        rotateAngle = playerLocation.getYaw();
        zeroDot = playerLocation.clone();
        zeroDot.setPitch(0); // 重設仰俯角
        zeroDot.add(zeroDot.getDirection().multiply(-0.3)); // 使原點與玩家有一點點距離
    }

    public Location getZeroDot() {
        return zeroDot;
    }

    public Location newLocation(double x, double y) {
        return rotateLocationAboutPoint(zeroDot.clone().add(-x, y, 0), rotateAngle, zeroDot);
    }

        /**
     * 在二維平面上利用給定的中心點逆時針旋轉(zhuǎn)一個點
     * 
     * @param location 待旋轉(zhuǎn)的點
     * @param angle    旋轉(zhuǎn)角度
     * @param point    中心點
     * @return {@link Location}
     */
    public static Location rotateLocationAboutPoint(Location location, double angle, Location point) {
        double radians = Math.toRadians(angle);
        double dx = location.getX() - point.getX();
        double dz = location.getZ() - point.getZ();

        double newX = dx * Math.cos(radians) - dz * Math.sin(radians) + point.getX();
        double newZ = dz * Math.cos(radians) + dx * Math.sin(radians) + point.getZ();
        return new Location(location.getWorld(), newX, location.getY(), newZ);
    }
}

上方代碼的使用:實例1:在玩家后背繪制一個圓

Player player = (Player) sender;
PlayerBackCoordinate coordinate = new PlayerBackCoordinate(player.getLocation().add(0, 1.6D, 0));

for (int angle = 0; angle < 360; angle++) {
    double radians = Math.toRadians(angle);
    double x = Math.cos(radians);
    double y = Math.sin(radians);

    Location loc = coordinate.newLocation(x, y);
    loc.getWorld().spawnParticle(Particle.FLAME, loc, 1, 0, 0, 0, 0);
}

具體效果:


背后的火圈

制作簡易翅膀

不說這么多,直接上代碼好吧,用的就是上面的代碼

Player player = (Player) sender;
PlayerBackCoordinate coordinate = new PlayerBackCoordinate(player.getLocation().add(0, 1.5D, 0));

for (double angle = 0; angle <= 135; angle++) {
    double x = Math.toRadians(angle);
    double y = Math.sin(2 * x);

    Location loc = coordinate.newLocation(x, y);
    loc.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, loc, 1, 0, 0, 0, 0);
}

for (double angle = -135; angle <= 0; angle++) {
    double x = Math.toRadians(angle);
    double y = Math.cos((2 * x) + (Math.PI / 2));

    Location loc = coordinate.newLocation(x, y);
    loc.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, loc, 1, 0, 0, 0, 0);
}

coordinate = new PlayerBackCoordinate(player.getLocation().add(0, 1, 0));
double radius = 0;
for (double angle = 0; angle <= 3 * 360; angle++) {
    double radians = Math.toRadians(angle);
    double x = radius * Math.cos(radians);
    double y = radius * Math.sin(radians);

    Location loc = coordinate.newLocation(x, y);
    loc.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, loc, 1, 0, 0, 0, 0);
    radius += 0.001;
}

具體效果:


函數(shù)翅膀

結語

是的這個教程又開始更新了,就當做沒事拿來玩玩的了,畢竟上了大學,還是可以拿線性代數(shù)來學以致用的嘛嘻嘻嘻。此外我想開一個ParticleLib的坑,專門來制作這類特效,希望各位看官可以多多支持

看情況更新 —— 撰寫: 一個來自已經(jīng)上了帶學的大一新生 2020/3/8
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容