Particle System Test
New Rocket Sprite Rotation Limit Entity & Physics Entity Classes Random Number Generator Particle System(WIP)
This commit is contained in:
parent
c1ac111941
commit
ca1963612e
|
@ -9,6 +9,45 @@
|
||||||
"sstream": "cpp",
|
"sstream": "cpp",
|
||||||
"*.m": "cpp",
|
"*.m": "cpp",
|
||||||
"cmath": "cpp",
|
"cmath": "cpp",
|
||||||
"type_traits": "cpp"
|
"type_traits": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"locale": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"xfacet": "cpp",
|
||||||
|
"xlocale": "cpp",
|
||||||
|
"xlocbuf": "cpp",
|
||||||
|
"xlocinfo": "cpp",
|
||||||
|
"xlocmes": "cpp",
|
||||||
|
"xlocmon": "cpp",
|
||||||
|
"xlocnum": "cpp",
|
||||||
|
"xloctime": "cpp",
|
||||||
|
"xmemory": "cpp",
|
||||||
|
"xmemory0": "cpp",
|
||||||
|
"xstddef": "cpp",
|
||||||
|
"xtr1common": "cpp",
|
||||||
|
"xutility": "cpp",
|
||||||
|
"iostream": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
#include "SynGame.hpp"
|
||||||
|
|
||||||
|
class Entity
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
sf::Vector2f position;
|
||||||
|
public:
|
||||||
|
Entity();
|
||||||
|
void SetPosition(float, float, bool = true);
|
||||||
|
void SetPosition(sf::Vector2f, bool = true);
|
||||||
|
sf::Vector2f GetPosition(bool = true);
|
||||||
|
};
|
||||||
|
|
||||||
|
Entity::Entity()
|
||||||
|
{
|
||||||
|
position = sf::Vector2f(0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::SetPosition(float x, float y, bool inverse)
|
||||||
|
{
|
||||||
|
position.x = x;
|
||||||
|
position.y = !inverse ? -y : y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::SetPosition(sf::Vector2f position, bool inverse)
|
||||||
|
{
|
||||||
|
this -> position = position;
|
||||||
|
this -> position.y = !inverse ? -this -> position.y : this -> position.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2f Entity::GetPosition(bool inverse)
|
||||||
|
{
|
||||||
|
sf::Vector2f result = position;
|
||||||
|
if(inverse) result.y *= -1;
|
||||||
|
return result;
|
||||||
|
}
|
|
@ -0,0 +1,154 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "SynGame.hpp"
|
||||||
|
|
||||||
|
#pragma region Particle
|
||||||
|
class Particle : public PhysicEntity
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
sf::Vertex vertex;
|
||||||
|
float lifeTime;
|
||||||
|
float remainingLifeTime;
|
||||||
|
void ChangeColorOverLifeTime();
|
||||||
|
public:
|
||||||
|
Particle();
|
||||||
|
Particle(sf::Color);
|
||||||
|
void Update(float);
|
||||||
|
void ResetRemainingLifeTime();
|
||||||
|
void SetLifeTime(float);
|
||||||
|
bool IsLifeTimeOver();
|
||||||
|
sf::Vertex &GetVertex();
|
||||||
|
};
|
||||||
|
|
||||||
|
Particle::Particle() : PhysicEntity()
|
||||||
|
{
|
||||||
|
vertex.color = sf::Color::White;
|
||||||
|
lifeTime = remainingLifeTime = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Particle::Particle(sf::Color color) : PhysicEntity()
|
||||||
|
{
|
||||||
|
vertex.color = color;
|
||||||
|
lifeTime = remainingLifeTime = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::Update(float deltaTime)
|
||||||
|
{
|
||||||
|
PhysicEntity::Update(deltaTime);
|
||||||
|
|
||||||
|
if(!isActive)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::cout << position.x << ", " << position.y << "\n";
|
||||||
|
|
||||||
|
remainingLifeTime -= deltaTime;
|
||||||
|
ChangeColorOverLifeTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::ChangeColorOverLifeTime()
|
||||||
|
{
|
||||||
|
int value = 255 * (remainingLifeTime / lifeTime);
|
||||||
|
vertex.color.a = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::ResetRemainingLifeTime()
|
||||||
|
{
|
||||||
|
remainingLifeTime = lifeTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::SetLifeTime(float time)
|
||||||
|
{
|
||||||
|
remainingLifeTime = lifeTime = time;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Particle::IsLifeTimeOver()
|
||||||
|
{
|
||||||
|
return remainingLifeTime <= 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vertex &Particle::GetVertex()
|
||||||
|
{
|
||||||
|
vertex.position = position;
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
#pragma endregion
|
||||||
|
#pragma region Particle System
|
||||||
|
class ParticleSystem : public Entity
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::vector<Particle> particles;
|
||||||
|
sf::Color color;
|
||||||
|
int particleSize;
|
||||||
|
float particleLifeTime;
|
||||||
|
public:
|
||||||
|
ParticleSystem();
|
||||||
|
void SetSize(int);
|
||||||
|
void SetParticleLifeTime(float);
|
||||||
|
void Update(float);
|
||||||
|
Particle &operator[](int);
|
||||||
|
int Size();
|
||||||
|
};
|
||||||
|
|
||||||
|
ParticleSystem::ParticleSystem()
|
||||||
|
{
|
||||||
|
particles.clear();
|
||||||
|
color = sf::Color::White;
|
||||||
|
particleSize = 0;
|
||||||
|
particleLifeTime = 0.1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::Update(float deltaTime)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
Particle *particle;
|
||||||
|
|
||||||
|
for (i = 0; i < particleSize; i++)
|
||||||
|
{
|
||||||
|
particle = &particles[i];
|
||||||
|
if(particle -> IsLifeTimeOver())
|
||||||
|
{
|
||||||
|
particle -> ResetRemainingLifeTime();
|
||||||
|
particle -> SetPosition(position.x, position.y, false);
|
||||||
|
particle -> SetVelocity(sf::Vector2f(Random(-10.0, 10.0), Random(-10.0, 10.0)));
|
||||||
|
}
|
||||||
|
particle -> Update(deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::SetSize(int size)
|
||||||
|
{
|
||||||
|
while(particleSize != size)
|
||||||
|
if(particleSize > size)
|
||||||
|
{
|
||||||
|
delete &particles[--particleSize];
|
||||||
|
particles.pop_back();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Particle newParticle = Particle(color);
|
||||||
|
newParticle.SetLifeTime(particleLifeTime);
|
||||||
|
particles.push_back(newParticle);
|
||||||
|
particleSize++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::SetParticleLifeTime(float lifeTime)
|
||||||
|
{
|
||||||
|
if(lifeTime < 0.1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
particleLifeTime = lifeTime;
|
||||||
|
for (i = 0; i < particleSize; i++)
|
||||||
|
particles[i].SetLifeTime(lifeTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
Particle &ParticleSystem::operator[](int index)
|
||||||
|
{
|
||||||
|
return particles[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
int ParticleSystem::Size()
|
||||||
|
{
|
||||||
|
return particles.size();
|
||||||
|
}
|
||||||
|
#pragma endregion
|
|
@ -0,0 +1,64 @@
|
||||||
|
#include "SynGame.hpp"
|
||||||
|
|
||||||
|
class PhysicEntity : public Entity
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void ApplyGravity(float);
|
||||||
|
void ApplyVelocity(float);
|
||||||
|
protected:
|
||||||
|
sf::Vector2f velocity;
|
||||||
|
float gravity;
|
||||||
|
bool isActive;
|
||||||
|
public:
|
||||||
|
PhysicEntity();
|
||||||
|
void Update(float);
|
||||||
|
void SetVelocity(sf::Vector2f);
|
||||||
|
void SetGravity(float = 10);
|
||||||
|
void SetActive(bool = true);
|
||||||
|
float GetSpeed();
|
||||||
|
};
|
||||||
|
|
||||||
|
PhysicEntity::PhysicEntity() : Entity()
|
||||||
|
{
|
||||||
|
velocity = sf::Vector2f(0.0, 0.0);
|
||||||
|
gravity = 0.0;
|
||||||
|
isActive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::Update(float deltaTime)
|
||||||
|
{
|
||||||
|
if(!isActive) return;
|
||||||
|
|
||||||
|
ApplyGravity(deltaTime);
|
||||||
|
ApplyVelocity(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::ApplyGravity(float deltaTime)
|
||||||
|
{
|
||||||
|
velocity.y -= gravity * deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::ApplyVelocity(float deltaTime)
|
||||||
|
{
|
||||||
|
position += velocity * deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::SetVelocity(sf::Vector2f velocity)
|
||||||
|
{
|
||||||
|
this -> velocity = velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::SetGravity(float gravity)
|
||||||
|
{
|
||||||
|
this -> gravity = gravity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicEntity::SetActive(bool active)
|
||||||
|
{
|
||||||
|
isActive = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
float PhysicEntity::GetSpeed()
|
||||||
|
{
|
||||||
|
return sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include <time.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T Random(T min, T max)
|
||||||
|
{
|
||||||
|
T result;
|
||||||
|
long int value;
|
||||||
|
static unsigned long int counter = time(0);
|
||||||
|
srand(time(0) * counter++);
|
||||||
|
value = ((rand() * counter) % (int)((max - min) * 100000000.0));
|
||||||
|
result = value / 100000000.0 + min;
|
||||||
|
return result;
|
||||||
|
}
|
|
@ -1,55 +1,38 @@
|
||||||
#include <SFML/Graphics.hpp>
|
// #include <SFML/Graphics.hpp>
|
||||||
#include <vector>
|
// #include <cmath>
|
||||||
#include <cmath>
|
#include "SynGame.hpp"
|
||||||
|
// #include "Particles.hpp"
|
||||||
|
// #include "PhysicEntity.hpp"
|
||||||
|
|
||||||
#define DegToRad 0.0174533
|
#define DegToRad 0.0174533
|
||||||
|
#define RotationLimit 45.0
|
||||||
|
|
||||||
class Rocket
|
class Rocket : public PhysicEntity
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
float thrustForce;
|
float thrustForce;
|
||||||
float rotationForce;
|
float rotationForce;
|
||||||
float rotation;
|
float rotation;
|
||||||
float gravity;
|
|
||||||
sf::Texture texture;
|
sf::Texture texture;
|
||||||
sf::Sprite sprite;
|
sf::Sprite sprite;
|
||||||
sf::Vector2f position;
|
|
||||||
sf::Vector2f velocity;
|
|
||||||
bool isActive;
|
|
||||||
void ApplyGravity(float);
|
|
||||||
void ApplyVelocity(float);
|
|
||||||
public:
|
public:
|
||||||
|
ParticleSystem particleSystem;
|
||||||
Rocket();
|
Rocket();
|
||||||
void Update(float);
|
|
||||||
void Rotate(float, bool = true);
|
void Rotate(float, bool = true);
|
||||||
void Thrust(float);
|
void Thrust(float);
|
||||||
|
void Update(float);
|
||||||
void SetThrustForce(float);
|
void SetThrustForce(float);
|
||||||
void SetRotationForce(float);
|
void SetRotationForce(float);
|
||||||
void SetGravity(float);
|
|
||||||
void SetPosition(float, float, bool = true);
|
|
||||||
void SetTexture(sf::Texture, float = 1.0);
|
void SetTexture(sf::Texture, float = 1.0);
|
||||||
void SetActive(bool = true);
|
|
||||||
float GetSpeed();
|
|
||||||
sf::Sprite GetSprite();
|
sf::Sprite GetSprite();
|
||||||
sf::Vector2f GetPosition(bool = true);
|
|
||||||
sf::Vector2f GetLandingPoint(bool = true);
|
sf::Vector2f GetLandingPoint(bool = true);
|
||||||
|
void SetPosition(float, float, bool = true);
|
||||||
};
|
};
|
||||||
|
|
||||||
Rocket::Rocket()
|
Rocket::Rocket() : PhysicEntity()
|
||||||
{
|
{
|
||||||
velocity.x = velocity.y = 0.0f;
|
|
||||||
position.x = position.y = 0.0f;
|
|
||||||
thrustForce = rotationForce = rotation = 0.0f;
|
thrustForce = rotationForce = rotation = 0.0f;
|
||||||
gravity = 10;
|
SetGravity();
|
||||||
isActive = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Rocket::Update(float deltaTime)
|
|
||||||
{
|
|
||||||
if(!isActive) return;
|
|
||||||
|
|
||||||
ApplyGravity(deltaTime);
|
|
||||||
ApplyVelocity(deltaTime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rocket::Rotate(float deltaTime, bool isRight)
|
void Rocket::Rotate(float deltaTime, bool isRight)
|
||||||
|
@ -57,6 +40,12 @@ void Rocket::Rotate(float deltaTime, bool isRight)
|
||||||
if(!isActive) return;
|
if(!isActive) return;
|
||||||
|
|
||||||
rotation += (isRight ? rotationForce : -rotationForce) * deltaTime;
|
rotation += (isRight ? rotationForce : -rotationForce) * deltaTime;
|
||||||
|
|
||||||
|
if(rotation > RotationLimit)
|
||||||
|
rotation = RotationLimit;
|
||||||
|
else if(rotation < -RotationLimit)
|
||||||
|
rotation = -RotationLimit;
|
||||||
|
|
||||||
sprite.setRotation(rotation);
|
sprite.setRotation(rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,31 +56,18 @@ void Rocket::Thrust(float deltaTime)
|
||||||
float force = thrustForce * deltaTime;
|
float force = thrustForce * deltaTime;
|
||||||
velocity.x += sin(rotation * DegToRad) * force;
|
velocity.x += sin(rotation * DegToRad) * force;
|
||||||
velocity.y += cos(rotation * DegToRad) * force;
|
velocity.y += cos(rotation * DegToRad) * force;
|
||||||
|
|
||||||
|
particleSystem.SetPosition(position);
|
||||||
|
particleSystem.Update(deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rocket::ApplyGravity(float deltaTime)
|
void Rocket::Update(float deltaTime)
|
||||||
{
|
{
|
||||||
velocity.y -= gravity * deltaTime;
|
PhysicEntity::Update(deltaTime);
|
||||||
}
|
particleSystem.Update(deltaTime);
|
||||||
|
|
||||||
void Rocket::ApplyVelocity(float deltaTime)
|
|
||||||
{
|
|
||||||
position += velocity * deltaTime;
|
|
||||||
sprite.setPosition(GetPosition());
|
sprite.setPosition(GetPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rocket::SetPosition(float x, float y, bool inverse)
|
|
||||||
{
|
|
||||||
position.x = x;
|
|
||||||
position.y = !inverse ? -y : y;
|
|
||||||
sprite.setPosition(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Rocket::SetGravity(float gravity)
|
|
||||||
{
|
|
||||||
this -> gravity = gravity;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Rocket::SetThrustForce(float thrustForce)
|
void Rocket::SetThrustForce(float thrustForce)
|
||||||
{
|
{
|
||||||
this -> thrustForce = thrustForce;
|
this -> thrustForce = thrustForce;
|
||||||
|
@ -110,14 +86,10 @@ void Rocket::SetTexture(sf::Texture texture, float size)
|
||||||
sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
|
sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rocket::SetActive(bool active)
|
void Rocket::SetPosition(float x, float y, bool inverse)
|
||||||
{
|
{
|
||||||
isActive = active;
|
Entity::SetPosition(x, y, inverse);
|
||||||
}
|
sprite.setPosition(position);
|
||||||
|
|
||||||
float Rocket::GetSpeed()
|
|
||||||
{
|
|
||||||
return sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Sprite Rocket::GetSprite()
|
sf::Sprite Rocket::GetSprite()
|
||||||
|
@ -125,13 +97,6 @@ sf::Sprite Rocket::GetSprite()
|
||||||
return sprite;
|
return sprite;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f Rocket::GetPosition(bool inverse)
|
|
||||||
{
|
|
||||||
sf::Vector2f result = position;
|
|
||||||
if(inverse) result.y *= -1;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Vector2f Rocket::GetLandingPoint(bool inverse)
|
sf::Vector2f Rocket::GetLandingPoint(bool inverse)
|
||||||
{
|
{
|
||||||
sf::Vector2f result = position;
|
sf::Vector2f result = position;
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 429 B After Width: | Height: | Size: 441 B |
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef SynClasses
|
||||||
|
#define SynClasses
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
|
#include "Randomizer.hpp"
|
||||||
|
#include "Entity.hpp"
|
||||||
|
#include "PhysicEntity.hpp"
|
||||||
|
#include "Particles.hpp"
|
||||||
|
#include "Rocket.hpp"
|
||||||
|
#endif
|
|
@ -1,7 +1,7 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "Rocket.hpp"
|
#include "SynGame.hpp"
|
||||||
|
|
||||||
#define WindowSize sf::VideoMode(960, 540)
|
#define WindowSize sf::VideoMode(960, 540)
|
||||||
#define WindowStyle sf::Style::Titlebar | sf::Style::Close
|
#define WindowStyle sf::Style::Titlebar | sf::Style::Close
|
||||||
|
@ -49,6 +49,8 @@ int main()
|
||||||
player.SetGravity(10 * SizeMultiplier);
|
player.SetGravity(10 * SizeMultiplier);
|
||||||
player.SetPosition(480, -50);
|
player.SetPosition(480, -50);
|
||||||
player.SetActive(true);
|
player.SetActive(true);
|
||||||
|
player.particleSystem.SetParticleLifeTime(1.5);
|
||||||
|
player.particleSystem.SetSize(25);
|
||||||
|
|
||||||
text.setFont(font);
|
text.setFont(font);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
@ -101,6 +103,8 @@ int main()
|
||||||
window.clear();
|
window.clear();
|
||||||
window.draw(text);
|
window.draw(text);
|
||||||
window.draw(&terrain[0], 320, sf::PrimitiveType::LinesStrip);
|
window.draw(&terrain[0], 320, sf::PrimitiveType::LinesStrip);
|
||||||
|
for (int i = 0; i < player.particleSystem.Size(); i++)
|
||||||
|
window.draw(&player.particleSystem[i].GetVertex(), 1, sf::PrimitiveType::Points);
|
||||||
window.draw(player.GetSprite());
|
window.draw(player.GetSprite());
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue