Windows Tank Game Help with Collision Code

K

Kaji_Ikaraseru

Guest
Ok so I am making top down tank game, and I made a nice (in my opinion) code for movement:
Code:
if keyboard_check((ord("D"))){
    image_angle -= 4;
}
if keyboard_check((ord("A"))){
    image_angle += 4;
}

if keyboard_check((ord("W"))){
    if speed < 5 {
        speed += 0.2;
    }
    direction = image_angle;
}
else{
    if speed > 0 {
        speed -= 0.1;
    }
    direction = image_angle;
}
if keyboard_check((ord("S"))){
    if speed > -5 {
        speed -= 0.2;
    }
    direction = image_angle;
}
else{
    if speed < 0 {
        speed += 0.1
    }
    direction = image_angle;
}
I am looking for a collision code (I don't know how to program so that all sides have perfect collision)
Any tips?
 

FrostyCat

Redemption Seeker
Most similar games involving 360-degree movement eschew perfect collision in favour of ease of computation. Instead of perfect rectangles or precise sprite masks, they use a static circle as the collision mask for rotating agents. That way it's impossible rotate them into anything.

A misplaced belief in numerical perfection is a common rookie tendency that wears off with experience.
 
K

Kaji_Ikaraseru

Guest
Most similar games involving 360-degree movement eschew perfect collision in favour of ease of computation. Instead of perfect rectangles or precise sprite masks, they use a static circle as the collision mask for rotating agents. That way it's impossible rotate them into anything.

A misplaced belief in numerical perfection is a common rookie tendency that wears off with experience.
Thank you for the clarification. I decided to make my collision more like Tank Trouble 2 (https://tanktrouble3.com/tank-trouble-2/). My problem is that when I hit the wall with the longer side of my tank, then rotate, the tank doesn't stop when it hits the wall, therefore making me stuck.
 

HayManMarc

Member
That's why FrostyCat suggested using a circular mask instead of a precise mask. You can change your mask settings in the sprite resource.
 
K

Kaji_Ikaraseru

Guest
That's why FrostyCat suggested using a circular mask instead of a precise mask. You can change your mask settings in the sprite resource.
I don't know if there is a way around this but since my sprite is the full 32 pixels wide, if I make a circular mask it does not cover the corners.
 

HayManMarc

Member
Leave it that way, or you can resize the canvas of the sprite so that a circle WILL cover the corners.

Otherwise, you'll have to do heavier coding to handle precise collisions from all angles, while also kicking you out of other objects when rotating.
 

Freddy Jones

Your Main Detective
Leave it that way, or you can resize the canvas of the sprite so that a circle WILL cover the corners.

Otherwise, you'll have to do heavier coding to handle precise collisions from all angles, while also kicking you out of other objects when rotating.
Or just make a new sprite that's large enough to cover the corners, set that as a circular collision mask, and then set the object's collision mask to that sprite.
 

Bingdom

Googledom
This is what you have to do for your steering ;)

Code:
old_angle = image_angle;
if keyboard_check((ord("D"))){
    image_angle -= 4;
    if place_meeting(x,y,OBJ_Wall) image_angle = old_angle;
}
if keyboard_check((ord("A"))){
    image_angle += 4;
    if place_meeting(x,y,OBJ_Wall) image_angle = old_angle;
}
So basically what happens is when your rectangular tank turns into a wall, the tank will instantly be turned back to its original angle.
 
Last edited:
K

Kaji_Ikaraseru

Guest
t
This is what you have to do for your steering ;)

Code:
old_angle = image_angle;
if keyboard_check((ord("D"))){
    image_angle -= 4;
    if place_meeting(x,y,OBJ_Wall) image_angle = old_angle;
}
if keyboard_check((ord("A"))){
    image_angle += 4;
    if place_meeting(x,y,OBJ_Wall) image_angle = old_angle;
}
So basically what happens is when your rectangular tank turns into a wall, the tank will instantly be turned back to its original angle.
Thanks :)
 
B

bojack29

Guest
Isn't it still a rectangular collision box even when you set the mask as an eliptical? I thought I tried this once setting the mask as an eliptical and drew the bounding box with a draw_rectangle. Even when I rotated the object with a centered origin, the bounding box grew / shrink.
 
Top