Confused with velocity (SDL)

I tried to make a pathfinder using coords. Its about a reddot following a dot.
Over here is the event handler for the first dot:
1
2
3
4
5
6
7
8
//Adjust the velocity
        switch( e.key.keysym.sym )
        {
            case SDLK_UP: mVelY -= DOT_VEL; Dot_xCoords++ ; break;
            case SDLK_DOWN: mVelY += DOT_VEL; break;
            case SDLK_LEFT: mVelX -= DOT_VEL; break;
            case SDLK_RIGHT: mVelX += DOT_VEL; break;
        }

I made a "Dot_xCoords" so that i can test if i can use it as a pathfinder.
"RedDot_xCoords" is equal to 0.
Here is me attempting:
1
2
3
4
5
6
7
8
void RedDot::pathfinding()
{
	if(Dot_xCoords > RedDot_xCoords)
	{
		mVelX -= REDDOT_VEL;
                RedDot_xCoords++;
	}
}

The purpose of this is to make it move for now.
Is there something obvious that im missing?
I am using a lazyfoo tutorial if your familiar with it.
Last edited on
Hi there,

I would suggest moving your question to the "general C++ programming" forum, where it may be picked up by someone who knows about SDL. Alternatively, you could try at an SDL specific forum.

Hope that helps.

All the best,
NwN
Halp.
I'm not sure what velocity has to do with pathfinding. Maybe I'm thinking you mean something else from what you do?


So you have a dot and a red dot. When the dot moves, the red dot follows behind it. Right? Are there walls or other obstacles that the red dot has to move around or can it just go in a straight line towards the dot?


EDIT: Also I almost didn't respond because you typed "halp". *cringe*
Last edited on
Well I used the word pathfinding because it moves the red dot towards the player dot. But I have to admit, pathfinding is for obstacles, and for now there are none.
Anyways...
The problem is that the red dot does not move.
If someone knows lazyfoo's tutorials well, they can list what else i have to do. This is what i added that is related to the "pathfinding".
(In main, the comment is just to reference to where exactly in the tutorial it is)
1
2
3
					//Handle input for the dot
					...
					reddot.pathfinding();

And that is all that i added, i bet I'm missing something.


I don't know LazyFoo's tutorials that well... but if you're just copy/pasting the code from the tutorial without trying to understand what it's actually doing, then it's no wonder you're running into problems.

My advice is to put the tutorials aside for now and try to solve this problem without trying to just find out "how did I mess up the tutorial".


So the general flow of a game is:

1) Process events
2) Do logic updates (ie: move everything, do collisions, etc)
3) Draw the scene
4) Repeat until user wants to quit


You seem to be having a logic problem. Your red dot is not moving when you want it to be.

The way it usually works is:
a) "Velocity" modifies position
b) "Force" modifies velocity

So in your case, your red dot has a position and a velocity. So for point 'a' all you have to do in the logic update is:

 
position += velocity;


That's it. It's that simple.

The trickier bit is part 'b'. Applying force to change the velocity based on where the red dot is relative to the player dot. But this isn't particularly complicated. You could have some simplistic logic:

- if the player dot is to the left of the red dot.... apply force to move the red dot left (-x velocity)
- if the player dot is to the right of the right dot... apply force to move the red dot right (+x velocity)
- ditto for Y coord.
Anyways guys I was lazy and threw this question into the forums just to see if you guys can do it for me :1
This forum has gave me no help so far, seriously.
Yea, I got it working because you guys were taking too much time.
The problem was that after:
mVelX -= REDDOT_VEL,
i had to do another function called "move()"
in that i put:
mBox.x += mVelX
Yea...
I'm gonna go to a different forum now.
closed account (3qX21hU5)
Wow.

We are not your employees we dedicate OUR time for free to help out others like you because we want to not because it is our obligation to.

It really is people like you that make a lot of people stop finding joy in helping others on here (Myself included). The people that answered your question actually gave you great answers that would have helped you improve as a programmer greatly but YOU chose to ignore them.

I don't feel bad in saying you aren't going to make it far in programming that attitude and please find another programming forum ;)
Last edited on
in that i put:
mBox.x += mVelX


If you would have read my post.. I believe I told you to do exactly that:

I wrote:
So in your case, your red dot has a position and a velocity. So for point 'a' all you have to do in the logic update is:

position += velocity;




This forum has gave me no help so far, seriously.
[snip]
Yea...
I'm gonna go to a different forum now.


I thought I gave you a pretty detailed answer. But whatever. If you're too lazy to read and understand it, then I'm going to be too lazy to help you.

Bye.
Last edited on
You shouldn't be copy pasting other peoples code together and asking for help when it doesn't work unless you understand most of the work behind it in the first place.

The fact of the matter is that the lazyfoo tutorials are meant to help you understand the basics of sdl. The methods he/she uses to explain the algorithmic functionality he/she developed is to help you get your feet wet and build something of your own.

If you are having trouble understanding the basic concepts of pixel coordinates and algorithms used to alter display coordinates, then the truth of the matter is that people don't need to know SDL at all in order to help you, since your problem is mathematical in essence.

Your question was ambiguous, and you basically asked people to go through 20 or so lessons just to figure out what it is you are talking about, when the code you posted had absolutely nothing to do with SDL aside from the handling of input (which in this instance was irrelevant).

What you should have done:

Think : If the dot appeared, but did not move, then the problem lies in 1 of 2 places.
1: The screen is not updating with updated coordinates for the red dot -> SDL issue -> look at documentation of the functions your working with and figure out what you're missing (http://wiki.libsdl.org/CategoryAPI)
2: The code to move the dot is malfunctioning -> SDL Does not have velocity and or movement functions -> Problem is mathematical -> Figure out math being used -> If running into a problem, post the math.

P.S. There are forums on SDL's home site http://forums.libsdl.org/
Topic archived. No new replies allowed.