View Single Post
  #1  
Old 06-23-2012, 01:47 AM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Collision detection advice?

I'm working on a car script, and I'm having a lot of trouble figuring out how to stop the car from driving over a wall, and also being able to turn the car away from the wall and drive off again without the collision detection catching it and stopping it from moving.

Does anybody have any ideas about how to do this?

PHP Code:
//#CLIENTSIDE

const CAR_MAX 2;
const 
CAR_FRI 0.1;
const 
CAR_ACC 1.2;

function 
onCreated() {
  
this.angle 0;
  
client.incar false;
  
onTimeout();
}

function 
onPlayerChats() {
  if (
player.chat == "car") {
    
client.incar = !client.incar;
    
player.chat "Car:" SPC client.incar;
  }
}

function 
onTimeout() {
  if (
client.incar) {
    
freezeplayer(0.05);
    if (
keydown(0)) {
      
this.speed min(CAR_MAXmax(0.1CAR_ACC this.speed));
    } else {
      
this.speed *= (CAR_FRI);
    }
    if (
keydown(1)) this.angle -= 0.1;
    if (
keydown(3)) this.angle += 0.1;
    
    if (
client.stuck == true) {
      
this.angle += 0.2;
    }
    if (
player.dir == || player.dir == 2) {
      
temp.cpos = {player.0.5player.1};
      if (
onwall(cpos[0] - 2cpos[1] - 1) || onwall(cpos[0] + 4cpos[1] - 1) || onwall(cpos[0] + 4cpos[1] + 2) || onwall(cpos[0] - 2cpos[1] + 2)) {
        
//Stop from driving onto wall
      
} else {
        
player.-= sin(this.angle) * this.speed;
        
player.+= cos(this.angle) * this.speed;
      }
    } elseif (
player.dir == || player.dir == 3) {
      
temp.cpos = {player.0.5player.1};
      if (
onwall(cpos[0] - 3cpos[1] - 1.5) || onwall(cpos[0] + 6cpos[1] - 1.5) || onwall(cpos[0] + 6cpos[1] + 3.5) || onwall(cpos[0] - 2cpos[1] + 3.5)) {
        
//Stop from driving onto wall
      
} else {
        
player.-= sin(this.angle) * this.speed;
        
player.+= cos(this.angle) * this.speed;
      }
    }
    
player.dir getdir(-sin(this.angle), cos(this.angle));
  }
  
setTimer(0.05);

(Pardon the messy code. I'm yet to clean it up since I'm still working on it)

Also, is there a simpler method of checking for walls rather than checking for walls by calling onwall() a gazillion times?
Reply With Quote