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 - Kiblinix

Pages: [1]
1
Graphics / Moving one shape over another using the mouse
« on: March 17, 2012, 12:45:12 am »
For some reason adding that second part in the else condition causes me to not be able to drag the circles (Reason which is unknown to me :P)

I can click on a part of the circle and it moves slightly, but no dragging.

2
Graphics / Moving one shape over another using the mouse
« on: March 17, 2012, 12:17:06 am »
I have two circle shapes on the screen, and when the left mouse button is held down, and the mouse cursor is over one of the circles, it should "pick it up" and the circle follows the mouse cursor until the left mouse button is released.
There is an issue when I pick up one of the circles, and move it over the other, because then this causes the second shape to be picked up also and I cannot separate them since they are on top of each other.

Here is my input code for the circles:
 
Code: [Select]
if ( (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
    && (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}


This picks up the circle fine but causes the issue mentioned above :(

I did try to add a bool so it would only pick it up if it didn't have one picked up already, and here is the code for that:

Code: [Select]
bool nodeGrabbed = false;
if ( (nodeGrabbed == false) && (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
nodeGrabbed = true;
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}
else
{
nodeGrabbed = false;
}


But this still did not work :(

Maybe I should mention that each shape is in a class and the two different shapes I have created are in a vector, and I go through this vector to check for user input, so maybe this has something to do with it as it just gets the input for both at the same time?

Any help would be appreciated

3
Graphics / Ball movement and bouncing in the Pong example
« on: August 09, 2011, 03:59:16 pm »
I have another quick question, so I just thought I'd post it here.

When bouncing the ball off of the paddle, this does not work:

Code: [Select]
ballAngleRad = -ballAngleRad;

The problem with this is that the ball's angle does not seem to change, and just passes right through.

I looked at the pong example which is so kindly provided, and tried this:

Code: [Select]
ballAngleRad = PI - ballAngleRad;

It fixes the problem, why is this?

4
Graphics / Ball movement and bouncing in the Pong example
« on: August 09, 2011, 12:30:05 am »
They should be, but honestly I don't do most of that stuff on the pc I'm on at the moment. I'll take a look.

Edit : Derp. I ran it in release mode, seems to have fixed it. :)

5
Graphics / Ball movement and bouncing in the Pong example
« on: August 08, 2011, 01:26:54 pm »
Thanks a ton for that guys, was a big help :) I now have it how I want, but there's a new problem now that I've started to do it this way.

The ball's movement across the screen is no longer smooth and it jitters and jerks instead, which is not easy on the eyes :(

Edit: I could try to record it moving I suppose, if that helps you understand what I mean somewhat.

Here's my full code, in hopes of an easy solution:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <cmath>

int main()
{
// Create the App of the application
   sf::RenderWindow App(sf::VideoMode(640, 480, 32), "SFML Pong");
App.SetFramerateLimit(60);

sf::Shape Ball = sf::Shape::Circle(0, 0, 15, sf::Color::Black);
Ball.SetPosition(320, 240);

// Define PI
float PI = 3.14159265f;

// Ball properties
float ballSpeed = 0.2f;
float ballAngleRad = 90 * (PI / 180);

   while (App.IsOpened())
   {
      // Handle events
      sf::Event Event;
      while (App.PollEvent(Event))
      {
         // App closed or escape key pressed : exit
         if (Event.Type == sf::Event::Closed)            
            App.Close();          
      }

if (Ball.GetPosition().y + 15 > App.GetHeight())
{
ballAngleRad = -ballAngleRad;
}

if (Ball.GetPosition().y - 15 < 0)
{
ballAngleRad = -ballAngleRad;
}

Ball.Move(App.GetFrameTime() * ballSpeed * std::cos(ballAngleRad), App.GetFrameTime() * ballSpeed * std::sin(ballAngleRad));

      // Clear the App
App.Clear(sf::Color::White);

// Draw the ball
App.Draw(Ball);
       
      // Display things on screen
      App.Display();
   }

   return 0;
}

6
Graphics / Ball movement and bouncing in the Pong example
« on: August 07, 2011, 11:35:59 pm »
I am trying to recreate pong to help me along with my learning on C++ and SFML, but I've ran into a few problems when trying to make the ball move and bounce.

I managed to get the ball to move on its own, and to bounce off the walls and the paddles easily, but the problem was that it didn't seem "realistic". The ball was always bouncing at the same angle, as what I did was this:

Code: [Select]
float velX = 0.15f;
float velY = 0.35f;

// Move the ball in the game loop
Ball.Move(velX * App.GetFrameTime(), velY * App.GetFrameTime());

// When the ball hits the top or bottom of the screen
velY = -velY;

// When the ball hits a paddle
velX = -velX;


Doing it like this meant that the user has no control over where the ball goes, as it would just bounce around at the same angle and be pretty boring.

I reasoned that to do this I'd need to use trigonometry, so I attempted that.

Correct me if I'm wrong, but this is what I did to try and work it out:

where x is the angle I want the ball to move, and hyp is the ball's speed?

So to move the ball I would do:
Code: [Select]
Ball.Move(App.GetFrameTime() * ballSpeed * cos(ballAngle), App.GetFrameTime() * ballSpeed * sin(ballAngle));

Is this correct? And excuse the cos and sin bits there, I don't know how to do those in C++ yet.

7
Graphics / Classes and using static
« on: July 18, 2011, 09:05:16 pm »
Thanks Hiura, that was a massive help :)

Quick question, would it be possible to use this static image loading function for an Entity class, and then derive a player, enemy, and missile class from that? I was thinking that would make the code easier to read and simplify it a bit, but wouldn't it just cause all the classes to share the same image?

8
Graphics / Classes and using static
« on: July 17, 2011, 11:01:28 pm »
For the past two days I have been trying to create a bullet class which can then be used to let the player fire bullets.

I have been struggling to get the bullet class to use the same image that only has to load once instead of loading it every time a bullet is created, but this leads to some problems.

I had a look in the 1.6 sprites tutorial: http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php , specifically the Images and Sprite management section, more specifically, this piece of code:

Code: [Select]
class Missile
{
public :

    static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }

    Missile()
    {
        Sprite.SetImage(Image); // every sprite uses the same unique image
    }

private :

    static sf::Image Image; // shared by every instance

    sf::Sprite Sprite; // one per instance
};


I do not understand this part:
Code: [Select]
static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }


I understand the rest of the class, but I am not sure what I am meant to do with this function, do I use it after I have created the bullet?

Code: [Select]
Missile missile1;
missile1.Init("Missile.png");


Like so? Because if I just copy the Missile class exactly into my code (I wouldn't normally do this but I've been working on it for a while now), and then try to just create a Missile, I get this error:

error LNK2001: unresolved external symbol "private: static class sf::Image Missile::Image" (?Image@Missile@@0V0sf@@A)

Here is my program that produces the problem:

Code: [Select]
#include <SFML/Graphics.hpp>

class Missile
{
public :
    static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }

    Missile()
    {
        Sprite.SetImage(Image);
    }

private :
    static sf::Image Image;
    sf::Sprite Sprite;
};

int main()
{
sf::RenderWindow App(sf::VideoMode(640, 480), "Space Shooter");
App.SetFramerateLimit(60);

Missile missile1;

while (App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}

App.Clear(sf::Color::White);
App.Display();
}

return 0;
}


Any help would be greatly appreciated, and as a side note, I'm using SFML 2.0.

9
SFML projects / Legend of Dungeon
« on: July 13, 2011, 12:33:00 am »
Really nice game :D I liked the level select (totally stealing that later)

I was terrible at it but got better after a couple of goes, was still fun. :P

10
SFML projects / Fruit Chase
« on: July 13, 2011, 12:03:55 am »
Quote
Sorry, I didn't express well. When you resize the window, the clickable areas doesn't match to the menu options anymore.


No problem :) I never really took into account people might resize the window haha. It's fixed now.

Quote
sf::Window::ConvertCoords

Thanks a ton for that, saved me having to look for ages to find something like that myself :P Downside was I had to create a new project where a circle followed the mouse to understand how it worked :)

11
SFML projects / Fruit Chase
« on: July 12, 2011, 03:29:26 pm »
Quote
resize window (e.g. maximize) and menus won't work properly


What's wrong with the menus? I checked through it a few times and didn't find anything.

For the resizing, do you mean that things get distorted when you change the window size? I kinda didn't know how to fix that :P

12
SFML projects / Fruit Chase
« on: July 10, 2011, 11:49:17 pm »
In some spare time I decided I wanted to create a simple game with C++ and (of course) SFML.
Hence, Fruit Chase was born!

All you have to do is move your character around the screen, and when he gets to some fruit, he'll eat it! When he eats fruit, he speeds up. If the character touches the sides of the screen, game over, so the game gets progressively more difficult.

Download here: https://legacy.sfmluploads.org/index.php?page=view_file&file_id=41

Source code is in there too, so feel free to take a look at it, but don't expect any amazing code :P

This is the first game I have made since I decided to start using and practising SFML, so any criticism is greatly appreciated :)

How high can you score?

13
Graphics / Help with game and sprite movement
« on: May 18, 2011, 07:59:25 pm »
I'd like it to move freely from the center of the tile to the center of the next tile, and thanks for the info, I'll look into that.

14
Graphics / Help with game and sprite movement
« on: May 18, 2011, 07:24:03 pm »
Thanks for your reply, it was very helpful :) I now have movement how I want it apart from the character not staying in the grid.

Does anybody have any tips on this?
I imagine it would be done using the sprite position and could I then compare it with each tile position? Or only be able to move the character when he is aligned with the grid?

15
Graphics / Help with game and sprite movement
« on: May 17, 2011, 09:31:45 pm »
Hey, I'm currently in the progress of creating my first game using SFML.
My game is going to be like Pickin' Sticks, but have the movement of something like Pokemon / Snake.
Basically, the character will have to collect an object, like in Pickin' Sticks, and will move by when I press a key, he sets off in that direction (No holding down the button to get him to move and then releasing to stop). I also want him to be walking on the tiles on the floor only, not in between.

Here's what the game looks like so far (Ugly but what I need to visualise it at the moment):



Here is my character movement code:
Code: [Select]
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
float charSpeed = 50.f;

if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::W))
Character.Move(0, -charSpeed * ElapsedTime);  
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
Character.Move(-charSpeed * ElapsedTime, 0);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::S))
Character.Move(0, charSpeed * ElapsedTime);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::D))
Character.Move(charSpeed * ElapsedTime, 0);


My problem at the moment is that the character sprite moves fine, until I tap a key, for example 'g', and then the sprite stops. How do I prevent this?

My next problem is, how do I keep the sprite between the tiles, so there is no overlapping on the black lines? I've thought about this for a while but haven't thought of a way to implement it.

Lastly, my code for creating all those tiles etc is quite messy:
Code: [Select]
const float imgWidth = Tile1.GetWidth();      // Tile1 is just the image version.
const float imgHeight = Tile1.GetHeight();
const float horizontalTiles = App.GetWidth() / imgWidth;
const float verticalTiles = App.GetHeight() / imgHeight;

std::vector<sf::Sprite> Tiles;
for (int i = 0; i < horizontalTiles; i++)
{
for (int j = 0; j < verticalTiles; j++)
{
sf::Sprite Tile;
Tile.SetImage(Tile1);
Tile.SetPosition(i * imgWidth, j * imgHeight);

Tiles.push_back(Tile);
}
}

Code: [Select]
for (unsigned int i = 0; i < Tiles.size(); i++)
App.Draw(Tiles[i]);


Is there a "cleaner" way of doing this? What if I also wanted to change a few of the middle tiles colour using .SetColor? I imagine to do this I would use for loops again, but I can't really think of how to do it without changing nearly all of them.

Thanks.  :)

Pages: [1]
anything