Legacy GM Top Down Car Collisions

H

Humphrey M

Guest
Hi
I've started a simple top down racing game and have been having a bit of trouble working out collisions. Normally it should be fairly simple but I was hoping to make it realistic, such as if you were to drive into a wall on an angle the car should readjust its angle and continue driving instead of just stopping. I've just set the speed to 0 when it hits the test object for the moment.

Code:
obj_c1 -> Create Event:

spd = 0;
mxspd = 11;
key = "";
accel = 0.25;
angle = 180;
slowing = false;
slowing2 = false;

obj_c1 -> Step Event:

// Verticle control
if(keyboard_check(vk_up) && !keyboard_check(vk_down)){
if(key != "up"){
if(spd <= 0){
key = "up";
spd = 0;
slowing = false;
}
else{
if(slowing2 == false){
spd -= 0.1;
slowing = true;
}
}
}
if(key == "up"){
slowing2 = false;
move_towards_point(x + lengthdir_x(spd,angle),y + lengthdir_y(spd,angle),spd);
if(spd != mxspd){
spd += accel;
}
}
}
else{
if(key == "up"){
if(spd != 0){
if(slowing == false){
slowing2 = true;
move_towards_point(x + lengthdir_x(spd,angle),y + lengthdir_y(spd,angle),spd);
spd -= accel;
}
}
else{
slowing2 = false;
key = "";
}
}
}

if(keyboard_check(vk_down) && !keyboard_check(vk_up)){
if(key != "down"){
if(spd <= 0){
key = "down";
spd = 0;
slowing = false;
}
else{
if(slowing2 == false){
spd -= 0.1;
slowing = true;
}
}
}
if(key == "down"){
slowing2 = false;
move_towards_point(x + lengthdir_x(spd,angle),y + lengthdir_y(spd,angle),-spd);
if(spd != mxspd / 2){
spd += accel;
}
}
}
else{
if(key == "down"){
if(spd != 0){
if(slowing == false){
slowing2 = true;
move_towards_point(x + lengthdir_x(spd,angle),y + lengthdir_y(spd,angle),-spd);
spd -= accel;
}
}
else{
slowing2 = false;
key = "";
}
}
}

// Horizontal control

if(keyboard_check(vk_left)){
angle += spd / 2;
}
else if(keyboard_check(vk_right)){
angle -= spd / 2;
}

image_angle = angle - 90;



// Wrap

if(x + sprite_height / 2 <= 0){
x = room_width + sprite_height / 2;
}
else if(x - sprite_height / 2 >= room_width){
x = 0 - sprite_height / 2;
}

if(y + sprite_height / 2 <= 0){
y = room_height + sprite_height / 2;
}
else if(y - sprite_height / 2 >= room_height){
y = 0 - sprite_height / 2;
}


obj_c1 -> Collision with object1

spd = 0;


Here is a link to the project if I haven't made it clear:
https://drive.google.com/open?id=0BzZXCHC937HgT2RRXzNUanVheDA

Sorry the code is a bit messy :p
Tnx
 
P

Paolo Mazzon

Guest
If you want to make a (somewhat) realistic car simulation top down, you may want to consider going with Box2D physics instead of straight up pixel collisions.
 
Top