GML Visual spaceship physics GMS2

W

will.w

Guest
background
Hi, first of all, I want to say I am new to GMS2, before using GMS2 I used construct 2 in school. I made a game similar to "SPACEWAR!" except never finished it. the game has two spaceships on opposite sides of a star, a player controls one while the second controls the other. next the spaceships have to use the sun's gravitational pull, missiles, and thrusters to defeat the other player

the chase
While trying to make my spaceship move, I found that there is no force module other than gravitational force to attempt to make my spaceship move like in construct 2. I have been trying different moving modules but none seems to work, it either acts wacky or drives like a top down car driving game

problem
Can someone please help me overcome this obstacle with using DND (If I need to use console somewhere I can live with that)

to show what I want the game's phisics and the ship's movement to work, the link to the "SPACEWAR!" game should be to the right of this smiley face :) http://www.masswerk.at/spacewar/
 

Binsk

Member
Eh... looking through the drag-and-drop functions they really don't give you much to work with. There isn't even a way (that I can find) to set your own variables. As such, we will work with what we've got and do the math ourselves.

The concept is this: You have the direction you are currently moving and you have the direction you are facing. When you apply force you want to apply it in the direction you are facing (aka., thrust). As such, when you rotate your ship you should be calling "Set Instance Rotation" which will rotate the image without rotating your actual movement.

Next, to add thrust, GameMaker only provides you three types of movement: 1) in the direction you are currently moving (which you can also change directly, and we don't want otherwise you get that "car" effect), 2) along the x-axis and 3) along the y-axis.

Because we want to use our OWN direction (the angle of the image) we will have to add force to the x and y axes and calculate how much to each is appropriate. When you want to add force you should be calling "Set Speed" twice; once for the "Horizontal" axis (aka., x-axis) and once for the "Vertical" axis (axa., y-axis). Make sure that the "Relative" option is checked and set to "true". Let's say you wanted to add a thrust of 10 units. Normally you would specify 10 directly. However, we need to modify how much goes to each axis based on the angle of our image. You can instead specify lengthdir_x(10, image_angle) and lengthdir_y(10, image_angle) in each "Set Speed" (the lengthdir_x for the horizontal and the lengthdir_y for the vertical). The first number (the 10) is how much force you want applied TOTAL for both axes. The second value image_angle is a built-in instance variable that contains the current direction of the image. When the game runs it will be replaced with the number (in degrees) that your image is facing, similar to how it handles your movement direction.

Each of these functions calculates how much of the force should go to their respective axis based on some direction. Doing the steps above will give you an "Asteroids-like" movement style.
 
Last edited:
I

icuurd12b42

Guest
^ that's a lot of text for 2 line of code...
step event:
image_angle+=(keyboard_check(ord("A")) - keyboard_check(ord("D"))) * 4;
if(keyboard_check(ord("W")))
{
motion_add(image_angle,.1);
}

>Can someone please help me overcome this obstacle with using DND
No, use code, if you can read, you can use code :)
 
W

will.w

Guest
Eh... looking through the drag-and-drop functions they really don't give you much to work with. There isn't even a way (that I can find) to set your own variables. As such, we will work with what we've got and do the math ourselves.

The concept is this: You have the direction you are currently moving and you have the direction you are facing. When you apply force you want to apply it in the direction you are facing (aka., thrust). As such, when you rotate your ship you should be calling "Set Instance Rotation" which will rotate the image without rotating your actual movement.

Next, to add thrust, GameMaker only provides you three types of movement: 1) in the direction you are currently moving (which you can also change directly, and we don't want otherwise you get that "car" effect), 2) along the x-axis and 3) along the y-axis.

Because we want to use our OWN direction (the angle of the image) we will have to add force to the x and y axes and calculate how much to each is appropriate. When you want to add force you should be calling "Set Speed" twice; once for the "Horizontal" axis (aka., x-axis) and once for the "Vertical" axis (axa., y-axis). Make sure that the "Relative" option is checked and set to "true". Let's say you wanted to add a thrust of 10 units. Normally you would specify 10 directly. However, we need to modify how much goes to each axis based on the angle of our image. You can instead specify lengthdir_x(10, image_angle) and lengthdir_y(10, image_angle) in each "Set Speed" (the lengthdir_x for the horizontal and the lengthdir_y for the vertical). The first number (the 10) is how much force you want applied TOTAL for both axes. The second value image_angle is a built-in instance variable that contains the current direction of the image. When the game runs it will be replaced with the number (in degrees) that your image is facing, similar to how it handles your movement direction.

Each of these functions calculates how much of the force should go to their respective axis based on some direction. Doing the steps above will give you an "Asteroids-like" movement style.
It worked, thank you
 
Top