目錄:
- 導(dǎo)讀
- Location點(diǎn)的旋轉(zhuǎn)
- 坐標(biāo)系的修正與在玩家背部建立坐標(biāo)系
- 制作簡(jiǎn)易翅膀
導(dǎo)讀
導(dǎo)讀
本教程需要讀者有一定的空間想象能力(因?yàn)槲乙矐械卯?huà)圖了233)
本教程使用的 PaperSpigot1.12.2-R0.1-SNAPSHOT 核心
在閱讀之前請(qǐng)確保你具有高中數(shù)學(xué)必修4和和Java基礎(chǔ)的知識(shí)
<To初中生>: 如果你是初中的話,別慌,你有趨向的概念就可以讀懂本教程(應(yīng)該吧...)
<To高中生>: 如果你還未學(xué)到關(guān)于上面的那本書(shū)箍镜,別慌學(xué)到了再來(lái)看也行233 (霧
<To大學(xué)生>: 沒(méi)什么好說(shuō)的...
Location點(diǎn)的旋轉(zhuǎn)
首先我們引入平面上點(diǎn)圍繞另一個(gè)點(diǎn)進(jìn)行旋轉(zhuǎn)的公式 (數(shù)學(xué)上)
平面中芒填,一個(gè)點(diǎn)(x,y)繞任意點(diǎn)(x0,y0)逆時(shí)針旋轉(zhuǎn)a度后的坐標(biāo)
dx = (x - x0)*cos(a) - (y - y0)*sin(a) + x0 ;
dy = (x - x0)*sin(a) + (y - y0)*cos(a) + y0 ;
那么我們寫(xiě)入代碼看看是怎么樣的
/**
* 在二維平面上利用給定的中心點(diǎn)逆時(shí)針旋轉(zhuǎn)一個(gè)點(diǎn)
*
* @param location 待旋轉(zhuǎn)的點(diǎn)
* @param angle 旋轉(zhuǎn)角度
* @param point 中心點(diǎn)
* @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坐標(biāo)內(nèi)陋葡,玩家走動(dòng)的二維平面混移,其實(shí)是影響x軸和z軸的內(nèi)容,所以我們上方的代碼就套用x埃仪,y
坐標(biāo)系的修正與在玩家背部建立坐標(biāo)系
在我們之前的教程中,我們都會(huì)發(fā)現(xiàn)陕赃,我們?cè)谧鲆恍┳屘匦С霈F(xiàn)在玩家面前時(shí)卵蛉,會(huì)出現(xiàn)特效出現(xiàn)在另外一邊颁股,這其實(shí)就是我們沒(méi)有經(jīng)過(guò)玩家朝向的修正,而發(fā)生的情況毙玻,比如下面這一張圖
那么我們可以重新建立一個(gè)修正過(guò)后的坐標(biāo)系豌蟋,用的方法就是利用Location點(diǎn)的旋轉(zhuǎn)
import org.bukkit.Location;
/**
* 自動(dòng)修正在平面上的粒子朝向
*
* @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);
// 重設(shè)仰俯角, 防止出現(xiàn)仰頭后旋轉(zhuǎn)角度不正確的問(wèn)題
}
public Location getZeroDot() {
return zeroDot;
}
public Location newLocation(double x, double z) {
return rotateLocationAboutPoint(zeroDot.clone().add(-x, 0, z), rotateAngle, zeroDot);
}
/**
* 在二維平面上利用給定的中心點(diǎn)逆時(shí)針旋轉(zhuǎn)一個(gè)點(diǎn)
*
* @param location 待旋轉(zhuǎn)的點(diǎn)
* @param angle 旋轉(zhuǎn)角度
* @param point 中心點(diǎn)
* @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);
}
}
首先我們來(lái)分析這個(gè)類(lèi)是怎么寫(xiě)的,首先我們要旋轉(zhuǎn)一個(gè)點(diǎn)桑滩,就需要旋轉(zhuǎn)的角度梧疲,那么這時(shí)候 location 里的 yaw 就可以幫助我們完成這個(gè)工作,所以我在構(gòu)造器里將 yaw 記錄為 rotateAngle
之后我們看newLocation這個(gè)方法运准,需要填入兩個(gè)參數(shù)分別是 x, y (為了方便理解幌氮,我其實(shí)直接將其設(shè)計(jì)為數(shù)學(xué)上的平面直角坐標(biāo)系(右手坐標(biāo)系))
而我們?cè)诳?/p>
zeroDot.clone().add(-x, 0, z)
這行代碼, 首先它是 rotateLocationAboutPoint 方法里的待旋轉(zhuǎn)的點(diǎn),那么我們?yōu)槭裁匆猘dd呢胁澳?
因?yàn)榘? zeroDot 就是我們坐標(biāo)系的原點(diǎn)该互,經(jīng)過(guò)add之后就可以得到新的x,y了韭畸,
比如說(shuō)宇智,zeroDot是(0, 0),方法填入3, 2, 那么add完之后就得到 (3, 2) 這個(gè)點(diǎn)
那么為什么是-x呢胰丁?随橘??
因?yàn)榘〗跤梗贛c中的坐標(biāo)系是遵循左手坐標(biāo)系來(lái)設(shè)計(jì)的机蔗,所以它的x軸我們要乘以一個(gè)-1才能按照我們平常理解的右手坐標(biāo)系來(lái)繪圖
之后我們套用上這個(gè)修復(fù)過(guò)的坐標(biāo)系來(lái)看看效果
完整代碼:
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);
}
下面分享一個(gè)PlayerBackCoordinate為了讓讀者能夠舉一反三,希望讀者能夠自行添加 z 軸的變化(思考:z軸的變化在右手坐標(biāo)系中是如何變化甘萧,又應(yīng)該如何將其轉(zhuǎn)換至MC坐標(biāo)系內(nèi))
繪圖思考可以參照這張圖:
import org.bukkit.Location;
/**
* 將玩家背后轉(zhuǎn)換為一個(gè)平面直角坐標(biāo)系
*
* @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); // 重設(shè)仰俯角
zeroDot.add(zeroDot.getDirection().multiply(-0.3)); // 使原點(diǎn)與玩家有一點(diǎn)點(diǎn)距離
}
public Location getZeroDot() {
return zeroDot;
}
public Location newLocation(double x, double y) {
return rotateLocationAboutPoint(zeroDot.clone().add(-x, y, 0), rotateAngle, zeroDot);
}
/**
* 在二維平面上利用給定的中心點(diǎn)逆時(shí)針旋轉(zhuǎn)一個(gè)點(diǎn)
*
* @param location 待旋轉(zhuǎn)的點(diǎn)
* @param angle 旋轉(zhuǎn)角度
* @param point 中心點(diǎn)
* @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);
}
}
上方代碼的使用:實(shí)例1:在玩家后背繪制一個(gè)圓
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);
}
具體效果:
制作簡(jiǎn)易翅膀
不說(shuō)這么多萝嘁,直接上代碼好吧,用的就是上面的代碼
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;
}
具體效果:
結(jié)語(yǔ)
是的這個(gè)教程又開(kāi)始更新了扬卷,就當(dāng)做沒(méi)事拿來(lái)玩玩的了牙言,畢竟上了大學(xué),還是可以拿線性代數(shù)來(lái)學(xué)以致用的嘛嘻嘻嘻怪得。此外我想開(kāi)一個(gè)ParticleLib的坑咱枉,專(zhuān)門(mén)來(lái)制作這類(lèi)特效,希望各位看官可以多多支持