106
Graphics / Bullets in SFML.
« on: September 13, 2009, 01:52:49 pm »
Your speed calculation is weird and wrong. :/ (If I correctly readed your code...)
For each frame, you take the position of your sprite, and multiply by 2 this position. I say at each frame, so between 300 to 5000 time by second. Far too fast!
Their is a difference from setting the speed and moving your sprite. The movement of your sprite is determined by its speed.
Your object should be something like that :
Change your SetSpeed to :
And in the while, add a function like this :
And in your main :
Do something like that, it would work better.
In other words, You should have a function "Calculate" or any name you want you call at each frames to actualize the properties of your sprite. The SetSpeed and SetDirection should change the properties of your sprite only when needed.
EDIT : I readed again your code, you already do that with the movement of the player. =p The movements of the bullet are no more different except it live by itself and not by listening to the inputs.
For each frame, you take the position of your sprite, and multiply by 2 this position. I say at each frame, so between 300 to 5000 time by second. Far too fast!
Their is a difference from setting the speed and moving your sprite. The movement of your sprite is determined by its speed.
Your object should be something like that :
Code: [Select]
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image BulletImage;
static sf::Sprite Bullet;
bool BulletState;
float BulletSpeed; // ADDED The speed of your bullet, it will be counted in Pixel Per Second
public:
void SetDirection(sf::Sprite);
void SetSpeed(float);
void Destroy(void);
bool IsDestroyed(const Projectile& b);
};
#endif
Change your SetSpeed to :
Code: [Select]
void Projectile::SetSpeed(float NewSpeed)
{
BulletSpeed = NewSpeed;
}
And in the while, add a function like this :
Code: [Select]
void Projectile::Calculate(sf::RenderWindow &app)
{
sf::Vector2f offset; // The offset we will add to the current position to move the sprite for ONE frame;
// Here we calculate the offset for each axis. We use the cos() function from <math.h> with the direction (wich is an Angle in Degree, we convert it in Radiant by multiplying it by PI/180)
// At this stade, we got a number between 0 and 1. We multiply it by the speed to have a number between 0 and BulletSpeed, and last, we multiply by the duration of the frame to have a correct offset for each frames.
offset.x = static_cast<float>(cos(PI * GetRotation() / 180) * BulletSpeed * app.GetFrameTime();
offset.y = static_cast<float>(sin(PI * GetRotation() / 180) * BulletSpeed * app.GetFrameTime();
//And we move our bullet
Move(offset);
}
And in your main :
Code: [Select]
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");
...
while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
...
}
Bullet.SetDirection(Player);
Bullet.SetSpeed(2);
Bullet.Calculate(Window); // ADDED
Window.Clear();
...
}
Do something like that, it would work better.
In other words, You should have a function "Calculate" or any name you want you call at each frames to actualize the properties of your sprite. The SetSpeed and SetDirection should change the properties of your sprite only when needed.
EDIT : I readed again your code, you already do that with the movement of the player. =p The movements of the bullet are no more different except it live by itself and not by listening to the inputs.