PDA

Click to See Complete Forum and Search --> : Math~Adding wall collision to car engine script



JPMoney
12-13-2002, 12:26 AM
Hello everyone,
I'm trying to add a wall collision script to my car game so that when my car comes in contact with the mc with instance 'wall', it can not penetrate or go through the wall. Someone gave me a script on how to do that but I do not want to use their car driving script, I just want to use their wall collision part. Here is their script:

onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
speed += 1;
}
if(Key.isDown(Key.DOWN)){
speed -= 1;
}
if(Math.abs(speed)>10){
speed *= 1;
}
if(Key.isDown(Key.LEFT)){
_rotation -=15;
}
if(Key.isDown(Key.RIGHT)){
_rotation += 15;
}
speed *= .90;

x = Math.sin(_rotation*(Math.PI/180))*speed
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;

if(!_root.wall.hitTest(_x+x,_y+y,true)){
_x += x;
_y += y;
}
else{
speed *= -0.4
}
}


***I know that the last part of that script is the part that states the wall collision part. But, when I put that part on the end of my car script, instead of having a nice clean wall collision like it does in the above script, it screws up and the car gets shot backwards glitchily. I need your help putting the wall collision part of the script on the end of my car engine script so it works. Here is my car script :



onClipEvent (load) {
h = _x;
v = _y;
hm = 0;
vm = 0;
wheelSpeed = 0;
actual_speed = 0;
ideal_hm = 0;
ideal_vm = 0;
tyreGrip = 1;
}
onClipEvent (enterFrame) {
// accelerating and braking
if (Key.isDown(Key.UP)) {
wheelSpeed += 10;
}
if (Key.isDown(Key.DOWN)) {
wheelSpeed += -5;
}
wheelSpeed *= 0.65;
ideal_hm = wheelSpeed*Math.cos(_rotation*(Math.PI/180));
ideal_vm = wheelSpeed*Math.sin(_rotation*(Math.PI/180));
hm += ((ideal_hm-hm)*tyreGrip);
vm += ((ideal_vm-vm)*tyreGrip);
h += hm;
v += vm;
// steering
if (Key.isDown(Key.LEFT)) {
_rotation -= Math.floor(actual_speed);
} else if (Key.isDown(Key.RIGHT)) {
_rotation += Math.floor(actual_speed);
}
actual_speed = Math.sqrt((hm*hm)+(vm*vm));
h = (h+800)%800;
v = (v+670)%670;
_x = h;
_y = v;
}
}


*** I know my script is kind of long but I need to use it for the kind of game I'm making and I can't just use the one that that guy (the first script) gave me.
So if you could help me I would appreciate it very much.

IN SUMMARY

I'm trying to add the following script to my car engine script (middle script I've listed) so that their is a good wall collision effect. Thank you. Here's the wall script:

speed *= .90;

x = Math.sin(_rotation*(Math.PI/180))*speed
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;

if(!_root.wall.hitTest(_x+x,_y+y,true)){
_x += x;
_y += y;
}
else{
wallSpeed *= -0.4
}
}