Public Group

# hove to move an object in c++

This topic is 5042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
what have you got so far?

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
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

##### Share on other sites
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 :-)

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009712
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!