Jump to content


Photo

Math Question Solved


  • Please log in to reply
6 replies to this topic

#1 SenakuJin

SenakuJin

    GMC Member

  • New Member
  • 2765 posts

Posted 16 August 2009 - 03:10 PM

Alright, as you know speed is a vector comprising of velocity and direction. However it can also be broken down into horizontal speed (hspeed) and vertical speed (vspeed). Now say i have something that is at:

hspeed=2;
vspeed=4;

the speed value would be:

spd=point_distance(0,0,hspeed,vspeed);

Now i can change hspeed to something like 4, but that would increase the overall speed naturally. However i can conform this by simply setting the speed again, this will change the direction as i wished, but also keep the same speed:

hspeed=2;
vspeed=4;
spd=point_distance(0,0,hspeed,vspeed);

hspeed+=2;
speed=spd;

So my question is, instead of simply typing speed=spd; what would be the math equivalent to conforming hspeed and vspeed as they do when i type speed=spd; any help is appreciated ^ ^

Edited by SenakuJin, 16 August 2009 - 04:03 PM.

  • 0

#2 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6261 posts
  • Version:GM:Studio

Posted 16 August 2009 - 03:30 PM

I think...

var old_speed;
old_speed = point_distance(0, 0, hspeed, vspeed);
hspeed *= new_speed / old_speed;
vspeed *= new_speed / old_speed;

  • 0

#3 SenakuJin

SenakuJin

    GMC Member

  • New Member
  • 2765 posts

Posted 16 August 2009 - 03:44 PM

For some reason that doesn't return the value i am looking for... maybe there is a problem w/ my set up. Here is what i have:

Create Event:

hspeed=2;
hspd=2;
vspeed=4;
vspd=4;
spd=point_distance(0,0,vspeed,hspeed);

Draw Event:

draw_sprite(sprite_index,-1,x,y);
draw_text(0,0,"spd:"+string(speed));

draw_text(0,40,"hspd:"+string(hspeed)+"/"+string(hspd));
draw_text(0,60,"vspd:"+string(vspeed)+"/"+string(vspd));

if(keyboard_check_pressed(vk_space)){
vspeed+=1;speed=spd;
nspd=point_distance(0,0,vspd+1,hspd);
hspd*=nspd/spd;
vspd*=nspd/spd;
}

Did i not use the code you gave me correctly?
  • 0

#4 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4201 posts
  • Version:GM8.1

Posted 16 August 2009 - 03:47 PM

What you're doing here is totally stupid. Hspeed and vspeed changes into direction and speed automatically (and vice versa). You don't need to calculate it.
  • 0

#5 SenakuJin

SenakuJin

    GMC Member

  • New Member
  • 2765 posts

Posted 16 August 2009 - 03:52 PM

Well i would like to use it outside of GM in some collision physics, so while it may be stupid in the GM environment, i would still like to know the math behind it to be used else where.

EDIT: lol, Tepi you actually helped. The solution is:

vspd+=1;
ndir=point_direction(0,0,hspd,vspd);
hspd=lengthdir_x(spd,ndir);
vspd=lengthdir_y(spd,ndir);

or in math language:

vspd+=1;
ndir=arctan2(vspd,hspd);
hspd=cos(ndir)*spd;
vspd=sin(ndir)*spd;

(i may of mixed up cos and sin, not sure...)

Edited by SenakuJin, 16 August 2009 - 04:03 PM.

  • 0

#6 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4201 posts
  • Version:GM8.1

Posted 16 August 2009 - 04:00 PM

(speed, direction) into (hspeed,vspeed):
hspeed = lengthdir_x(speed, direction) =  cos(pi/180*direction)*speed;
vspeed = lengthdir_y(speed, direction) = -sin(pi/180*direction)*speed;
(hspeed, vspeed) into (speed, direction):
speed	 = point_distance(0,0,hspeed,vspeed)  = sqrt(hspeed*hspeed+vspeed*vspeed);
direction = point_direction(0,0,hspeed,vspeed) = arctan2(vspeed,hspeed)*180/pi;

Mnementh's code is rather flawed. It divides the velocity by its old magnitude and scales by its new, letting it become just as long as it will become anyway. It should work if the '*' is turned into '/', but that contains way too much extra calculations. Do it the same way as before, but store the old speed as 'speed', not 'point_distance(0,0,hspeed,vspeed)'.

Edited by Tepi, 16 August 2009 - 04:02 PM.

  • 0

#7 xshortguy

xshortguy

    GMC Member

  • Global Moderators
  • 4186 posts
  • Version:GM:Studio

Posted 16 August 2009 - 07:12 PM

Alright, as you know speed is a vector comprising of velocity and direction.


This is incorrect. Velocity is a vector that is defined as the instantaneous change in position w.r.t. time. The magnitude of the velocity is called speed. Speed is a scalar value, and not a vector.

Let's say your object is moving with a magnitude of R in a direction A (in radians). The velocity vector is then given by the ordered pair (R, A) (note that this ordered pair is described using polar coordinates, and not standard Euclidean coordinates). Let (x, y) be the Euclidean representation of this vector.

The relationship between (R, A) and (x, y) is given by the following:
(x, y) = ( R * cos(A), R * sin(A) )
or
( sqrt( x^2 + y^2), arctan(y / x) ) = (R, A) (note the domain of the arctan function and adjust the angle accordingly).

So if we wanted to increase the magnitude of our velocity vector by a fixed value r, then we would have:
(R + r, A) = ( (R + r) * cos(A), (R + r) * cos(A) ).
Notice that the difference between the new vector and the old vector is simply ( r * cos(A), r * sin(A) ), which is simply the components of the change in magnitude to the appropriate direction.

From this, it is clear that if H is the new magnitude of your velocity with the same direction A, then the components is simply ( H * cos(A), H * sin(A) ).

But as Tepi said, the values are automatically converted for you when you use the built-in variables.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users