Monday, March 7, 2011

function physics(object){
    if (object._xvel == undefined){
        object._xvel = 10;
    }
    if (object._yvel == undefined){
        object._yvel = 0;
    }
    if (object._topspeed == undefined){
        object._topspeed = 10;
    }
    if (object._xres == undefined){
        object._xres = 0.1;
    }
    if (object._yres == undefined){
        object._yres = 0.1;
    }
    if (object._xacc == undefined){
        object._xacc = 0;
    }
    if (object._yacc == undefined){
        object._yacc = 0;
    }
    object._yvel += object._yacc*object._xres*object._topspeed - object._yvel * object._xres;
    object._xvel += object._xacc*object._yres*object._topspeed - object._xvel * object._yres;
    object._x += object._xvel;
    object._y += object._yvel;
}
function carPhysics(object){
    object._xacc = Math.cos(object._rotation/180*Math.PI);
    object._yacc = Math.sin(object._rotation/180*Math.PI);
}
onEnterFrame = function(){
    physics(_root.car);
    carPhysics(_root.car);
    _root.car._rotation = Math.atan2(_root.car._y-_root._ymouse;
}

1 feedbacks:

Alan said...

function physics(object){
if (object._xvel == undefined){
object._xvel = 10;
}
if (object._yvel == undefined){
object._yvel = 0;
}
if (object._topspeed == undefined){
object._topspeed = 10;
}
if (object._xres == undefined){
object._xres = 0.05;
}
if (object._yres == undefined){
object._yres = 0.05;
}
if (object._xacc == undefined){
object._xacc = 0;
}
if (object._yacc == undefined){
object._yacc = 0;
}
object._yvel += object._yacc*object._xres*object._topspeed - object._yvel * object._xres;
object._xvel += object._xacc*object._yres*object._topspeed - object._xvel * object._yres;
object._x += object._xvel;
object._y += object._yvel;
}
function carPhysics(object){
if(object._gas == undefined){
object._gas = 0;
}
object._xacc = object._gas*Math.cos(object._rotation/180*Math.PI);
object._yacc = object._gas*Math.sin(object._rotation/180*Math.PI)+1;
}
onEnterFrame = function(){
physics(_root.car);
carPhysics(_root.car);
_root.car._gas =(Math.pow((Math.pow(_root.car._y-_root._ymouse,2)+Math.pow(_root.car._x-_root._xmouse,2)),0.5))/50;
trace(Math.pow((Math.pow(_root.car._y-_root._ymouse,2)+Math.pow(_root.car._x-_root._xmouse,2)),0.5));
_root.car._rotation = 180+180*Math.atan2(_root.car._y-_root._ymouse,_root.car._x-_root._xmouse)/Math.PI;
}