ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Magnet.cpp
Go to the documentation of this file.
1#include "Magnet.h"
2
3Magnet::Magnet(float amplitude) : amplitude(amplitude) {}
4
6 this->offset = offset;
7}
8
9void Magnet::SetAmplitude(float amplitude) {
10 this->amplitude = amplitude;
11}
12
14 RGBColor* pixelColors = pixelGroup->GetColors();
15 RGBColor* colorBuffer = pixelGroup->GetColorBuffer();
16 Vector2D mid = pixelGroup->GetCenterCoordinate();
17 uint16_t tIndex = 0;
18
19 for (uint16_t i = 0; i < pixelGroup->GetPixelCount(); i++) {
20 Vector2D pos = pixelGroup->GetCoordinate(i) + offset;
21 Vector2D dif = pos - mid + Vector2D(0.0f, 50.0f);
22 float distance = fabsf(pos.CalculateEuclideanDistance(mid));
23
24 float theta = atan2f(dif.Y, dif.X);
25 float newDistance = (1.0f / distance) * 0.5f * amplitude * 4.0f; // * 10000.0f;//fGenSize.Update();
26
27 int offsetX = (int)(newDistance * cosf(theta));
28 int offsetY = (int)(newDistance * sinf(theta));
29
30 // Use this function to find the index of the pixel at the offset x y pixel
31 if (pixelGroup->GetOffsetXYIndex(i, &tIndex, offsetX, offsetY)) {
32 colorBuffer[i].R = pixelColors[tIndex].R; // copy the current pixel color into the buffer
33 colorBuffer[i].G = pixelColors[tIndex].G;
34 colorBuffer[i].B = pixelColors[tIndex].B;
35 } else {
36 colorBuffer[i].R = 0; // copy the current pixel color into the buffer
37 colorBuffer[i].G = 0;
38 colorBuffer[i].B = 0;
39 }
40 }
41
42 for (uint16_t i = 0; i < pixelGroup->GetPixelCount(); i++) {
43 pixelColors[i].R = colorBuffer[i].R;
44 pixelColors[i].G = colorBuffer[i].G;
45 pixelColors[i].B = colorBuffer[i].B;
46 }
47}
Defines the Magnet effect class for creating magnetic distortion effects on pixel groups.
Interface for managing and interacting with a collection of pixels.
Definition IPixelGroup.h:25
virtual RGBColor * GetColors()=0
Retrieves the array of colors for the pixel group.
virtual RGBColor * GetColorBuffer()=0
Retrieves the color buffer for the pixel group.
virtual Vector2D GetCoordinate(uint16_t count)=0
Retrieves the coordinate of a specific pixel.
virtual bool GetOffsetXYIndex(uint16_t count, uint16_t *index, int x1, int y1)=0
Retrieves an offset XY-axis index for a given pixel.
virtual uint16_t GetPixelCount()=0
Retrieves the total number of pixels in the group.
virtual Vector2D GetCenterCoordinate()=0
Retrieves the center coordinate of the pixel group.
float amplitude
The strength of the magnetic distortion.
Definition Magnet.h:28
void SetAmplitude(float amplitude)
Sets the amplitude of the magnetic distortion effect.
Definition Magnet.cpp:9
void SetPosition(Vector2D offset)
Sets the central position of the magnetic effect.
Definition Magnet.cpp:5
void ApplyEffect(IPixelGroup *pixelGroup) override
Applies the magnetic distortion effect to the given pixel group.
Definition Magnet.cpp:13
Magnet(float amplitude=0.5f)
Constructs a Magnet effect with a specified amplitude.
Definition Magnet.cpp:3
Vector2D offset
The central offset of the magnetic effect.
Definition Magnet.h:27
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
uint8_t B
Blue component of the color (0-255).
Definition RGBColor.h:27
uint8_t G
Green component of the color (0-255).
Definition RGBColor.h:26
uint8_t R
Red component of the color (0-255).
Definition RGBColor.h:25
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
float CalculateEuclideanDistance(const Vector2D &vector) const
Calculates the Euclidean distance between this vector and another Vector2D.
Definition Vector2D.cpp:150
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30