Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Moving diagonaly how to calulate from horizontal and vertical speed

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.

If i have a "unit", it can move horizontaly, verticaly and diagonaly.

If it moves only horizontaly its movement speed is 1 pixel, same goes for vertical speed, but what is formula to calculate for diagonal speed?

I made it using "Pythagorean theorem" and i figured out i speed up the "unit" instead of reducing its speed.

 

I am trying to achieve that if a unit is moving 10sec horizontally then 10 sec vertically to be same position as unit moving 20 sec diagonally.

Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Advertisement

I made it using "Pythagorean theorem" and i figured out i speed up the "unit" instead of reducing its speed.

 

You can't use the theorem as we've learnt it here directly. You need to reverse it, since your question is "how much units on the x and y axis do I need to move the unit to get a diagonal movement of 1", not "How much diagnal movement do I get from moving the unit both 1 on the x and 1 on the y axis". So if you have:

 

a^2 + b^2 = c^2

 

You have already given c, and are looking for a and b. Those are the same, so it comes down to:

 

2*a^2 = c^2

a^2 = (c^2) / 2

a = sqrt( c^2 / 2)

float baseSpeed = 1.0f;

float speedWhileMovingDiag = sqrt( (baseSpeed*baseSpeed) / 2); // should equal ~ 0.7f

I could have just told you to flip "* 2" in the calculation to "/ 2", but I hope I've given you a better explanation that way.

Edited by Juliean

Share this post


Link to post
Share on other sites

Hello.

If i have a "unit", it can move horizontaly, verticaly and diagonaly.

If it moves only horizontaly its movement speed is 1 pixel, same goes for vertical speed, but what is formula to calculate for diagonal speed?

I made it using "Pythagorean theorem" and i figured out i speed up the "unit" instead of reducing its speed.

 

I am trying to achieve that if a unit is moving 10sec horizontally then 10 sec vertically to be same position as unit moving 20 sec diagonally.

 

if you really want a diagonally moving unit to reach the end position at the same time as one that goes horizontally then vertically you need to slow it down when moving diagonally (as the diagonal distance is significantly shorter).

 

if you move 0.5px vertically and 0.5px horizontally when you move diagonally you will reach the end point at the same time regardless of how you move. (the actual speed you'll be moving then is roughly 0.7px per tick)

Share this post


Link to post
Share on other sites


if you move 0.5px vertically and 0.5px horizontally when you move diagonally you will reach the end point at the same time regardless of how you move. (the actual speed you'll be moving then is roughly 0.7px per tick)

 

Ah, good catch, but let me add for the OP: This does not resemble the actual effect of movement, if you are moving diagonally, you'll always save time, then if you where going on the two seperate axis. So unless you really want to achieve that very effect, the formula I posted should be correct, and will result in you being roughly sqrt(2) times faster than if you divided the speed / 2.

Share this post


Link to post
Share on other sites
I am trying to achieve that if a unit is moving 10sec horizontally then 10 sec vertically to be same position as unit moving 20 sec diagonally.

A unit moving horizontally and vertically over 10 seconds would be in the same position of a unit moving 10?2 seconds diagnally, at the same speed. Just saiyan.

Share this post


Link to post
Share on other sites

a = sqrt( c^2 / 2)

float baseSpeed = 1.0f;

float speedWhileMovingDiag = sqrt( (baseSpeed*baseSpeed) / 2); // should equal ~ 0.7f

 

this is the same as baseSpeed * sqrt(2) which is 1 constant multiplication

in practice, you never want to do straight diagonal movement, and instead be using vectors

regardless of any specific needs or rules you have defined already

vectors are simple enough to not bother setting anything in stone, especially with math as primitive as this =)

 

of course if the OP is not interested in vectors at the moment, that is fine

if you create a new topic regarding vectors, or just continue this thread, me and many others can give you the basics

it's very helpful to get started early with vectors, as they open alot of doors and simplifies things

Edited by Kaptein

Share this post


Link to post
Share on other sites
@Kaptein:

1 * sqrt(2) = ~1.4
Sqrt( 1 / 2) = ~0.7

How is that supposed to be the same? You mean basespeed / sqrt(2), right?

Share this post


Link to post
Share on other sites

Doing just x and y movement should allow you to not need diagonal movement. You don't need to explicitly move diagonaly if that's what you are doing so far.

velocity.x = 10;
velocity.y = 10;

Loop( Time )
{
//Time is the time elapsed since the last call of this function
position.x = velocity.x * Time;
position.y = velocity.y * Time;
}

Share this post


Link to post
Share on other sites

@Kaptein:

1 * sqrt(2) = ~1.4
Sqrt( 1 / 2) = ~0.7

How is that supposed to be the same? You mean basespeed / sqrt(2), right?

 

Yes, sorry, brainfart :)

Share this post


Link to post
Share on other sites

Try plotting X = 20Sin(t) and Y = 20cos(t).   t = time/tick, X,Y are points.    

 

You'll find yourself drawing a nice little circle, and by being a Circle that means that every point is the same distance from 0,0.   Which you could use as your speed.

 

The hard part is finding the angle you want the player to move at and how to plug that into the trig equations.   But still basic trig, and even with minimal understanding you can still get it to work(I did in early highschool for a missile command game).  But the end result will allow you to move at any angle and similar.   

__________

 

if you just want to be able to move at 45 degree angles than the distance the character will move along x and y is just Sin(45 degrees) Or Sin(1/4 * Pi) which will be around .707107 * velocity.    

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!