Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 07:36:21 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsSpeed and Acceleration help for RTS / Point and click
Pages: [1]
Print
Author Topic: Speed and Acceleration help for RTS / Point and click  (Read 2167 times)
eddieion
Guest
« on: March 09, 2014, 11:03:07 AM »

Sorry if this is in the wrong place - if so please move it or let me know and i'll close the thread.


I'm making a basic 'point and click' space ship game. I followed a tutorial on Youtube (

) to get the basic movement of the ship moving towards the cursor when clicked. I want to be able to add variables such as acceleration, top speed, friction, etc so that the ship doesn't just jump from a standstill to top speed at the click of a button, and instead accelerates and reaches top speed after a certain point. So far (before any attempted acceleration mechanics) the scripts look like this:


Create > Set mouse to cursors
Create > Execute code


desx = self.x;
desy = self.y;
moving = 0;

Step > Execute code

if moving = 1{
    mp_potential_step(desx,desy,1,true);  // 1 denotes speed
    }

Global left pressed > Execute code

moving = 1;
image_angle = point_direction(self.x,self.y,mouse_x,mouse_y);
desx = mouse_x;
desy = mouse_y;        




A few months ago I experimented with a different space game in which the ship was controlled using the directional buttons as opposed to a point and click method. I managed to get acceleration and speed working with another tutorial but so far haven't managed to implement the same sort of thing into my point and click game. The scripts look like this:




Create > Execute code

x_speed = 0
y_speed = 0
acc_speed = 0.5 //acceleration
max_speed = 3
speed_friction = 1 //1 = no friction
image_angle= 90
direction= 90

//Physics objects must call the parent object's create event
//to initialize physics variables.  Here we are calling it
//BEFORE we initialize the player's stats because obj_moveable
//also defines S_SLOPE_SLOW for itself and we want to
//overwrite the value of it with one specifically for obj_player
event_inherited();

//Here some player stats are defined (so they can be easily
//tweaked if needed).
S_GRAVITY       = 0.3;      //Accel. due to gravity (pixels/step*step)
S_RUN_ACCEL     = 0.6;      //Accel. from running on ground (pixels/step*step)
S_RUN_FRIC      = 0.6;      //Friction on the ground (pixels/step*step)
S_AIR_ACCEL     = 0.4;      //Accel. from running in the air (pixels/step*step)
S_AIR_FRIC      = 0.4;      //Friction in the air (pixels/step*step)
S_JUMP_SPEED    = -6;
S_DJUMP_SPEED   = -4;       //Double jump speed
S_MAX_H         = 3;        //Max horizontal speed
S_MAX_V         = 8;        //Max vertical speed
S_SLOPE_SLOW    = 0.7;      //Decceleration while climbing slopes

Step > Execute code

//step event
if keyboard_check(vk_left) view_angle[0] -= 3
if keyboard_check(vk_right) view_angle
  • += 3
if keyboard_check(vk_left) direction += 3
if keyboard_check(vk_right) direction -= 3

if keyboard_check(vk_up) {
y_speed += lengthdir_y(acc_speed, direction)
x_speed += lengthdir_x(acc_speed, direction)

}

if keyboard_check(vk_down) {
y_speed -= lengthdir_y(acc_speed, direction)
x_speed -= lengthdir_x(acc_speed, direction)

}

if (keyboard_check(ord('X'))) {
    instance_create(x, y, lazer)
}

x += x_speed
y += y_speed

x_speed *= speed_friction
y_speed *= speed_friction

image_angle[90] = direction[90]              


If anyone can give me any help at all I'd appreciate it immensely. I apologise if i haven't presented my code properly I'm just not sure of the best way to format it to make it clearest for you. I'm a bit of a laymen when it comes to using GML or any other code; just looking to learn by indulging in sci-fi spaceship dogfights Smiley

If it would help (and someone can suggest a good site?) I'd happily upload the game maker files for you to check it out and get a better feel for what I'm trying to achieve.
« Last Edit: March 09, 2014, 02:50:23 PM by eddieion » Logged
feminazi
Guest
« Reply #1 on: March 09, 2014, 11:55:31 PM »

Quote
    mp_potential_step(desx,desy,1,true);  // 1 denotes speed

it says it in your comment there...
change the number

do something like

create:
Code:
desx = self.x;
desy = self.y;
moving = false;
currentSpeed = 0;

step:
Code:
if (moving)
{
    if (currentSpeed < 8)
    {
        currentSpeed += 0.25;
    }
    mp_potential_step(desx, desy, currentSpeed, true);
    if (point_distance(self.x, self.y, desx, desy) < 1.0)
    {
        moving = false;
    }
}
else
{
    currentSpeed = 0;
}

global left pressed
Code:
moving = true;
image_angle = point_direction(self.x,self.y,mouse_x,mouse_y);
desx = mouse_x;
desy = mouse_y;
Logged
eddieion
Guest
« Reply #2 on: March 10, 2014, 06:44:37 AM »

That was incredibly helpful and it works almost perfectly now - Thank you very much!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic