Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've decided that it's best to start using vectors in my games instead of the
xPos += xSpeed and yPos += ySpeed approach.

I'm posting some of the code from my basic model class which handles movement logic in my game so that someone can tell me if I am using vectors correctly. I have a position, direction, acceleration and velocity vector. Here is some code, I can post more if needed:

model.update() and rotate() function

public function update():void
{
    checkInput();
    pos.addVec(velocity);
    velocity.addVec(accel);
    velocity.multiplyScalar(friction);
    dispatchEvent(new Event(Event.CHANGE));
}

public function rotate(val:Number):void
{
    angle += val;
    direction.x = Math.cos(angle * degreesToRads);
    direction.y = Math.sin(angle * degreesToRads);
    direction.normalize();
}

and the checkInput() function

private function checkKeys():void 
{
    accel.set(0, 0);
    if(keys[Keyboard.LEFT])
    {
        rotate( -5);
    }
    if (keys[Keyboard.RIGHT])
    {
        rotate(5);
    }
    if (keys[Keyboard.UP])
    {
        accel.x = direction.x * 0.8;
        accel.y = direction.y * 0.8;
    }
    if (keys[Keyboard.DOWN])
    {
        accel.x = direction.x * 0.5;
        accel.y = direction.y * 0.5;
    }
}

Hopefully someone can tell me if I'm using vectors correctly by adding acceleration to velocity and to speed and also if I'm doing rotation the correct way or if there is a better way. Or anything anyone can suggest to improve would be great.

share|improve this question
3  
Come on guys down votes with no comment are not helpful. –  James Jan 25 '12 at 0:23
2  
There is no specific question here. –  Tetrad Jan 25 '12 at 15:31
add comment

closed as not a real question by Trevor Powell, thedaian, Tetrad Jan 26 '12 at 7:25

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

There's no "correct" way to do this. Using xPos += xSpeed and yPos += ySpeed is basically the same as pos.addVec(velocity). I suggest you use what you're comfortable with.

If movement behaves as it should, then go ahead. Some minor things you could improve, by looking at your code:

  • Events are slow. Don't dispatch them in the update loop... call methods directly or use signals.
  • You should calculate movement in a framerate independent fashion, so that your game runs at the same speed on slower computers as well. The way to do this would be to measure the elapsed time since the last frame and use this delta for movement calculation.
  • You don't need to normalize your direction after the rotation calculation. It's a unit vector already.
share|improve this answer
    
I take minor issue with your first bullet point, because it's unclear from the code shown exactly what happens as a result of "dispatchEvent" and consequently it seems unreasonable to diagnose it as "slow." –  Josh Petrie Jan 25 '12 at 16:48
    
@JoshPetrie It's true that the amount of time for event dispatching and handling might not be significant. Compared to other approaches like signals, events are a bad choice though. They are slower (here's a comparison of turbosignals, as3signals and native events) cumbersome and prone to memory leaks (see this article). –  bummzack Jan 25 '12 at 16:58
    
thanks for your help just what I wanted to know. –  user531186 Jan 25 '12 at 17:13
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.