# what is the correct way to move object along direction

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

## Recommended Posts

I have some object anywhere in 2D World, and unit vector/degree and i would like to move the object on that direction from his origin .

how should I do that ? I thought about using ax+c , compute a using tan , and then calculate C and move the object , like that :

PlayerX += power * timestep;

PlayerY = PlayerX*A+C;

So what are the correct way to do that ?

I have some object anywhere in 2D World, and unit vector/degree and i would like to move the object on that direction from his origin .

how should I do that ? I thought about using ax+c , compute a using tan , and then calculate C and move the object , like that :

PlayerX += power * timestep;

PlayerY = PlayerX*A+C;

So what are the correct way to do that ?

Edited by MaorNr

##### Share on other sites

Ideally you should be moving completely stateless but this is rarely possible.

Moving by adding small amounts to persistent state like you plan to do is more flexible but there's a quirk: it accumulates floating point error and you must take care this doesn't grow out of control. You do this by updating at regular intervals rather than whatever possible. see: http://gafferongames.com/game-physics/fix-your-timestep/.

For a smoother movement, you'll need to keep multiple positions at n and n+1 tick so you can interpolate them. If your game is simple enough and framerate high enough you can forget about this complication so I guess you should elaborate on how accurate you want to be.

That said, the way you move your object across a line is... what you need I assume. It's perhaps not much a matter of "how" but rather of "where" this moving code should exist. Are you going to put it in the basic object itself? In some other controller? If this does not make much sense to you, take it easy and explore your problem.

##### Share on other sites

If your app includes 2D vector math support, calculate the unit vector direction. The player's position (another 2D vector), assuming you choose a speed** for the movement is:

PlayerPos += dir_vector * speed * delta-time

**It's not clear what you intend by "power."

To do it without 2D vectors, maintain the calculated direction as a pair of numbers, dirX and dirY. Ensure that dirX2 + dirY2 = 1.

PlayerX += dirX * speed * delta-time

PlayerY += dirY * speed * delta-time

hey thanks , its works fine .

Just antoher little question :)

somtimes some object move faster , and I couldnt check for collision, because the steps are too big, how can I solve this ?

##### Share on other sites

somtimes some object move faster , and I couldnt check for collision, because the steps are too big, how can I solve this ?

If your cycle can handle it, you could update the really fast objects (for examples: bullets) more than one time per update. Divide the big step in little steps and check them all in one update.

Or, you can look into swept collision: one example but google will probably show you more.

Cheers.

##### Share on other sites

somtimes some object move faster , and I couldnt check for collision, because the steps are too big, how can I solve this ?

Depends on how your collision detection system works. You don't mention a particular system. As desdemian mentions, perhaps you can shorten your collision checking delta-time, or "swept volumes**" will work for you.

**For 2D, I guess that would be swept areas.

##### Share on other sites

Yeah, depending on the objects in question, it can be better to just treat them as lines/rays for bullets, or rectangles for large things.

##### Share on other sites

You can also get a direction vector from an angle:

//theta is a float value between 0->2pi
dirX = speed * cos(theta);
dirY = speed * sin(theta);

So, movement in a direction is pretty simple:

Position.X += dirX;
Position.Y += dirY;

Then you can wire up some input controls to change the direction and speed.

##### Share on other sites

i too use saymin's method:

for fixed ET:

for variable ET:

as mentioned above, stepped movement is the usual way to handle fast moving objects like bullets. first you determine a step size. it should be as large as possible (to reduce iterations), while still being small enough that no object can pass from one side of an object to another in a single step.

then you divide the movement amount by the step size to get the number_of_steps, and the remainder.

then:

for a=0; a<number _of_steps; a++

{

check for collisions

}

followed by:

// move the remaining amount...

check for collisions

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013450
×