mirror of
https://gitea.sffempire.ru/Kolyah35/minecraft-pe-0.6.1.git
synced 2026-03-20 23:13:33 +00:00
the whole game
This commit is contained in:
249
src/world/phys/AABB.h
Executable file
249
src/world/phys/AABB.h
Executable file
@@ -0,0 +1,249 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_PHYS__AABB_H__
|
||||
#define NET_MINECRAFT_WORLD_PHYS__AABB_H__
|
||||
|
||||
//package net.minecraft.world.phys;
|
||||
|
||||
#include <vector>
|
||||
#include "Vec3.h"
|
||||
#include "HitResult.h"
|
||||
#include <sstream>
|
||||
|
||||
class AABB
|
||||
{
|
||||
public:
|
||||
AABB()
|
||||
: x0(0), y0(0), z0(0),
|
||||
x1(1), y1(1), z1(1)
|
||||
{
|
||||
}
|
||||
|
||||
AABB(float x0, float y0, float z0, float x1, float y1, float z1) {
|
||||
this->x0 = x0;
|
||||
this->y0 = y0;
|
||||
this->z0 = z0;
|
||||
this->x1 = x1;
|
||||
this->y1 = y1;
|
||||
this->z1 = z1;
|
||||
}
|
||||
|
||||
AABB* set(float x0, float y0, float z0, float x1, float y1, float z1) {
|
||||
this->x0 = x0;
|
||||
this->y0 = y0;
|
||||
this->z0 = z0;
|
||||
this->x1 = x1;
|
||||
this->y1 = y1;
|
||||
this->z1 = z1;
|
||||
return this;
|
||||
}
|
||||
|
||||
AABB expand(float xa, float ya, float za) {
|
||||
float _x0 = x0;
|
||||
float _y0 = y0;
|
||||
float _z0 = z0;
|
||||
float _x1 = x1;
|
||||
float _y1 = y1;
|
||||
float _z1 = z1;
|
||||
|
||||
if (xa < 0) _x0 += xa;
|
||||
if (xa > 0) _x1 += xa;
|
||||
|
||||
if (ya < 0) _y0 += ya;
|
||||
if (ya > 0) _y1 += ya;
|
||||
|
||||
if (za < 0) _z0 += za;
|
||||
if (za > 0) _z1 += za;
|
||||
|
||||
return AABB(_x0, _y0, _z0, _x1, _y1, _z1);
|
||||
}
|
||||
|
||||
AABB grow(float xa, float ya, float za) const {
|
||||
return AABB(x0 - xa, y0 - ya, z0 - za, x1 + xa, y1 + ya, z1 + za);
|
||||
}
|
||||
|
||||
AABB cloneMove(float xa, float ya, float za) const {
|
||||
return AABB(x0 + xa, y0 + ya, z0 + za, x1 + xa, y1 + ya, z1 + za);
|
||||
}
|
||||
|
||||
float clipXCollide(const AABB& c, float xa) const {
|
||||
if (c.y1 <= y0 || c.y0 >= y1) return xa;
|
||||
if (c.z1 <= z0 || c.z0 >= z1) return xa;
|
||||
|
||||
if (xa > 0 && c.x1 <= x0) {
|
||||
float max = x0 - c.x1;
|
||||
if (max < xa) xa = max;
|
||||
}
|
||||
if (xa < 0 && c.x0 >= x1) {
|
||||
float max = x1 - c.x0;
|
||||
if (max > xa) xa = max;
|
||||
}
|
||||
|
||||
return xa;
|
||||
}
|
||||
|
||||
float clipYCollide(const AABB& c, float ya) const {
|
||||
if (c.x1 <= x0 || c.x0 >= x1) return ya;
|
||||
if (c.z1 <= z0 || c.z0 >= z1) return ya;
|
||||
|
||||
if (ya > 0 && c.y1 <= y0) {
|
||||
float max = y0 - c.y1;
|
||||
if (max < ya) ya = max;
|
||||
}
|
||||
if (ya < 0 && c.y0 >= y1) {
|
||||
float max = y1 - c.y0;
|
||||
if (max > ya) ya = max;
|
||||
}
|
||||
|
||||
return ya;
|
||||
}
|
||||
|
||||
float clipZCollide(const AABB& c, float za) const {
|
||||
if (c.x1 <= x0 || c.x0 >= x1) return za;
|
||||
if (c.y1 <= y0 || c.y0 >= y1) return za;
|
||||
|
||||
if (za > 0 && c.z1 <= z0) {
|
||||
float max = z0 - c.z1;
|
||||
if (max < za) za = max;
|
||||
}
|
||||
if (za < 0 && c.z0 >= z1) {
|
||||
float max = z1 - c.z0;
|
||||
if (max > za) za = max;
|
||||
}
|
||||
|
||||
return za;
|
||||
}
|
||||
|
||||
bool intersects(const AABB& c) const {
|
||||
if (c.x1 <= x0 || c.x0 >= x1) return false;
|
||||
if (c.y1 <= y0 || c.y0 >= y1) return false;
|
||||
if (c.z1 <= z0 || c.z0 >= z1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool intersectsInner(const AABB& c) const {
|
||||
if (c.x1 < x0 || c.x0 > x1) return false;
|
||||
if (c.y1 < y0 || c.y0 > y1) return false;
|
||||
if (c.z1 < z0 || c.z0 > z1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
AABB* move(float xa, float ya, float za) {
|
||||
x0 += xa;
|
||||
y0 += ya;
|
||||
z0 += za;
|
||||
x1 += xa;
|
||||
y1 += ya;
|
||||
z1 += za;
|
||||
return this;
|
||||
}
|
||||
|
||||
bool intersects(float x02, float y02, float z02, float x12, float y12, float z12) const {
|
||||
if (x12 <= x0 || x02 >= x1) return false;
|
||||
if (y12 <= y0 || y02 >= y1) return false;
|
||||
if (z12 <= z0 || z02 >= z1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool contains(const Vec3& p) const {
|
||||
if (p.x <= x0 || p.x >= x1) return false;
|
||||
if (p.y <= y0 || p.y >= y1) return false;
|
||||
if (p.z <= z0 || p.z >= z1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
float getSize() const {
|
||||
float xs = x1 - x0;
|
||||
float ys = y1 - y0;
|
||||
float zs = z1 - z0;
|
||||
return (xs + ys + zs) / 3.0f;
|
||||
}
|
||||
|
||||
AABB shrink(float xa, float ya, float za) {
|
||||
return AABB(x0 + xa, y0 + ya, z0 + za, x1 - xa, y1 - ya, z1 - za);
|
||||
}
|
||||
|
||||
AABB copy() {
|
||||
return AABB(x0, y0, z0, x1, y1, z1);
|
||||
}
|
||||
|
||||
HitResult clip(const Vec3& a, const Vec3& b) {
|
||||
Vec3 xh0, xh1, yh0, yh1, zh0, zh1;
|
||||
|
||||
bool bxh0 = a.clipX(b, x0, xh0);
|
||||
bool bxh1 = a.clipX(b, x1, xh1);
|
||||
|
||||
bool byh0 = a.clipY(b, y0, yh0);
|
||||
bool byh1 = a.clipY(b, y1, yh1);
|
||||
|
||||
bool bzh0 = a.clipZ(b, z0, zh0);
|
||||
bool bzh1 = a.clipZ(b, z1, zh1);
|
||||
|
||||
//if (!containsX(xh0)) xh0 = NULL;
|
||||
if (!bxh0 || !containsX(&xh0)) bxh0 = false;
|
||||
if (!bxh1 || !containsX(&xh1)) bxh1 = false;
|
||||
if (!byh0 || !containsY(&yh0)) byh0 = false;
|
||||
if (!byh1 || !containsY(&yh1)) byh1 = false;
|
||||
if (!bzh0 || !containsZ(&zh0)) bzh0 = false;
|
||||
if (!bzh1 || !containsZ(&zh1)) bzh1 = false;
|
||||
|
||||
Vec3* closest = NULL;
|
||||
|
||||
//if (xh0 != NULL && (closest == NULL || a.distanceToSqr(xh0) < a.distanceToSqr(closest))) closest = xh0;
|
||||
if (bxh0 && (closest == NULL || a.distanceToSqr(xh0) < a.distanceToSqr(*closest))) closest = &xh0;
|
||||
if (bxh1 && (closest == NULL || a.distanceToSqr(xh1) < a.distanceToSqr(*closest))) closest = &xh1;
|
||||
if (byh0 && (closest == NULL || a.distanceToSqr(yh0) < a.distanceToSqr(*closest))) closest = &yh0;
|
||||
if (byh1 && (closest == NULL || a.distanceToSqr(yh1) < a.distanceToSqr(*closest))) closest = &yh1;
|
||||
if (bzh0 && (closest == NULL || a.distanceToSqr(zh0) < a.distanceToSqr(*closest))) closest = &zh0;
|
||||
if (bzh1 && (closest == NULL || a.distanceToSqr(zh1) < a.distanceToSqr(*closest))) closest = &zh1;
|
||||
|
||||
if (closest == NULL)
|
||||
return HitResult();
|
||||
|
||||
int face = -1;
|
||||
|
||||
if (closest == &xh0) face = 4;
|
||||
if (closest == &xh1) face = 5;
|
||||
if (closest == &yh0) face = 0;
|
||||
if (closest == &yh1) face = 1;
|
||||
if (closest == &zh0) face = 2;
|
||||
if (closest == &zh1) face = 3;
|
||||
|
||||
return HitResult(0, 0, 0, face, *closest);
|
||||
}
|
||||
|
||||
private:
|
||||
bool containsX(Vec3* v) {
|
||||
// if (!v) return false;
|
||||
return v->y >= y0 && v->y <= y1 && v->z >= z0 && v->z <= z1;
|
||||
}
|
||||
|
||||
bool containsY(Vec3* v) {
|
||||
// if (!v) return false;
|
||||
return v->x >= x0 && v->x <= x1 && v->z >= z0 && v->z <= z1;
|
||||
}
|
||||
|
||||
bool containsZ(Vec3* v) {
|
||||
// if (!v) return false;
|
||||
return v->x >= x0 && v->x <= x1 && v->y >= y0 && v->y <= y1;
|
||||
}
|
||||
|
||||
public:
|
||||
void set(const AABB& b) {
|
||||
this->x0 = b.x0;
|
||||
this->y0 = b.y0;
|
||||
this->z0 = b.z0;
|
||||
this->x1 = b.x1;
|
||||
this->y1 = b.y1;
|
||||
this->z1 = b.z1;
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
std::stringstream ss;
|
||||
ss << "AABB(" << x0 << "," << y0 << "," << z0 << " to " << x1 << "," << y1 << "," << z1 << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
float x0, y0, z0;
|
||||
float x1, y1, z1;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_PHYS__AABB_H__*/
|
||||
49
src/world/phys/HitResult.cpp
Executable file
49
src/world/phys/HitResult.cpp
Executable file
@@ -0,0 +1,49 @@
|
||||
#include "HitResult.h"
|
||||
#include "../entity/Entity.h"
|
||||
|
||||
|
||||
HitResult::HitResult()
|
||||
: type(NO_HIT),
|
||||
entity(NULL),
|
||||
indirectHit(false)
|
||||
{}
|
||||
|
||||
HitResult::HitResult(int x, int y, int z, int f, const Vec3& pos)
|
||||
: type(TILE),
|
||||
x(x),
|
||||
y(y),
|
||||
z(z),
|
||||
f(f),
|
||||
pos(pos),
|
||||
indirectHit(false),
|
||||
entity(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
HitResult::HitResult(Entity* entity)
|
||||
: type(ENTITY),
|
||||
entity(entity),
|
||||
pos(entity->x, entity->y, entity->z),
|
||||
indirectHit(false)
|
||||
{
|
||||
}
|
||||
|
||||
HitResult::HitResult(const HitResult& hr)
|
||||
: type(hr.type),
|
||||
x(hr.x),
|
||||
y(hr.y),
|
||||
z(hr.z),
|
||||
f(hr.f),
|
||||
pos(hr.pos),
|
||||
indirectHit(hr.indirectHit),
|
||||
entity(hr.entity)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
float HitResult::distanceTo(Entity* entity) const {
|
||||
float xd = pos.x - entity->x;
|
||||
float yd = pos.y - entity->y;
|
||||
float zd = pos.z - entity->z;
|
||||
return xd * xd + yd * yd + zd * zd;
|
||||
}
|
||||
34
src/world/phys/HitResult.h
Executable file
34
src/world/phys/HitResult.h
Executable file
@@ -0,0 +1,34 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_PHYS__HitResult_H__
|
||||
#define NET_MINECRAFT_WORLD_PHYS__HitResult_H__
|
||||
|
||||
//package net.minecraft.world.phys;
|
||||
|
||||
#include "Vec3.h"
|
||||
class Entity;
|
||||
|
||||
enum HitResultType {
|
||||
TILE,
|
||||
ENTITY,
|
||||
NO_HIT
|
||||
};
|
||||
|
||||
class HitResult
|
||||
{
|
||||
public:
|
||||
HitResult();
|
||||
HitResult(int x, int y, int z, int f, const Vec3& pos);
|
||||
HitResult(Entity* entity);
|
||||
HitResult(const HitResult& hr);
|
||||
|
||||
float distanceTo(Entity* entity) const ;
|
||||
|
||||
bool isHit() const { return type != NO_HIT; }
|
||||
|
||||
HitResultType type;
|
||||
int x, y, z, f;
|
||||
Vec3 pos;
|
||||
Entity* entity;
|
||||
bool indirectHit; // when targeting the block we're standing on, without actually looking at it
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_PHYS__HitResult_H__*/
|
||||
231
src/world/phys/Vec3.h
Executable file
231
src/world/phys/Vec3.h
Executable file
@@ -0,0 +1,231 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_PHYS__Vec3_H__
|
||||
#define NET_MINECRAFT_WORLD_PHYS__Vec3_H__
|
||||
|
||||
//package net.minecraft.world.phys;
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
class Vec3
|
||||
{
|
||||
public:
|
||||
Vec3()
|
||||
: x(0),
|
||||
y(0),
|
||||
z(0)
|
||||
{}
|
||||
|
||||
Vec3(float x, float y, float z) {
|
||||
if (x == -0.0f) x = 0.0f;
|
||||
if (y == -0.0f) y = 0.0f;
|
||||
if (z == -0.0f) z = 0.0f;
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
Vec3* set(float x, float y, float z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
Vec3 operator+(const Vec3& rhs) const {
|
||||
return Vec3(x + rhs.x, y + rhs.y, z + rhs.z);
|
||||
}
|
||||
Vec3& operator+=(const Vec3& rhs) {
|
||||
x += rhs.x;
|
||||
y += rhs.y;
|
||||
z += rhs.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3 operator-(const Vec3& rhs) const {
|
||||
return Vec3(x - rhs.x, y - rhs.y, z - rhs.z);
|
||||
}
|
||||
Vec3& operator-=(const Vec3& rhs) {
|
||||
x -= rhs.x;
|
||||
y -= rhs.y;
|
||||
z -= rhs.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3 operator*(float k) const {
|
||||
return Vec3(x * k, y * k, z * k);
|
||||
}
|
||||
Vec3& operator*=(float k) {
|
||||
x *= k;
|
||||
y *= k;
|
||||
z *= k;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3 normalized() const {
|
||||
float dist = sqrt(x * x + y * y + z * z);
|
||||
if (dist < 0.0001f) return Vec3();
|
||||
return Vec3(x / dist, y / dist, z / dist); // newTemp
|
||||
}
|
||||
|
||||
float dot(const Vec3& p) const {
|
||||
return x * p.x + y * p.y + z * p.z;
|
||||
}
|
||||
|
||||
Vec3 cross(const Vec3& p) const {
|
||||
return Vec3(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); // newTemp
|
||||
}
|
||||
|
||||
Vec3 add(float x, float y, float z) const {
|
||||
return Vec3(this->x + x, this->y + y, this->z + z); // newTemp
|
||||
}
|
||||
Vec3& addSelf(float x, float y, float z) {
|
||||
this->x += x;
|
||||
this->y += y;
|
||||
this->z += z;
|
||||
return *this;
|
||||
}
|
||||
Vec3 sub(float x, float y, float z) const {
|
||||
return Vec3(this->x - x, this->y - y, this->z - z); // newTemp
|
||||
}
|
||||
Vec3& subSelf(float x, float y, float z) {
|
||||
this->x -= x;
|
||||
this->y -= y;
|
||||
this->z -= z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void negate() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
}
|
||||
|
||||
Vec3 negated() const {
|
||||
return Vec3(-x, -y, -z);
|
||||
}
|
||||
|
||||
float distanceTo(const Vec3& p) const {
|
||||
float xd = p.x - x;
|
||||
float yd = p.y - y;
|
||||
float zd = p.z - z;
|
||||
return sqrt(xd * xd + yd * yd + zd * zd);
|
||||
}
|
||||
|
||||
float distanceToSqr(const Vec3& p) const {
|
||||
float xd = p.x - x;
|
||||
float yd = p.y - y;
|
||||
float zd = p.z - z;
|
||||
return xd * xd + yd * yd + zd * zd;
|
||||
}
|
||||
|
||||
float distanceToSqr(float x2, float y2, float z2) const {
|
||||
float xd = x2 - x;
|
||||
float yd = y2 - y;
|
||||
float zd = z2 - z;
|
||||
return xd * xd + yd * yd + zd * zd;
|
||||
}
|
||||
|
||||
float length() const {
|
||||
return sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
bool clipX(const Vec3& b, float xt, Vec3& result) const {
|
||||
float xd = b.x - x;
|
||||
float yd = b.y - y;
|
||||
float zd = b.z - z;
|
||||
|
||||
if (xd * xd < 0.0000001f) return false;
|
||||
|
||||
float d = (xt - x) / xd;
|
||||
if (d < 0 || d > 1) return false;
|
||||
|
||||
result.set(x + xd * d, y + yd * d, z + zd * d);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool clipY(const Vec3& b, float yt, Vec3& result) const {
|
||||
float xd = b.x - x;
|
||||
float yd = b.y - y;
|
||||
float zd = b.z - z;
|
||||
|
||||
if (yd * yd < 0.0000001f) return false;
|
||||
|
||||
float d = (yt - y) / yd;
|
||||
if (d < 0 || d > 1) return false;
|
||||
|
||||
result.set(x + xd * d, y + yd * d, z + zd * d);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool clipZ(const Vec3& b, float zt, Vec3& result) const {
|
||||
float xd = b.x - x;
|
||||
float yd = b.y - y;
|
||||
float zd = b.z - z;
|
||||
|
||||
if (zd * zd < 0.0000001f) return false;
|
||||
|
||||
float d = (zt - z) / zd;
|
||||
if (d < 0 || d > 1) return false;
|
||||
|
||||
result.set(x + xd * d, y + yd * d, z + zd * d);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
std::stringstream ss;
|
||||
ss << "Vec3(" << x << "," << y << "," << z << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Vec3 lerp(const Vec3& v, float a) const {
|
||||
return Vec3(x + (v.x - x) * a, y + (v.y - y) * a, z + (v.z - z) * a); // newTemp
|
||||
}
|
||||
|
||||
void xRot(float degs) {
|
||||
float cosv = cos(degs);
|
||||
float sinv = sin(degs);
|
||||
|
||||
float xx = x;
|
||||
float yy = y * cosv + z * sinv;
|
||||
float zz = z * cosv - y * sinv;
|
||||
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
this->z = zz;
|
||||
}
|
||||
|
||||
void yRot(float degs) {
|
||||
float cosv = cos(degs);
|
||||
float sinv = sin(degs);
|
||||
|
||||
float xx = x * cosv + z * sinv;
|
||||
float yy = y;
|
||||
float zz = z * cosv - x * sinv;
|
||||
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
this->z = zz;
|
||||
}
|
||||
|
||||
void zRot(float degs) {
|
||||
float cosv = cos(degs);
|
||||
float sinv = sin(degs);
|
||||
|
||||
float xx = x * cosv + y * sinv;
|
||||
float yy = y * cosv - x * sinv;
|
||||
float zz = z;
|
||||
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
this->z = zz;
|
||||
}
|
||||
|
||||
static Vec3 fromPolarXY(float angle, float radius) {
|
||||
return Vec3( radius * cos(angle), radius * sin(angle), 0 );
|
||||
}
|
||||
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_PHYS__Vec3_H__*/
|
||||
Reference in New Issue
Block a user