• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Tank Movement Question!

C

CydoEntis

Guest
I am currently making a tank based game and was curious how can I make the tank move back and forth based on the angle the tank is facing, and how can I have the gun move independently? Like an actual working tank?

My code looks like this
Code:
//Tank Movement Variables
var up = (keyboard_check(ord("W")));
var down = (keyboard_check(ord("S")));
var left = (keyboard_check(ord("A")));
var right = (keyboard_check(ord("D")));

// Tank Movement Code

if (up) {y -= 4; }      // Move the tank Up

if (down) {y += 4; }    // Move the tank Down

if (left) {image_angle += 2}    // Move tank at an angle left

if (right) {image_angle -= 2}   // Move tank at an angle right
Here are some things I have tried.


Code:
// Tank Movement Code

if (up) {direction -= 4; }      // Move the tank Up

if (down) {direction += 4; }    // Move the tank Down

if (left) {image_angle += 2}    // Move the tank Left

if (right) {image_angle -= 2}   // Move the tank Right
 
Last edited by a moderator:

icuurd12b42

TMC Founder
GMC Elder
motion_set, use image_angle for directions... or use
x+=lenghtdir_x(4,image_angle);
y+=lenghtdir_y(4,image_angle);


var up = (keyboard_check(ord("W")));
var down = (keyboard_check(ord("S")));
can be simplified with
move = (keyboard_check(ord("W")) - (keyboard_check(ord("S")); // -1,0,1

x+=lenghtdir_x(4*move,image_angle);
y+=lenghtdir_y(4*move,image_angle);


var left = (keyboard_check(ord("A")));
var right = (keyboard_check(ord("D")));
and the image angle stuff can be a single linner

image_angle+= (keyboard_check(ord("D")) - keyboard_check(ord("A"))) * 2;
 
C

CydoEntis

Guest
motion_set, use image_angle for directions... or use
x+=lenghtdir_x(4,image_angle);
y+=lenghtdir_y(4,image_angle);


var up = (keyboard_check(ord("W")));
var down = (keyboard_check(ord("S")));
can be simplified with
move = (keyboard_check(ord("W")) - (keyboard_check(ord("S")); // -1,0,1

x+=lenghtdir_x(4*move,image_angle);
y+=lenghtdir_y(4*move,image_angle);


var left = (keyboard_check(ord("A")));
var right = (keyboard_check(ord("D")));
and the image angle stuff can be a single linner

image_angle+= (keyboard_check(ord("D")) - keyboard_check(ord("A"))) * 2;
I tried what you said and simplified the code but the tank doesn't move the correct way. Instead it left to right instead of up and down.
 
M

Maximus

Guest
I tried what you said and simplified the code but the tank doesn't move the correct way. Instead it left to right instead of up and down.
Make sure the tank sprite is facing right in the sprite editor (this is direction 0)
 
C

CydoEntis

Guest
Thank You! Now is there a reason why right is turning slower then left?

*EDIT* Nvm I actually fixed this myself with some number changing :D!

Now is it possible to have the gun of the tank move at its own independent angles? Like say where the mouse is pointing?
 
A

Aura

Guest
Yes. Draw the gun sprite at tank position facing the mouse.

Code:
draw_self();
draw_sprite_ext(spr_Gun, 0, x, y, image_xscale, image_yscale, point_direction(x, y, mouse_x, mouse_y), image_blend, image_alpha);
 
C

CydoEntis

Guest
Yes. Draw the gun sprite at tank position facing the mouse.

Code:
draw_self();
draw_sprite_ext(spr_Gun, 0, x, y, image_xscale, image_yscale, point_direction(x, y, mouse_x, mouse_y), image_blend, image_alpha);
Alright thanks! and then alls I would do is write code on the gun object to move based on the position of the mouses X and Y coordinates right? and spawn the bullet out of the gun instead of the tank itself?
 

Roderick

Member
Yes. Draw the gun sprite at tank position facing the mouse.

Code:
draw_self();
draw_sprite_ext(spr_Gun, 0, x, y, image_xscale, image_yscale, point_direction(x, y, mouse_x, mouse_y), image_blend, image_alpha);
Oh, good idea! When I did the same thing, I created the turret as a separate object that moved along with the tank, but that meant that every tank was unnecessarily two objects.
 
A

Aura

Guest
You don't need to move the gun to the mouse position. The code for drawing the gun sprite already does that. You don't need to use multiple objects either. Use the code I gave you and only the tank object would suffice. For creating bullet instances, you could use the same.
 
C

CydoEntis

Guest
When I try this the gun sprite doesn't show up when I run the game.

Code:
draw_self();
draw_sprite_ext(spr_gun, 0, obj_greenTank.x, obj_greenTank.y, image_xscale, image_yscale, point_direction(x, y, mouse_x, mouse_y), image_blend, image_alpha); //Create gun and move it the direction of mouse
draw_sprite(spr_tracks, 0, obj_greenTank.x, obj_greenTank.y); // Draw Tank Treads under the tank *Remember to make them dissapear after "X" Seconds.


var up = (keyboard_check(ord("W")));           
var down = (keyboard_check(ord("S")));             // Movement Variables for Up and Down
var move = (up) - (down);

x += lengthdir_x (2 * move, image_angle);       //Move tank based on the direction the tank is facing
y += lengthdir_y (2 * move, image_angle);       //Move tank based on the direction the tank is facing


var left = (keyboard_check(ord("A")));          //Movement Variables for Left and RIght
var right = (keyboard_check(ord("D")));

image_angle += (left) - (right) * 1;            ////Move tanks angle left or right
Sorry for all the probably super beginner questions. I'm slowly trying to teach myself how to program.
 
Last edited by a moderator:
A

Aura

Guest
All the drawing is supposed to be done in the Draw event, not the Step event. Put that code in the Draw event instead.
 
C

CydoEntis

Guest
Ah! I totally knew that yeah... quick question if I wanted the tank treads to spawn only when the tank is moving in any given direction then delete them after so many frames. Do I use create to make it then set an alarm to destroy it after X amount of frames? or do I still use the draw event to draw them to the object?
 

Changgi

Member
Ah! I totally knew that yeah... quick question if I wanted the tank treads to spawn only when the tank is moving in any given direction then delete them after so many frames. Do I use create to make it then set an alarm to destroy it after X amount of frames? or do I still use the draw event to draw them to the object?
I'd consider myself a beginner in GML but I'd use the alarm thing. That or make the thread marks (assuming you're talking about the trails) then I'd also make an animation where they fade to transparency and have them destroyed at animation end.
 
C

CydoEntis

Guest
I will try it with an alarm since the drawing it to the screen didnt work at all haha. Btw is there a way to control the speed at which the gun will rotate? I want it to go to the mouse but its WAY to fast. I want it to like have to catch up to the mouse pointer if that makes sense, soo like the actual tank gunner had to wait and position the gun before firing.
 
A

Aura

Guest
Create:
Code:
angle = 0;
Draw:
Code:
draw_self();
var new_dir = point_direction(x, y, mouse_x, mouse_y);
var diff = angle_difference(angle, new_dir);
angle -= min(10 * sign(diff), abs(diff));
draw_sprite_ext(spr_gun, 0, x, y, image_xscale, image_yscale, angle, image_blend, image_alpha);
Replace 10 with the maximum rotating speed.
 
Top