Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Spidyy

Pages: 1 ... 6 7 [8]
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 :

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.

107
System / Help with clock()
« on: September 13, 2009, 11:54:20 am »
In your case, the Clock.Reset isn't necessary. It even avoid the Time to reach the desired value.

Each frame duration is about 0,0001 sec, and when you do a Clock.Reset, it reset the countdown to 0.

So it do that :


Code: [Select]
while (App.GetEvent(Event))
{
    int Time = Clock.GetElapsedTime(); // Retrieve the current time in a variable,
    Clock.Reset(); // Reset the time to 0
        if (Time==3) // Here, time have the value of the duration of your frame, so it never reach 3 except if you pause the programm for 3 seconde.
            App.Close();

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


The solution : just avoid the Clock.Reset(). Also, use a > condition instead of ==. The time very rarely have an integer value (3.00000...), it's most often close to the value (3.00000154...) but rarely get at an integer value. Having a "superior at" condition guarantie you to detect the right time. =p

You should do :
Code: [Select]

while (App.GetEvent(Event))
{
    if (Clock.GetElapsedTime() > 3)
        App.Close();

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


And if you want to reapeat an action, here you can use the Clock.Reset() function.

Code: [Select]

while (App.GetEvent(Event))
{
    if (Clock.GetElapsedTime() > 3)
    {
        //Doing something to repeat
        ...
        Clock.Reset();
    }

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


And as Hiura said, there is a precise order to do things. It isn't mandatory, but rather logical. =p

First, proceed with the Event. If someone ask to close the window, it is useless to execute the rest of the instruction following.

Then do all the calculation operation modifying your present objects, taking the changes of the Event in account.

Last, display the result. =)

It is what Hiura was explaining with his pseudo-code.

108
SFML projects / Short n Sweet 2D Space Shooter Action
« on: September 13, 2009, 02:00:43 am »
Thanks! =D

109
SFML projects / Short n Sweet 2D Space Shooter Action
« on: September 13, 2009, 01:47:05 am »
WASD = ZQSD for AZERTY Keyboards. Switch the W with Z and the A with Q. =p

110
SFML projects / Short n Sweet 2D Space Shooter Action
« on: September 13, 2009, 01:40:29 am »
Nice little game but it's hard to play on AZERTY keyboard. =p (Yeah i'm french!), don't tell me about the Shift + Alt, don't work.

If I was able to do so nice PostFX effects... =) (love the deflagration wave when the ship explode!)

How many are you working in the team? For a 13 day work, it's pretty well done. :p

111
Graphics / Bullets in SFML.
« on: September 13, 2009, 01:11:27 am »
Does the boost/smart ptr allow to use the inheritance advantages? (Having a pointer on base class and instantiate her inheriting class)

Pages: 1 ... 6 7 [8]
anything