Thread: Object Physics
View Single Post
  #1  
Old 11-04-2007, 01:22 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Object Physics

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.
PHP Code:
/*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))
    
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
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts

Last edited by coreys; 11-04-2007 at 01:33 AM..
Reply With Quote