View Full Version : Moving a Character Based on Rotation [renamed]
gravy
February 12th, 2005, 07:46 PM
I've been trying to figure this out: Say you have your character MC and you move him forward and backward with the arrow keys. By pressing left and right you rotate your MC left and right but as the MC is rotated Up and Down now move him forward and backward depending on the direction the MC is facing, like a car except not really. If possible: could you give an elaborate description on how the code works. If not, then I can just mess around with it.
brainy
February 12th, 2005, 08:49 PM
Well, simple trigonometry using the cos/sin functions give the solution. Basically, it's something like this:
function movePlayer(mc,step) {
var angle=mc._rotation/180*Math.PI;
mc._x+=Math.cos(angle)*step;
mc._y+=Math.sin(angle)*step;
}
There are plenty tutorials explaining how cos/sin provide you with the right x/y, so I won't bother elaborating too much on it. If you remember some basic trig you should be able to see for yourself. Basically, you have a right triangle, for which you have one of the corner angles, and the hypotenuse. Trigonometry let's you calculate the other two sides of the triangle, as I've done in the code.
Basically, to use the code, simple call movePlayer with the clip and the step you wish (a negative step will move the player backwards).
eiefai
February 13th, 2005, 12:22 AM
take a look at this, maybe it is what you want.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.