Thread: Car drifting
View Single Post
  #13  
Old 09-24-2006, 12:40 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
Well, you'll want to have two variables: xvelocity and yvelocity

While the car is moving, you'll increase the x/y by xvelocity and yvelocity.
When you press the up key, you'll decrease y velocity by the acceleration until it reaches the speed limit. When it reaches the speed limit, it stops increasing the velocity.
Do the same for the other keys.

PHP Code:
function onCreated() {
  
this.xvelocity = this.yvelocity = 0;
  
this.acceleration = .1;
  
this.speedlimit = 1;
}

function 
onTimeout() {
  for (
k = 0; k < 4; k++) {
    if (
keydown(k)) {
      
this.xvelocity = limit(this.xvelocity + vecx(k) * this.acceleration,-this.speedlimit,this.speedlimit);
      
this.yvelocity = limit(this.yvelocity + vecy(k) * this.acceleration,-this.speedlimit,this.speedlimit);
    }
  }
}

function 
limit(val,min,max) {
  if (
val < min) val = min;
  if (
val > max) val = max;
  return 
val;
} 
Reply With Quote