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

Pages: 1 2 [3] 4
31
Graphics / Sprite Movement on Single MouseClick
« on: January 17, 2012, 10:48:53 pm »
I got it to work now thanks to everyone. Just one little thing lol. The player continuously follows the cursor, even though everything is now within the if statement, it doesnt wait for an action from the mouse.

Code: [Select]
        if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left));
            {
            Clock1.Reset();
            float NewX = App.GetInput().GetMouseX();
            float NewY = App.GetInput().GetMouseY();
            sf::Vector2f Origin = PlayerObj.GetPosition();
            sf::Vector2f Destination = sf::Vector2f (NewX, NewY);
            sf::Vector2f Direction = Destination - Origin;
            float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );
            Direction /= Distance;
            float Speed = .5f;
            PlayerObj.SetPosition(Origin + Direction * Speed);
            }

32
Graphics / Sprite Movement on Single MouseClick
« on: January 17, 2012, 10:02:08 pm »
How would I update the Destination and Distance vectors then?  Because doing:
Code: [Select]
Destination = (NewX, NewY);
Doesnt work (error).  And I can not find any other commands in the documentation that would allow for the vector to be updated within the if statement.

33
Graphics / Sprite Movement on Single MouseClick
« on: January 17, 2012, 07:11:23 am »
I did ask you suggested.  The Destination vector in the beginning to the Origin vector outside of the loop.  The NewX and NewY are now inside the loop, and the Destination vector is now  updated inside the if statement. Everything else is updated as well.  

Code: [Select]
   sf::Clock::Clock(Clock1);
    sf::Vector2f Origin = PlayerObj.GetPosition();
    sf::Vector2f Destination = Origin;
    sf::Vector2f Direction = Destination - Origin;
    float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );

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

         if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)); {
            Clock1.Reset();
            float NewX = App.GetInput().GetMouseX();
            float NewY = App.GetInput().GetMouseY();
            Origin = PlayerObj.GetPosition();
            sf::Vector2f Destination(NewX, NewY);
            Direction = Destination - Origin;
            Direction /= Distance;}
            float Speed = 500.f;
            float DistanceMoved = Speed * Clock1.GetElapsedTime() / 1000.f;
            if (DistanceMoved > Distance) {
            DistanceMoved = Distance;
            }
         PlayerObj.SetPosition(Origin + Direction * DistanceMoved);


Bugs: One, the player does not show up on the screen on startup. I think it has something to do with this code:
Code: [Select]
   sf::Vector2f Destination = Origin;
    sf::Vector2f Direction = Destination - Origin;


Second, still nothing happens when the mouse is clicked. Perhaps related to the first bug?

34
Graphics / Sprite Movement on Single MouseClick
« on: January 17, 2012, 02:19:45 am »
Okay, but there is one problem. Dont I need to declare the NewX, NewY variables twice? Once for a defining them for the definitions of the vectors outside of the loop, and a second time within the if statement that collects data from the mouse position:

Code: [Select]
   float NewX = 0;
    float NewY = 0;
    sf::Clock::Clock(Clock1);
    sf::Vector2f Origin = PlayerObj.GetPosition();
    sf::Vector2f Destination(NewX, NewY);
    sf::Vector2f Direction = Destination - Origin;
    float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );

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

         if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)); {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
            Direction /= Distance;}
            float Speed = 50000.f;
            float DistanceMoved = Speed * Clock1.GetElapsedTime() / 1000.f;
            if (DistanceMoved > Distance) {
            DistanceMoved = Distance;
            }
         PlayerObj.SetPosition(Origin + Direction * DistanceMoved);

35
Graphics / Sprite Movement on Single MouseClick
« on: January 16, 2012, 04:27:20 pm »
Everything seems to be correct, though I may have messed up with declaring the variables outside of the loop?
Code: [Select]
#include <SFML/Graphics.hpp>

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(800, 600), "Sprite Test");

     sf::Image ForestTileImg;
     sf::Image PlayerImg;
     if(!ForestTileImg.LoadFromFile("forest_terrain_tile.png")) {
        return EXIT_FAILURE;
     }
     if(!PlayerImg.LoadFromFile("player.png")) {
        return EXIT_FAILURE;
     }

     ForestTileImg.SetSmooth(false);
     PlayerImg.SetSmooth(false);

     sf::Sprite PlayerObj;
     sf::Sprite ForestTileObj;

     ForestTileObj.SetImage(ForestTileImg);
     ForestTileObj.SetPosition(400,284);

    PlayerObj.SetImage(PlayerImg);
    PlayerObj.SetPosition(400, 284);

    float OldX = PlayerObj.GetPosition().x;
    float OldY = PlayerObj.GetPosition().y;
    float NewX = (OldX + 1);
    float NewY = (OldY - 1);
    sf::Clock::Clock(Clock1);

    sf::Vector2f Origin = PlayerObj.GetPosition();
    sf::Vector2f Destination(NewX, NewY);
    sf::Vector2f Direction = Destination - Origin;
    float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );

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

         if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)); {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
            sf::Vector2f Origin = PlayerObj.GetPosition();
            sf::Vector2f Destination(NewX, NewY);
            sf::Vector2f Direction = Destination - Origin;
            // calculate distance between origin and destination points
            float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );
            // vector normalization (you obtain a unit vector = vector of lenght 1)
            Direction /= Distance;}
            float Speed = 50000.f;
            float DistanceMoved = Speed * Clock1.GetElapsedTime() / 1000.f;
            if (DistanceMoved > Distance) {
            DistanceMoved = Distance;
            }
         PlayerObj.SetPosition(Origin + Direction * DistanceMoved);

         App.Draw(PlayerObj);
         App.Draw(ForestTileObj);
         App.Display();
     }
     return EXIT_SUCCESS;
 }

36
Graphics / Sprite Movement on Single MouseClick
« on: January 14, 2012, 05:20:48 pm »
Hey Tex Killer, I tried to implemented the changes you suggested, with declaring the Direction, Distance, and Origin vectors outside the main loop. However, I still have the same problem: The sprite doesn't move.

Code: [Select]
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)); {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
            sf::Vector2f Origin = PlayerObj.GetPosition();
            sf::Vector2f Destination(NewX, NewY);
            sf::Vector2f Direction = Destination - Origin;
            // calculate distance between origin and destination points
            float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );
            // vector normalization (you obtain a unit vector = vector of lenght 1)
            Direction /= Distance;}
            float Speed = 50000.f;
            float DistanceMoved = Speed * Clock1.GetElapsedTime() / 1000.f;
            if (DistanceMoved > Distance) {
            DistanceMoved = Distance;
            }
        PlayerObj.SetPosition(Origin + Direction * DistanceMoved);

37
Graphics / Sprite Movement on Single MouseClick
« on: January 14, 2012, 04:24:54 am »
I dont expect a reply right now since everything I need to now is probably right in front of me....

I have tried working on this for a few days alone now, and have the below code:
Code: [Select]
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
        }
        sf::Vector2f Origin = PlayerObj.GetPosition();
        sf::Vector2f Destination(NewX, NewY);
        sf::Vector2f Direction = Destination - Origin;
        // calculate distance between origin and destination points
        float Distance = sqrt( (Destination.x - Origin.x) * (Destination.x - Origin.x) + (Destination.y - Origin.y) *  (Destination.y - Origin.y) );
        // vector normalization (you obtain a unit vector = vector of lenght 1)
        Direction /= Distance;
        float FrameTime = App.GetFrameTime();
        float Speed = 40 * FrameTime / 1000;
        PlayerObj.Move(Direction * Speed);


Problem is that when I run it, the sprite doesnt move at all. Thanks in advance to anyone willing to help me :)

38
Network / Quick question about the SFML Netowrking tutorials
« on: January 11, 2012, 12:49:47 pm »
Okay, thanks :)

39
Network / Quick question about the SFML Netowrking tutorials
« on: January 11, 2012, 04:53:22 am »
I am attempting to make my game a multiplayer game but know diddly-sqwat about network programming.  Are the tutorials provided on this site in-depth enough for me to adequately learn and apply the skills necessary to do what I want? Or should I buy a C++ Network Programming book.

40
Graphics / Sprite Movement on Single MouseClick
« on: January 06, 2012, 10:38:52 pm »
Alright, I know this is probably frustrating to you continuing to correct me so this is my last reply...I believe I have implemented all of the coding suggestions, but the result is the same.  No matter the distance, the sprite covers it in the same amount of time.

Code: [Select]
       if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
        }
        //Clock
        float ElapsedTime = Clock1.GetElapsedTime();
        float FrameTime = App.GetFrameTime();
        //Initialization of translation
        sf::Vector2f NewPosition(NewX, NewY);
        float duration = 50;
        sf::Vector2f OldPosition = PlayerObj.GetPosition();
        sf::Vector2f Movement = NewPosition - OldPosition;
        //Update translation
        ElapsedTime += App.GetFrameTime();
        float progress = ElapsedTime / (duration);
        sf::Vector2f Current_Position = OldPosition + Movement * progress;
        float speed = 40 * FrameTime / 1000;
        float angle = atan((sqrt((NewX - OldX) * (NewX - OldX))) / (sqrt((NewY - OldY) * (NewY - OldY))));
        Current_Position.x += cos(angle) * speed;
        Current_Position.y += sin(angle) * speed;
        if(PlayerObj.GetPosition().x == NewX && PlayerObj.GetPosition().y == NewY);
        PlayerObj.SetPosition(Current_Position.x, Current_Position.y);


If I take out the following code from the above code...
Code: [Select]
if(PlayerObj.GetPosition().x == NewX && PlayerObj.GetPosition().y == NewY);
It doesn't seem to have an effect.  In conjunction with this article: http://gamedev.stackexchange.com/questions/20175/sfml-moving-a-sprite-on-mouseclick, I think I have done something wrong since I don't seem to need to control it...

41
Graphics / Sprite Movement on Single MouseClick
« on: January 06, 2012, 04:12:27 pm »
How would you calculate the angle between the two vectors without using the distance? Is there some special function I cant find in the documentation.

42
Graphics / Sprite Movement on Single MouseClick
« on: January 05, 2012, 10:17:28 pm »
Quote

...you must forget the distance at all...


Okay...thats a little confusing in my head but okay.

Quote
...and stablish some constant speed. Do it as jmcmorris sugested.


Alright, but how do I implement his coding suggestions? Or better phrased, where do I implement it?

Code: [Select]

40 * FrameRate / 1000

43
Graphics / Buttons moving with view
« on: January 05, 2012, 04:47:13 pm »
For SFML 1.6:

Code: [Select]

App.SetView(App.GetDefaultView())


Place that before your button is drawn and after your player is drawn.

44
Graphics / Sprite Movement on Single MouseClick
« on: January 05, 2012, 01:49:44 pm »
Where would I implement the constant speed in the code?

And by trigonometry, I assume you mean something like this:
Code: [Select]
 float XDistance = sqrt((NewX - OldX) * (NewX - OldX));//always outputs positive number
        float YDistance = sqrt((NewY - OldY) * (NewY - OldY));
        sf::Vector2f NewPosition(NewX, NewY);
        sf::Vector2f OldPosition = PlayerObj.GetPosition();
        float duration = 50;
        sf::Vector2f Movement = sqrt(XDistance * XDistance + YDistance * YDistance);


However, I get the following error:
Code: [Select]
|146|error: conversion from 'double' to non-scalar type 'sf::Vector2f' requested|

45
Graphics / Sprite Movement on Single MouseClick
« on: January 05, 2012, 01:09:06 am »
Alright, I think I am able to follow what you guys are getting at. I tried to alter my code some, and the result was much better, but with one little hiccup.  No matter what the distance my sprite goes, it always covers it within 2 seconds.  How can I alter my current code so that the sprite covers any distance at a constant speed?

Code:
Code: [Select]
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            Clock1.Reset();
        }
        //Clock
        float ElapsedTime = Clock1.GetElapsedTime();
        //Initialization of translation
        sf::Vector2f NewPosition(NewX, NewY);
        float duration = 50;
        sf::Vector2f OldPosition = PlayerObj.GetPosition();
        sf::Vector2f Movement = NewPosition - OldPosition;
        //Update translation
        ElapsedTime += App.GetFrameTime();
        float progress = ElapsedTime / (duration);
        sf::Vector2f Current = OldPosition + Movement * progress;
        PlayerObj.SetPosition(Current);
        App.Draw(PlayerObj);

Pages: 1 2 [3] 4
anything