Add ultrafast semi-accurate trig functions and update Cylone to use them. (#547)

This commit is contained in:
jombo23 2018-04-05 23:04:57 -04:00 committed by jacob1
parent 7f985b47dc
commit 564a9dfc48
4 changed files with 6721 additions and 5 deletions

6582
data/TrigTables.h Normal file

File diff suppressed because it is too large Load Diff

110
src/common/tpt-math.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <cmath>
#include "TrigTables.h"
float orig_atan(float val)
{
return atan(val);
}
namespace tpt
{
float sin(float angle)
{
angle *= 81.4873;
int i = (int)angle % 512;
if (i < 0)
{
i += 512;
}
return sineLookupTable[i];
}
float cos(float angle)
{
angle *= 81.4873;
int i = (int)angle % 512;
if (i < 0)
{
i += 512;
}
return cosineLookupTable[i];
}
float tan(float angle)
{
angle *= 81.4873;
int i = (int)angle % 512;
if (i < 0)
{
i += 512;
}
return tanLookupTable[i];
}
float asin(float angle)
{
angle *= 81.4873;
if (angle > 256 || angle < 0)
{
return 0.0;
}
return asinLookupTable[(int)(angle + 256)];
}
float acos(float angle)
{
angle *= 81.4873;
if (angle > 256 || angle < 0)
{
return 0.0;
}
return acosLookupTable[(int)(angle + 256)];
}
float atan(float ratio)
{
if (ratio > 20)
{
return orig_atan(ratio);
}
if (ratio < -20)
{
return orig_atan(ratio);
}
return atanLookupTable[(int)(ratio * 100) + 2000];
}
float atan2(float y, float x)
{
if (x > 0)
{
return tpt::atan(y / x);
}
else if (x < 0)
{
if (y >= 0)
{
return tpt::atan(y / x) + M_PI;
}
return tpt::atan(y / x) - M_PI;
}
else if (y > 0)
return M_PI_2;
else if (y < 0)
return M_PI_2;
else
return 0.0f;
}
}

21
src/common/tpt-math.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TPT_MATH_
#define TPT_MATH_
namespace tpt
{
float sin(float angle);
float cos(float angle);
float tan(float angle);
float asin(float angle);
float acos(float angle);
float atan(float ratio);
float atan2(float y, float x);
}
#endif /* TPT_MATH_ */

View File

@ -1,5 +1,8 @@
#include "ToolClasses.h"
#include "simulation/Air.h"
#include "common/tpt-math.h"
//#TPT-Directive ToolClass Tool_Cycl TOOL_CYCL 7
Tool_Cycl::Tool_Cycl()
{
@ -16,16 +19,16 @@ int Tool_Cycl::Perform(Simulation * sim, Particle * cpart, int x, int y, int bru
Air velocity X = cosine of cell angle
Angle of cell is calculated via cells X/Y relation to the brush center and arctangent
Angle has 1.57 radians added to it (90 degrees) in order to make the velocity be at 90 degrees to the centerpoint.
Ditto for X, except X uses sine
Ditto for Y, except Y uses sine
*/
// only trigger once per cell (less laggy)
if ((x%CELL) == 0 && (y%CELL) == 0)
{
float *vx = &sim->air->vx[y/CELL][x/CELL];
float *vy = &sim->air->vy[y/CELL][x/CELL];
float *vx = &sim->air->vx[y / CELL][x / CELL];
float *vy = &sim->air->vy[y / CELL][x / CELL];
*vx -= (strength / 16) * (cos(1.57f + (atan2(brushY - y, brushX - x))));
*vy -= (strength / 16) * (sin(1.57f + (atan2(brushY - y, brushX - x))));
*vx -= (strength / 16) * (tpt::cos(1.57f + (tpt::atan2(brushY - y, brushX - x))));
*vy -= (strength / 16) * (tpt::sin(1.57f + (tpt::atan2(brushY - y, brushX - x))));
// Clamp velocities
if (*vx > 256.0f)