The Daily Click ::. Forums ::. Klik Coding Help ::. Velocity
 

Post Reply  Post Oekaki 
 

Posted By Message

CakeSpear



Registered
  28/01/2004
Points
  112
7th January, 2010 at 14:00:43 -

Hi, im trying to make an object move by using X and velocity.
I also want the movement to have a 'max speed'.
I can limit the X and Y velocity to, lets say -6 to +6. But when Xvel=6(or -6) and Yvel=6(or -6), the movement of the object gets way faster than 6. Any ideas on how to fix this?

Edited by CakeSpear

 
n/a

CakeSpear



Registered
  28/01/2004
Points
  112
7th January, 2010 at 15:19:45 -

Im moving the object like this:
Allways
---> setXposition to X( "Object" )-Xvelocity_( "Object" )
---> setYposition to Y( "Object" )-Yvelocity_( "Object" )

Edited by CakeSpear

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
7th January, 2010 at 15:20:55 -

If XVel and YVel are both 6, then speed = Sqr((6 pow 2)+(6 pow 2)) = 8.5 (according to pythagoras' theorum)
You need to use a bit of vector maths...


Find the length of the movement vector (ie. velocity):
Length = Sqr((XVel pow 2) + (YVel pow 2))

Find the unit vector (in same direction, but with length = 1) of the movement vector:
XUnit = (1 / Length) * XVel
YUnit = (1 / Length) * YVel

If the length of the movement vector is greater than the maximum velocity, make it equal to the maximum velocity:
NewLength = Min(Length, MaxVel)

Mulitply each unit vector component by the new vector length:
XVel = NewLength * XUnit
YVel = NewLength * YUnit



Or, if you want all that lot combined into one long expression (slightly less efficient):
XVel = ((1 / Sqr((XVel pow 2) + (YVel pow 2))) * XVel) * Min( Sqr((XVel pow 2) + (YVel pow 2)), MaxVel )
YVel = ((1 / Sqr((XVel pow 2) + (YVel pow 2))) * YVel) * Min( Sqr((XVel pow 2) + (YVel pow 2)), MaxVel )

Edited by Sketchy

 
n/a

CakeSpear



Registered
  28/01/2004
Points
  112
7th January, 2010 at 16:12:31 -

If i dropped Newlength, and only did:
Length = Min(Sqr((XVel pow 2) + (YVel pow 2)) , MaxVel)
Would that affect the outcome somehow?

And i dont get:
XUnit = (1 / Length) * XVel
YUnit = (1 / Length) * YVel

XVel = NewLength * XUnit
YVel = NewLength * YUnit

XYUnit and XYVel wil constantly change eachother(???)

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
7th January, 2010 at 17:11:53 -

The basics first:
a "vector" has a direction and magnitude (eg. movement vector),
a "unit vector" has only a direction,
a "scalar" has only a magnitude (eg. length).

The length (magnitude) of the movement vector represents the actual speed of movement. You calculate it using Pythagoras' theorum.
eg. If XVel and YVel are both 6, then Length = 8.5 (see my last post)
It is a scalar.

XUnit and YUnit are the X and Y components of a "unit vector", in the same way that XVel and YVel are components of the movement vector..
The "unit vector" is a vector going in the same direction as the original movement vector, but with a length of 1.
Basically, it's just a way of describing the direction of the movement vector alone (ie. not it's length).
It is a unit vector.

NewLength represents the final speed (length of the movement vector), after it's been limited to the maximum speed.
If you want to just say: Length = Min(Length, MaxVel) then that's fine, but you still have to do it after calculating the unit vector.
I was just attempting to explain the maths theory one step at a time though...
It is a scalar.

You can multiply a vector by a scalar, simply by multiplying each vector component by the scalar.
In this case, we are multiplying the movement direction (the unit vector) by the movement speed (NewLength).

To be honest, vector maths is far too complicated for me to explain it all here. If you want a thorough explanation, look on wikipedia etc - http://en.wikipedia.org/wiki/Euclidean_vector - otherwise just use what I've written - unless anyone else feels like trying...

 
n/a
   

Post Reply



 



Advertisement

Worth A Click