the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 292 lines 5.7 kB view raw
1#include "stdafx.h" 2#include "Vec3.h" 3#include "AABB.h" 4 5DWORD Vec3::tlsIdx = 0; 6Vec3::ThreadStorage *Vec3::tlsDefault = NULL; 7 8Vec3::ThreadStorage::ThreadStorage() 9{ 10 pool = new Vec3[POOL_SIZE]; 11 poolPointer = 0; 12} 13 14Vec3::ThreadStorage::~ThreadStorage() 15{ 16 delete [] pool; 17} 18 19void Vec3::CreateNewThreadStorage() 20{ 21 ThreadStorage *tls = new ThreadStorage(); 22 if(tlsDefault == NULL ) 23 { 24 tlsIdx = TlsAlloc(); 25 tlsDefault = tls; 26 } 27 TlsSetValue(tlsIdx, tls); 28} 29 30void Vec3::UseDefaultThreadStorage() 31{ 32 TlsSetValue(tlsIdx, tlsDefault); 33} 34 35void Vec3::ReleaseThreadStorage() 36{ 37 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 38 if( tls == tlsDefault ) return; 39 40 delete tls; 41} 42 43Vec3 *Vec3::newPermanent(double x, double y, double z) 44{ 45 return new Vec3(x,y,z); 46}; 47 48void Vec3::clearPool() 49{ 50} 51 52void Vec3::resetPool() 53{ 54} 55 56Vec3 *Vec3::newTemp(double x, double y, double z) 57{ 58 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 59 Vec3 *thisVec = &tls->pool[tls->poolPointer]; 60 thisVec->set(x, y, z); 61 tls->poolPointer = ( tls->poolPointer + 1 ) % ThreadStorage::POOL_SIZE; 62 return thisVec; 63} 64 65Vec3::Vec3(double x, double y, double z) 66{ 67 if (x == -0.0) x = 0.0; 68 if (y == -0.0) y = 0.0; 69 if (z == -0.0) z = 0.0; 70 this->x = x; 71 this->y = y; 72 this->z = z; 73} 74 75Vec3 *Vec3::set(double x, double y, double z) 76{ 77 this->x = x; 78 this->y = y; 79 this->z = z; 80 return this; 81} 82 83 84Vec3 *Vec3::interpolateTo(Vec3 *t, double p) 85{ 86 double xt = x + (t->x - x) * p; 87 double yt = y + (t->y - y) * p; 88 double zt = z + (t->z - z) * p; 89 90 return Vec3::newTemp(xt, yt, zt); 91} 92 93Vec3 *Vec3::vectorTo(Vec3 *p) 94{ 95 return Vec3::newTemp(p->x - x, p->y - y, p->z - z); 96} 97 98Vec3 *Vec3::normalize() 99{ 100 double dist = (double) (sqrt(x * x + y * y + z * z)); 101 if (dist < 0.0001) return Vec3::newTemp(0, 0, 0); 102 return Vec3::newTemp(x / dist, y / dist, z / dist); 103} 104 105double Vec3::dot(Vec3 *p) 106{ 107 return x * p->x + y * p->y + z * p->z; 108} 109 110Vec3 *Vec3::cross(Vec3 *p) 111{ 112 return Vec3::newTemp(y * p->z - z * p->y, z * p->x - x * p->z, x * p->y - y * p->x); 113} 114 115Vec3 *Vec3::add(double x, double y, double z) 116{ 117 return Vec3::newTemp(this->x + x, this->y + y, this->z + z); 118} 119 120double Vec3::distanceTo(Vec3 *p) 121{ 122 double xd = p->x - x; 123 double yd = p->y - y; 124 double zd = p->z - z; 125 return (double) sqrt(xd * xd + yd * yd + zd * zd); 126} 127 128double Vec3::distanceToSqr(Vec3 *p) 129{ 130 double xd = p->x - x; 131 double yd = p->y - y; 132 double zd = p->z - z; 133 return xd * xd + yd * yd + zd * zd; 134} 135 136double Vec3::distanceToSqr(double x2, double y2, double z2) 137{ 138 double xd = x2 - x; 139 double yd = y2 - y; 140 double zd = z2 - z; 141 return xd * xd + yd * yd + zd * zd; 142} 143 144Vec3 *Vec3::scale(double l) 145{ 146 return Vec3::newTemp(x * l, y * l, z * l); 147} 148 149double Vec3::length() 150{ 151 return sqrt(x * x + y * y + z * z); 152} 153 154Vec3 *Vec3::clipX(Vec3 *b, double xt) 155{ 156 double xd = b->x - x; 157 double yd = b->y - y; 158 double zd = b->z - z; 159 160 if (xd * xd < 0.0000001f) return NULL; 161 162 double d = (xt - x) / xd; 163 if (d < 0 || d > 1) return NULL; 164 return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d); 165} 166 167Vec3 *Vec3::clipY(Vec3 *b, double yt) 168{ 169 double xd = b->x - x; 170 double yd = b->y - y; 171 double zd = b->z - z; 172 173 if (yd * yd < 0.0000001f) return NULL; 174 175 double d = (yt - y) / yd; 176 if (d < 0 || d > 1) return NULL; 177 return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d); 178} 179 180Vec3 *Vec3::clipZ(Vec3 *b, double zt) 181{ 182 double xd = b->x - x; 183 double yd = b->y - y; 184 double zd = b->z - z; 185 186 if (zd * zd < 0.0000001f) return NULL; 187 188 double d = (zt - z) / zd; 189 if (d < 0 || d > 1) return NULL; 190 return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d); 191} 192 193wstring Vec3::toString() 194{ 195 static wchar_t buf[128]; 196 swprintf(buf, 128, L"(%f,%f,%f)",x,y,z); 197 return wstring(buf); 198} 199 200Vec3 *Vec3::lerp(Vec3 *v, double a) 201{ 202 return Vec3::newTemp(x + (v->x - x) * a, y + (v->y - y) * a, z + (v->z - z) * a); 203} 204 205void Vec3::xRot(float degs) 206{ 207 double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here 208 double _sin = sin(degs); 209 210 double xx = x; 211 double yy = y * _cos + z * _sin; 212 double zz = z * _cos - y * _sin; 213 214 x = xx; 215 y = yy; 216 z = zz; 217} 218 219void Vec3::yRot(float degs) 220{ 221 double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here 222 double _sin = sin(degs); 223 224 double xx = x * _cos + z * _sin; 225 double yy = y; 226 double zz = z * _cos - x * _sin; 227 228 x = xx; 229 y = yy; 230 z = zz; 231} 232 233void Vec3::zRot(float degs) 234{ 235 double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here 236 double _sin = sin(degs); 237 238 double xx = x * _cos + y * _sin; 239 double yy = y * _cos - x * _sin; 240 double zz = z; 241 242 x = xx; 243 y = yy; 244 z = zz; 245} 246 247// Returns 0 if this point is within the box 248// Otherwise returns the distance to the box 249double Vec3::distanceTo(AABB *box) 250{ 251 if(box->contains(this)) return 0; 252 253 double xd = 0, yd = 0, zd = 0; 254 255 if(x < box->x0) xd = box->x0 - x; 256 else if( x > box->x1) xd = x - box->x1; 257 258 if(y < box->y0) yd = box->y0 - y; 259 else if( y > box->y1) yd = y - box->y1; 260 261 if(z < box->z0) zd = box->z0 - z; 262 else if( z > box->z1) zd = z - box->z1; 263 264 return sqrt(xd * xd + yd * yd + zd * zd); 265} 266 267 268 269Vec3* Vec3::closestPointOnLine(Vec3* p1, Vec3* p2) 270{ 271 Vec3* diff = newTemp(x-p1->x, y-p1->y, z-p1->z); 272 Vec3* dir = newTemp(p2->x-p1->x, p2->y-p1->y, p2->z-p1->z); 273 float dot1 = diff->dot(dir); 274 if (dot1 <= 0.0f) 275 return p1; 276 277 float dot2 = dir->dot(dir); 278 279 if (dot2 <= dot1) 280 return p2; 281 282 float t=dot1/dot2; 283 return newTemp(p1->x + t * dir->x, p1->y + t * dir->y, p1->z + t * dir->z); 284} 285 286 287double Vec3::distanceFromLine(Vec3* p1, Vec3* p2) 288{ 289 Vec3* closestPoint = closestPointOnLine(p1, p2); 290 Vec3* diff = newTemp(x-closestPoint->x, y-closestPoint->y, z-closestPoint->z); 291 return diff->length(); 292}