2014-04-30 08:50:12 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-04-30 08:50:12 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
Original file from GamePlay3D: http://gameplay3d.org
|
|
|
|
|
|
|
|
This file was modified to fit the cocos2d-x project
|
|
|
|
*/
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/Vec3.h"
|
|
|
|
#include "math/MathUtil.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-04-02 12:09:51 +08:00
|
|
|
NS_CC_MATH_BEGIN
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3::Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3::Vec3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3::Vec3(const float* array)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
set(array);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3::Vec3(const Vec3& p1, const Vec3& p2)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
set(p1, p2);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 Vec3::fromColor(unsigned int color)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float components[3];
|
|
|
|
int componentIndex = 0;
|
|
|
|
for (int i = 2; i >= 0; --i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int component = (color >> i * 8) & 0x0000ff;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
components[componentIndex++] = static_cast<float>(component) / 255.0f;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 value(components);
|
2014-04-02 10:33:07 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Vec3::angle(const Vec3& v1, const Vec3& v2)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float dx = v1.y * v2.z - v1.z * v2.y;
|
|
|
|
float dy = v1.z * v2.x - v1.x * v2.z;
|
|
|
|
float dz = v1.x * v2.y - v1.y * v2.x;
|
|
|
|
|
2016-07-07 00:38:27 +08:00
|
|
|
return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::add(const Vec3& v1, const Vec3& v2, Vec3* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = v1.x + v2.x;
|
|
|
|
dst->y = v1.y + v2.y;
|
|
|
|
dst->z = v1.z + v2.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::clamp(const Vec3& min, const Vec3& max)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
|
|
|
|
|
|
|
|
// Clamp the x value.
|
|
|
|
if (x < min.x)
|
|
|
|
x = min.x;
|
|
|
|
if (x > max.x)
|
|
|
|
x = max.x;
|
|
|
|
|
|
|
|
// Clamp the y value.
|
|
|
|
if (y < min.y)
|
|
|
|
y = min.y;
|
|
|
|
if (y > max.y)
|
|
|
|
y = max.y;
|
|
|
|
|
|
|
|
// Clamp the z value.
|
|
|
|
if (z < min.z)
|
|
|
|
z = min.z;
|
|
|
|
if (z > max.z)
|
|
|
|
z = max.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::clamp(const Vec3& v, const Vec3& min, const Vec3& max, Vec3* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
|
|
|
|
|
|
|
|
// Clamp the x value.
|
|
|
|
dst->x = v.x;
|
|
|
|
if (dst->x < min.x)
|
|
|
|
dst->x = min.x;
|
|
|
|
if (dst->x > max.x)
|
|
|
|
dst->x = max.x;
|
|
|
|
|
|
|
|
// Clamp the y value.
|
|
|
|
dst->y = v.y;
|
|
|
|
if (dst->y < min.y)
|
|
|
|
dst->y = min.y;
|
|
|
|
if (dst->y > max.y)
|
|
|
|
dst->y = max.y;
|
|
|
|
|
|
|
|
// Clamp the z value.
|
|
|
|
dst->z = v.z;
|
|
|
|
if (dst->z < min.z)
|
|
|
|
dst->z = min.z;
|
|
|
|
if (dst->z > max.z)
|
|
|
|
dst->z = max.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::cross(const Vec3& v)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
cross(*this, v, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::cross(const Vec3& v1, const Vec3& v2, Vec3* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
// NOTE: This code assumes Vec3 struct members are contiguous floats in memory.
|
2014-04-02 10:33:07 +08:00
|
|
|
// We might want to revisit this (and other areas of code that make this assumption)
|
|
|
|
// later to guarantee 100% safety/compatibility.
|
2014-05-15 01:07:09 +08:00
|
|
|
MathUtil::crossVec3(&v1.x, &v2.x, &dst->x);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Vec3::distance(const Vec3& v) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float dx = v.x - x;
|
|
|
|
float dy = v.y - y;
|
|
|
|
float dz = v.z - z;
|
|
|
|
|
2016-07-07 00:38:27 +08:00
|
|
|
return std::sqrt(dx * dx + dy * dy + dz * dz);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Vec3::distanceSquared(const Vec3& v) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float dx = v.x - x;
|
|
|
|
float dy = v.y - y;
|
|
|
|
float dz = v.z - z;
|
|
|
|
|
|
|
|
return (dx * dx + dy * dy + dz * dz);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Vec3::dot(const Vec3& v) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
return (x * v.x + y * v.y + z * v.z);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Vec3::dot(const Vec3& v1, const Vec3& v2)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::normalize()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float n = x * x + y * y + z * z;
|
|
|
|
// Already normalized.
|
|
|
|
if (n == 1.0f)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-07-07 00:38:27 +08:00
|
|
|
n = std::sqrt(n);
|
2014-04-02 10:33:07 +08:00
|
|
|
// Too close to zero.
|
|
|
|
if (n < MATH_TOLERANCE)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-02 10:33:07 +08:00
|
|
|
n = 1.0f / n;
|
2014-04-30 19:50:36 +08:00
|
|
|
x *= n;
|
|
|
|
y *= n;
|
|
|
|
z *= n;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 Vec3::getNormalized() const
|
2014-04-30 19:50:36 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 v(*this);
|
2014-04-30 19:50:36 +08:00
|
|
|
v.normalize();
|
|
|
|
return v;
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::subtract(const Vec3& v1, const Vec3& v2, Vec3* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = v1.x - v2.x;
|
|
|
|
dst->y = v1.y - v2.y;
|
|
|
|
dst->z = v1.z - v2.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Vec3::smooth(const Vec3& target, float elapsedTime, float responseTime)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
if (elapsedTime > 0)
|
|
|
|
{
|
|
|
|
*this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
const Vec3 Vec3::ZERO(0.0f, 0.0f, 0.0f);
|
|
|
|
const Vec3 Vec3::ONE(1.0f, 1.0f, 1.0f);
|
|
|
|
const Vec3 Vec3::UNIT_X(1.0f, 0.0f, 0.0f);
|
|
|
|
const Vec3 Vec3::UNIT_Y(0.0f, 1.0f, 0.0f);
|
|
|
|
const Vec3 Vec3::UNIT_Z(0.0f, 0.0f, 1.0f);
|
2014-05-06 09:07:58 +08:00
|
|
|
|
2014-04-02 12:09:51 +08:00
|
|
|
NS_CC_MATH_END
|