#include <iostream>
#include <cstdio>
using namespace std;
static const int width = 512, height = 512;
static const int pixels_per_unit = 20;
pos[0] += vels[0] * pixels_per_unit * dt;
pos[1] += vels[1] * pixels_per_unit * dt;
forces[0] = -1 * diff_x / dist;
forces[1] = -1 * diff_y / dist;
forces[0] *= pixels_per_unit * dt;
forces[1] *= pixels_per_unit * dt;
vels[0] *= 1 - (0.005*dt);
vels[1] *= 1 - (0.005*dt);
vels[0] += forces[0];
vels[1] += forces[1];
}
const int radius = 50;
const float elastic_constant = 0.91f;
if(sum<int>(dist<radius) > 0) {
vels[0](dist<radius) = -elastic_constant * vels[0](dist<radius);
vels[1](dist<radius) = -elastic_constant * vels[1](dist<radius);
diff_x /= dist;
diff_y /= dist;
pos[0](dist<radius) = width/2 + diff_x(dist<radius) * radius;
pos[1](dist<radius) = height/2 + diff_y(dist<radius) * radius;
}
}
int main(int argc, char *argv[])
{
try {
const static int total_particles = 1000;
static const int reset = 500;
af::Window myWindow(width, height,
"Gravity Simulation using ArrayFire");
int frame_count = 0;
pos[1] =
af::randu(total_particles) * height;
while(!myWindow.close()) {
ids = (pos[0].
as(
u32) * height) + pos[1].as(
u32);
image(ids) += 255;
myWindow.image(image);
frame_count++;
if(frame_count % reset == 0) {
pos[1] =
af::randu(total_particles) * height;
}
collisions(pos, vels);
simulate(pos, vels, forces, dt);
}
fprintf(stderr,
"%s\n", e.
what());
throw;
}
return 0;
}
Window object to render af::arrays.
Definition: graphics.h:37
A multi dimensional data container.
Definition: array.h:27
Definition: exception.h:20
virtual const char * what() const
Definition: exception.h:34
@ u32
32-bit unsigned integral values
Definition: defines.h:202
AFAPI array sqrt(const array &in)
C++ Interface for square root of input.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
AFAPI array randn(const dim4 &dims, const dtype ty=f32)
AFAPI array randu(const dim4 &dims, const dtype ty=f32)
AFAPI array gaussianKernel(const int rows, const int cols, const double sig_r=0, const double sig_c=0)
C++ Interface for generating gausian kernels.
dim4 dims() const
Get dimensions of the array.
const array as(dtype type) const
Converts the array into another type.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface for maximum values in an array.
AFAPI array min(const array &in, const int dim=-1)
C++ Interface for minimum values in an array.
AFAPI array convolve2(const array &signal, const array &filter, const convMode mode=AF_CONV_DEFAULT, const convDomain domain=AF_CONV_AUTO)
C++ Interface for convolution on two dimensional signals.
Definition: algorithm.h:15
Internal timer object.
Definition: timing.h:29
static AFAPI timer start()
static AFAPI double stop()