PDA

View Full Version : Object Physics

coreys
11-04-2007, 01:22 AM
Well, after years of always wanting to do it, I finally have! I made physics for NPC objects, with some advice from a few people. Now, it may take some editing on your part to work for your server, but that shouldn't be too hard.

/*Set this.weight and this.width/this.height (in pixels) before
joining the class*/
function onCreated() {
this.anglex = this.angley = NULL;
this.velocity = this.dist = this.momentum = NULL;
this.distgone = this.time = this.timetotal = NULL;
this.w = this.width/16;
this.h = this.height/16;
}
/*You'll probably have your own function for when the object is hit,
hence you'll probably need your own calculations for velocity and distance.*/
function onActionHit(damage, accuracy, effects, acc, px, py) {
this.distgone = 0;
this.angle = getangle(px-this.x,py-this.y);
if (this.angle > degtorad(180))
else this.angle += degtorad(180);
this.velocity = ((damage*4.45)/this.weight)*.05;
temp.friction = this.weight/damage;
if (temp.friction>=1) this.dist = 0;
else this.dist = 1 / temp.friction;
onTimeOut();
}
function onTimeOut() {
if (this.distgone < this.dist) {
temp.x = this.x+(this.w/2); temp.y = this.y+(this.h/2);
temp.dx = temp.x+cos(this.angle)*((this.velocity/3)*4);
temp.dy = temp.y-sin(this.angle)*((this.velocity/3)*4);
if (temp.dx > temp.x) temp.dx += 2;
if (temp.dy < temp.y) temp.dy += .5;
else if (temp.dy > temp.y) temp.dy += 1.75;
if (!onwall(temp.dx, temp.y))
this.x += cos(this.angle)*this.velocity;
else
this.angle = pi - this.angle;
if (!onwall(temp.x,temp.dy))
this.y += -sin(this.angle)*this.velocity;
else
this.angle = -this.angle;
this.distgone += this.velocity;
this.velocity = (this.velocity/7)*6;
setTimer(0.05);
}
}

Enjoy :D