Game Development Community

Determining movement direction with X/Y?

by Rachel P · in Torque Game Builder · 10/07/2009 (8:01 pm) · 3 replies

Hey guys, I hate to have to bother you all for some help, but I've been working on this the past few days with no luck.

Movement of the player is determined by X/Y positions, not velocity, so how can I convert a code like this so it works properly?
Is there a way to compare the current position to the previous?



function playerClass::setCurrentAnimation(%this)
{
%xVelocity = %this.getLinearVelocityX();
%yVelocity = %this.getLinearVelocityY();

if (%xVelocity > 0)
{
%this.setFlip(false, false);
}
else if (%xVelocity < 0)
{
%this.setFlip(true, false);
}
}


Thanks much for any help! :)

#1
10/07/2009 (8:20 pm)
I have not tried this code, but I guess you could do something like this:

function playerClass::setCurrentAnimation(%this) {

  %xDelta = getWord($LastPosition,0) - %this.getPositionX();
  %yDelta = getWord($LastPosition,1) - %this.getPositionY();
  $LastPosition = %this.getPosition();

  if (%xDelta > 0) {
    %this.setFlip(false, false);
  } else

  if (%xDelta < 0) {
    %this.setFlip(true, false);
  }

}

If you don't need Delta Y you can remove line 4.
You have to test if you sould do Delta - Last or Last - Delta depending on the cordinate system (I never remember)... ;)

Good luck!

#2
10/07/2009 (10:29 pm)
Holy crap, that did it! Thanks so much!! :]
I really appreciate it. I'm still kinda new to Torque so I'm trying to learn the ropes and stuff, but that was just something that really had me stuck. And all it took was basically subtracting the X/Y value from... itself? Huh, that's interesting. I tried similar stuff to that, but never got to that exactly, obviously. :P

Thanks again. ;)
#3
10/08/2009 (9:14 am)
Glad I could help even if I only have been evaluating Torque for about 4 weeks... ;)