• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Applying two forces to an object relative to its direction

AnalogF6

Member
EDIT: Nevermind, I've fixed it!

Here's the bit I changed:

Code:
lefttread_x = x
lefttread_y = y-75
righttread_x = x
righttread_y = y+75

if keyboard_check(ord('D')) phy_rotation += 1
if keyboard_check(ord('A')) phy_rotation -= 1

var ltx,rtx,lty,rty;

ltx = lengthdir_x(lefttread_x,phy_rotation)
lty = lengthdir_y(lefttread_y,phy_rotation)
rtx = lengthdir_x(righttread_x,phy_rotation)
rty = lengthdir_y(righttread_y,phy_rotation)
was changed to

Code:
lefttread_x = 0
lefttread_y = -75
righttread_x = 0
righttread_y = 75





if keyboard_check(ord('D')) phy_rotation += 1
if keyboard_check(ord('A')) phy_rotation -= 1


var ltx,rtx,lty,rty;

ltx = x+lengthdir_x(lefttread_x,phy_rotation)
lty = y+lengthdir_y(lefttread_y,phy_rotation)
rtx = x+lengthdir_x(righttread_x,phy_rotation)
rty = y+lengthdir_y(righttread_y,phy_rotation)
Here's my original post:

I'm learning physics and trying to create a rough simulation of tracked vehicle movement, and keep getting strange results

In theory, I'm trying to apply a separate force to both sides of the tank, where the treads are, with the goal of applying force to just the right tread to turn left, the left tread to turn right, and both treads to go forwards.

However, even though it seems like the force is being applied at the right locations (possibly), it seems to be pushing it constantly down and to the right, regardless of where I'm trying to get it to apply the force.

In essence, my code is checking if W is pressed, calculating the acceleration force, and then applying it from the back end of the object relative to direction. At least, that is what I'm trying to do. Here is an image to illustrate this:

[image removed due to spamflagging]


My Create and Step events are below:

Code:
//CREATE EVENT
accel = 0
accel_rate = 5
max_accel = 400
push_force = 0
max_force = 20000
Code:
//STEP EVENT


lefttread_x = x
lefttread_y = y-75
righttread_x = x
righttread_y = y+75

if keyboard_check(ord('D')) phy_rotation += 1
if keyboard_check(ord('A')) phy_rotation -= 1

var ltx,rtx,lty,rty;

ltx = lengthdir_x(lefttread_x,phy_rotation)
lty = lengthdir_y(lefttread_y,phy_rotation)
rtx = lengthdir_x(righttread_x,phy_rotation)
rty = lengthdir_y(righttread_y,phy_rotation)

if keyboard_check(vk_up) max_force += 10
if keyboard_check(vk_down) max_force -= 10

if keyboard_check(ord('W'))
{
    if accel < max_accel
        {accel += accel_rate}

    push_force += accel

}
else
if accel > 0
    {
        accel -= accel_rate
        push_force -= accel*2
    }
 
var px,py;

px = lengthdir_x(300,-phy_rotation) + push_force
py = lengthdir_y(300,-phy_rotation) + push_force

if accel > 0
{
    physics_apply_force(ltx,lty,px,py)
    physics_apply_force(rtx,rty,px,py)
 
}
 
accel = clamp(accel,0,max_accel)
push_force = clamp(push_force,0,max_force)

Any help the kind folks here at the Gamemaker Community could offer would be much appreciated.
 
Last edited:

AnalogF6

Member
Actually, already fixed it! I just needed to remove the x+ and y+ from the tread positions (so that x is 0 on both and y is + and - y for right and left respectively) and then add the x and y positions to the lengthdir_x and _y calculations instead.
 
Top