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.
