Moving sprite relative to another sprite

• 04-13-2009
Akkernight
Moving sprite relative to another sprite
Hello.

I know the "roterating ship" kinda uses about the same algorithm, but there were some factors there I didn't understand.
Also, sorry if the title is bad...

Well, I have one character class that has a vector of it's position.
This character has a function that moves it towards a position pointer, and I want the fastest and still nice looking algorithm for for this to happen.
What I mean is that the position pointer might change, thus the character needs to change where to move... I do have an algorithm myself, that I understand, but I believe it would be too slow, as the code runs a million times in the minute, and this algorithm makes a new direction each time... (I did describe it in the rotating ship thread)

So has anyone got any advice?

Code:

``` hgeVector m_hVec_position; void MoveRelative(const hgeVector &position_, const float speed_){         hgeVector* p_position = &position_; }```
that's what I've got so far :S
hope I was clear...

• 04-13-2009
Shakti
So you have an object at point A, and want to move it towards a moving object at point B i take it?

Construct a normalized vector betwean the 2 and have that be your directional vector and move object at point A along that vector. This vector only needs to be recalculated when object at point B moves. After that just add some fluff-fluff (you could make it so the A object only can turn a certain amount per second, and if you keep a constant speed forward you would have to recalculate it every time A or B moves but would give nicer curve movement if the delta change in B is large).
• 04-13-2009
Akkernight
I know this method, still when B moves, it would be calculated a million times, since B can move for more than a minute, infact it can move for an hour and so on...
I thing the overhead would be a killer with this method...
• 04-13-2009
Shakti
I cant think of a way to not having to recalculate it, if B moves around randomly you would have to recalculate your direction as soon as B moves. And its not that many calculations, you still only run them once every frame so maybe 60 times/second? Thats not overly bad.
• 04-13-2009
Akkernight
well, I see the calculations as graphics in my head automaticly xD if each calculated direction would be drawn in white, the whole screen would go white,, if the object went all the way around the screen...
But I also noticed, I don't need this :S
But thanks anyways!
• 04-13-2009
VirtualAce
Calculating per frame should not be an issue. The only other way to do this is to calculate the vector once and then alter it relative to the base line vector you calculated. So subtract the current vector to the target from the known calculated vector and use the offset to change the pre-computed target vector.