Page 1 of 1

Vector Calculus and sf::Sprite Questions

Posted: Tue Jan 18, 2011 7:17 pm
by THe Floating Brain
Hello Lately I have had a couple problems with vector calculus and sf::Sprites (SFML sprites). I know how to calculate a vector but I do not know how to get a object to move along the vector properly. I understand why my object will not move along a vector properly but I just don't know how to get it to move along the vector properly. When it moves it moves in a sort of "Lopsided" fashion I suppose (at a loss or words there) like in this example: http://www.youtube.com/watch?v=7MYNpez8q1g
If you look at my movement function you may understand why it outputs this behavior. (I hope I have commented and organised it enough :oops: it looks long but some things are repeated so just bear with me here :-) )
http://pastebin.com/LwGGiY8A
If anyone can help me fix this "lopsided movement" it would be greatly appreciated :-)
My other question is: how do I properly "destruct" a sf::Sprite? I have successfully done this but sometimes I get a runtime error saying "pure virtual function" I am using SFML 1.6.
This is how I am currently doing it:

Code: Select all

//The std::vector "LASORSMirror" currently contains sf::Sprites that are not allocated from the heap.//
LASORSMirror[V].sf::Drawable::~Drawable();
I am wondering if I should just add a destructior to sf::Sprite but I am not sure if I would mess anything up plus I haven't read the terms of use well enough ;-) .
Any help will be greatly appreciated and thank you for reading :-D

p.s I know I sometimes have problems getting my point across if any clarification is needed please ask :-) .
-------EDIT-------
Btw I am using C++ :-) .

Re: Vector Calculus and sf::Sprite Questions

Posted: Tue Jan 18, 2011 10:24 pm
by Ginto8
OK if I figure out the vector issue I'll edit my post but for now WHY WHY WHY on EARTH are you calling a destructor manually? Is it for fun? Are you developing a hobby of seeing what kind of shit you can mess up the C++ runtime with? Because honestly that is BAD BAD BAD.

EDIT: Alright, what IS your problem with the vectors? from what I got from a fairly cursory glance is that in the video a fighter jet moves around, as does a box, you have a weird mouse cursor, and white rectangles shoot out of a single point and get caught by the box. The code has lots of unexplained parameters and honestly I don't have enough context to understand ANY of it. I want to help you, but honestly right now all I'd be doing is shooting a shotgun into a room and hope that one of the shells hits the zombie that's hiding behind one of the pieces of furniture. Tell us exactly WHAT it's supposed to be doing, what it isn't doing, and what it's doing that it shouldn't. Once we know that, we can figure it out. Code debugging is like being a doctor; you can't diagnose based on inaccurate or incomplete information. Even House isn't THAT awesome.

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 3:41 pm
by THe Floating Brain
Well as I said
p.s I know I sometimes have problems getting my point across if any clarification is needed please ask .
so I knew this would happen XD .
WHY WHY WHY on EARTH are you calling a destructor manually?
: Well since I am using zero memory heap allocation and I am using std::vectors to store objects I am unable to utilize the .erase() function on the std::vector's and I can not call the delete function to delete any objects there-four I must call destructors manually.
Alright, what IS your problem with the vectors?
: (In the video) Pay no attention to the space ship thingy or the lasers. Notice the white square (it being a white square because I am currently having a image loading problem with that particular object :roll: ) I click on it with the
weird mouse cursor
then right click somewhere else with the cursor. After that the white square moves to were the cursor is/previously was. but if you will notice the movement at first is diagonal but then it goes straight.
Ex.

Code: Select all

\
  \     <- wanted result.
    \
      --------- <- un-wanted part of result.

:-(
This would not be a problem if I did not want to use the same function on projectiles. (Seriously who ever herd of a projectile that does that :lol: )
This is caused because the object is offset by its speed and will not stop moving until both the X and Y coordinates for the object are at there destination.
This means that if one axis is greater than the other then it will eventually continue to move on the axis with a greater value and not the lesser one.

Code: Select all

 (hence the) ----- (from the above example)
I am unsure how to avoid this. Right now I just got home so I will post a better (meaning more commented) version of the code later. (Probably in a hour or 2)

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 3:47 pm
by thejahooli
THe Floating Brain wrote:I must call destructors manually.
Do compilers even let you do that? I would have though that that would have produced some kind of error.

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 3:58 pm
by Ginto8
THe Floating Brain wrote:: Well since I am using zero memory heap allocation and I am using std::vectors to store objects I am unable to utilize the .erase() function on the std::vector's and I can not call the delete function to delete any objects there-four I must call destructors manually.
... You do realize that std::vectors, by definition, use heap allocation?

Also, if you're going to have so much functionality in one function, at least break it up a little into smaller functions for readability's sake. Along with that, the sheer amount of parameters is pretty ridiculous ;)

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 5:24 pm
by THe Floating Brain
... You do realize that std::vectors, by definition, use heap allocation?
Yes but arrays dont have a .push_back() function :-) .
Also, if you're going to have so much functionality in one function, at least break it up a little into smaller functions for readability's sake. Along with that, the sheer amount of parameters is pretty ridiculous ;)
I did... that is only one of four XD
here is the more commented code:
http://pastebin.com/PC5uRQqg
Yes the function arguments are still messy BUT I explaned them.

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 8:14 pm
by xiphirx
Your English made my brain hurt.

First thing I noticed, your function is named RTSDestinationSetVectorCalculationAndMovement ... I think this is self explanatory.

Code: Select all

//I wanted less to type so I made a pointer to the varible.//
                        bool calcdone = VectorCalculationCompleteVar;
                        bool *calcdonep = &calcdone;
what?... just plain what?

Code: Select all

MovingY / 0;
MovingX / 0;
Okay, what the fuck is this? Honestly?

This is how I think you should do it.

In your object's event handling function, check whether the mouse issued a move command, if so, set the currentAction to move, and save the mouse coordinates in a vector

Code: Select all

if (mouse->button(RIGHT) && selected)
{
     currentAction = MOVE; //enum
     moveTo.x = mouse->x;
     moveTo.y = mouse->y;
}
Then in your update function

Code: Select all

switch (currentAction)
{
    ...
    case MOVE:
           vec2 to;
           to.x = mouse->x - moveTo.x; 
           to.y = mouse->y - moveTo.y;
           if (to.x*to.x + to.y*to.y < 1)
           {
                  currentAction = IDLE;
           }
           else
           {
                 to.normalize();
                 position.x += to.x;
                 position.y += to.y;
           }
           break;
    ....
}
This should make your object move to wherever you click.

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 8:56 pm
by Ginto8
THe Floating Brain wrote:
... You do realize that std::vectors, by definition, use heap allocation?
Yes but arrays dont have a .push_back() function :-) .
Yes but using a std::vector means you ARE using heap allocation. Even if you weren't, there is ZERO reason to manually call a destructor. Also, take a look at what live is pointing out... that's some fucked up shit

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 8:59 pm
by THe Floating Brain
Wait wouldent

Code: Select all

                       position.x += to.x;
                       position.y += to.y;
                       
just make my object jump to where I want it to go instead of move beacuse of SFML moves things (by offseting them) Ex.

Code: Select all

Obj
     


                   Obj
instad of

Code: Select all

Obj
     \
       \
         \
           \
             Obj
???
Or is this accounted for when you normilise the vector? (Sorry I am realy realy bad at math XD)

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 9:10 pm
by THe Floating Brain
Ginto8 wrote:
THe Floating Brain wrote:
... You do realize that std::vectors, by definition, use heap allocation?
Yes but arrays dont have a .push_back() function :-) .
Yes but using a std::vector means you ARE using heap allocation. Even if you weren't, there is ZERO reason to manually call a destructor. Also, take a look at what live is pointing out... that's some fucked up shit
Oh ooooooooops... I should take a look at that. But im not allowcating into a std::vector I create a instance of whatever I am making a instance of, and then use a function to return its value into the std::vecotrs .push_back function therefoure there is no memory heap allocation buuuut If I want to destroy a element in it say A space ship got blown up (Space RTS) then there is reason to call its destructor. I am having no problem calling the destructor of anything else manully with the exception of sf::Drawable.
Ex.

Code: Select all

class Lasor
{
      //...//
};
std::vector<Lasor> Lasors;
//MiscFunctions is a class in my engine.//
class Ship : public MiscFunctions
{
    public:
        refresh()
         {
              //...//
               if(count == time)
               {
                   Lasor A;
                   InstanceCreate B;                   //"InstanceCreate" is a class that is a member of MiscFunctions.//
                   Lasors.push_back(B.CreateInstance(A)); //"CreateInstance" takes a instance of a class and returns its value.//

              }
          }
};

Re: Vector Calculus and sf::Sprite Questions

Posted: Wed Jan 19, 2011 9:45 pm
by xiphirx
THe Floating Brain wrote:Wait wouldent

Code: Select all

                       position.x += to.x;
                       position.y += to.y;
                       
just make my object jump to where I want it to go instead of move beacuse of SFML moves things (by offseting them) Ex.

Code: Select all

Obj
     


                   Obj
instad of

Code: Select all

Obj
     \
       \
         \
           \
             Obj
???
Or is this accounted for when you normilise the vector? (Sorry I am realy realy bad at math XD)
No. If you normalize the vector beforehand, it wont.

Re: Vector Calculus and sf::Sprite Questions

Posted: Fri Jan 21, 2011 1:31 pm
by THe Floating Brain
Thank you :-)