hove to move an object in c++

Started by
4 comments, last by mnansgar 19 years, 3 months ago
One object moves along a path,which is an array of [3][3] and the other walks from e.g. Point(14,9) to Point(20,9) and i can't write the C++ code for them:)) I'll be glad if anyone could help me!!
Advertisement
what have you got so far?
the most basic kind of movement is having a RECT witch has a x and y. you use that for were the top left corner of your picture will be drawn the you just increment it ever cycle based on a number of factor for example

RECT A
OBJECT B
draw_at(A);
A.x+=2;
A.y+=2;

this will make the object move diagonaly downward because screen coordeniths are like this
Decreasing X <- X -> increasing X
Axis-------------------------------------------------------------------------
|
|
|
| ^
|Y |deacreaing Y
|
| |
| V increasing Y
|
|

hope i helped
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:the other walks from e.g. Point(14,9) to Point(20,9)


This one is straightforward. Every frame, your "object" needs to walk a constant distance, in this case in a straight line between those two points. If you render at, say, 30 fps, and you want him to arrive there in one second, then first we calculate the amount you'd have to increment in the x and y directions each frame:

float delX = (x1 - x0) / (seconds * fps);
float delY = (y1 - y0) / (seconds * fps);

Then we simply update the object's position every frame by those values:

objX += delX;
objY += delY;

For your example, let's see if this works:
delX = (20 - 14) / (1.0 * 30.0) = 0.2f
delY = (9 - 9) / (1.0 * 30.0) = 0.0f

Thus, if we run the simulation for one second:
objX += 0.2f * (1 * 30) = 6; Thus, objX = 14 + 6 = 20.
objY += 0.0f * (1 * 30) = 0; Thus, objY = 9 + 0 = 9.

Thus, we arrived at the second point in exactly 30 frames, as we previously calculated. As for the path movement, you'll have to be more specific about the contents of your [3][3] array for us to give you advice! Hopefully knowing this much will give you a better chance at figuring it out on your own though ;-)

[Edited by - mnansgar on January 1, 2005 10:58:50 AM]
h20, member of WFG 0 A.D.
the object moves along the path[3][3] and the user doesn't have any control over this path.
simply,
first at (1,1) then (1,2) then (1,3) then (2,3) and so forth...(coordinates are fixed)

by the second part of my question,i forgot to write, theobject moving along aline should also come back:) in an infinite loop..
thanx
Quote:by the second part of my question,i forgot to write, theobject moving along aline should also come back:) in an infinite loop..
thanx

To make it come back, just use the same logic as I proposed above, except just swap the two points around! When you switch which point is x0 vs. x1, for instance, then the delX will be negative what it used to be. Thus, the object will move in the opposite direction.

Quote:
simply, first at (1,1) then (1,2) then (1,3) then (2,3) and so forth...(coordinates are fixed)

This is just an extension on the MoveToPoint question ... if you have a series of points (a "path") then when it reaches one point, apply the MoveToPoint algorithm like I illustrated earlier to have it move to the next point in the series (and so on and so forth if you want it to go backward as well).

If this is too vague, reply and I can try to make what you don't understand a little clearer :-)
h20, member of WFG 0 A.D.

This topic is closed to new replies.

Advertisement