This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
The-Powder-Toy/src/simulation/simtools/CYCL.cpp
2021-02-15 21:24:44 +01:00

53 lines
1.2 KiB
C++

#include "simulation/ToolCommon.h"
#include "simulation/Air.h"
#include <cmath>
static int perform(Simulation * sim, Particle * cpart, int x, int y, int brushX, int brushY, float strength);
void SimTool::Tool_CYCL()
{
Identifier = "DEFAULT_TOOL_CYCL";
Name = "CYCL";
Colour = PIXPACK(0x132f5b);
Description = "Cyclone, produces swirling air currents";
Perform = &perform;
}
static int perform(Simulation * sim, Particle * cpart, int x, int y, int brushX, int brushY, float strength)
{
/*
Air velocity calculation.
(x, y) -- turn 90 deg -> (-y, x)
*/
// only trigger once per cell (less laggy)
if ((x%CELL) == 0 && (y%CELL) == 0)
{
if(brushX == x && brushY == y)
return 1;
float *vx = &sim->air->vx[y / CELL][x / CELL];
float *vy = &sim->air->vy[y / CELL][x / CELL];
auto dvx = float(brushX - x);
auto dvy = float(brushY - y);
float invsqr = 1/sqrtf(dvx*dvx + dvy*dvy);
*vx -= (strength / 16) * (-dvy)*invsqr;
*vy -= (strength / 16) * dvx*invsqr;
// Clamp velocities
if (*vx > 256.0f)
*vx = 256.0f;
else if (*vx < -256.0f)
*vx = -256.0f;
if (*vy > 256.0f)
*vy = 256.0f;
else if (*vy < -256.0f)
*vy = -256.0f;
}
return 1;
}