-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.cpp
More file actions
55 lines (42 loc) · 1.13 KB
/
particle.cpp
File metadata and controls
55 lines (42 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "raylib.h"
#include "particle.h"
#include <math.h>
#include <sstream>
using namespace std;
float Distance(float x, float y) {
return max((float)sqrt(pow(x, 2)+pow(y, 2)), 50.0f);
}
Particle::Particle(int x, int y) {
this->pos = Vector2{(float)x, (float)y};
this->vel = Vector2{0, 0};
this->acc = Vector2{0, 0};
this->m = 2;
this->original = pos;
}
Particle::Particle() {
this->pos = Vector2{0, 0};
this->vel = Vector2{0, 0};
this->acc = Vector2{0, 0};
this->m = 1;
}
void Particle::ApplyGravityForce(Vector2 pos, int mass, float sign) {
float xdist = this->pos.x-pos.x;
float ydist = this->pos.y-pos.y;
float dist = Distance(xdist, ydist)/5;
float f = min(-((mass*m)/pow(dist, 2)), 2.0)*sign;
Vector2 force = {f*(xdist/dist), f*(ydist/dist)};
acc.x += force.x/m;
acc.y += force.y/m;
}
void Particle::Update() {
vel.x += acc.x;
vel.y += acc.y;
vel.x *= 0.98f;
vel.y *= 0.98f;
pos.x += vel.x*GetFrameTime();
pos.y += vel.y*GetFrameTime();
acc = Vector2{0, 0};
}
void Particle::Draw() {
DrawPixelV(pos, {255, 255, 255, 225});
}