ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: Garo on September 26, 2014, 04:03:10 PM

Title: Script problem
Post by: Garo on September 26, 2014, 04:03:10 PM
Hello there. I've been lately interested in using ENIGMA again in an attempt to make a shoot 'em up engine. There was a general-purpose script I wanted to make, but thus far I had troubles on figuring it out.

It was intended to function as follows:

I tried my hand at it, but I just couldn't figure it out very well. I would appreciate if someone else would help me on that, and told me how it works.
Thanks.

--- Garo
Title: Re: Script problem
Post by: Garo on September 26, 2014, 05:08:45 PM
Oop. Could someone move this to the proper category please?
Title: Re: Script problem
Post by: Goombert on September 26, 2014, 06:01:25 PM
Yes sir I can move it for you! You were looking for Programming Help. I would like to call your attention to the functions move_towards_point and mp_potential_step which are designed to move the object towards a point while avoiding other objects and such. Though the later of these functions is a little finicky in ENIGMA due to the difference in implementation however playing with the settings function for it does make it work. We currently have a ticket filed on it not working exactly the same as GM though.
https://github.com/enigma-dev/enigma-dev/issues/678

Additional details as to what you are looking to do would also be helpful, such as do you want to avoid other objects? Are you looking to calculate the shortest path and not just any path?
Title: Re: Script problem
Post by: Garo on September 26, 2014, 06:47:00 PM
Well, what I was looking for wasn't exactly related to pathfinding. Just moving an object from it's current point to another XY coordinate.
I can think of a way to make it move statically towards a point, but I rather wanted to aim for acceleration for smooth movements. Somewhat like you'd see in the Path functionality, except in a script form (speed).

https://www.youtube.com/watch?v=fEpzoLNugLo < Here's a video of a boss I was working on using Fraxy, I want to achieve that sort of movement with the script.
Title: Re: Script problem
Post by: Goombert on September 26, 2014, 07:18:12 PM
Hmm I am not really sure I follow you though, because I don't see why you would need a script particularly for this as both GM and ENIGMA do have functions built in for this. For instance, move_towards_point sounds like it does exactly what you want this script to do.
http://enigma-dev.org/docs/Wiki/Move_towards_point

Additional Instance and Motion functions are documented here.
http://enigma-dev.org/docs/Wiki/Instance_and_Motion_Functions

The acceleration you can control yourself using the speed variable. You could also take the distance between the two points (Pythagorean theorem; also the point_distance function) and divide it by some arbitrary number and use that as the speed, then the object will be faster when it is further away and get exponentially slower as it gets closer and closer to the point.
Title: Re: Script problem
Post by: Garo on September 26, 2014, 07:55:26 PM
That still makes me ponder on the function of the script. It does indeed use that, however I want the speed to change, let's say, like this:
Let's say the time is counted in frames. I want to move the object within 100 frames, with the following change in speed:

-Starting Point (speed = x; time = 0)
-Middle (speed = 2x; time = 50)
-End Point (speed = x; time = 100)

What I lack for the script is the proper equation for this sort of thing. Can it be done through measuring the distance as well?
Title: Re: Script problem
Post by: Goombert on September 26, 2014, 08:46:38 PM
Yes you could still do what I suggested, this is simply physics/calculus. rate of change = distance/time.

My original suggestion was this which gives us an exponential graph where velocity is the y-axis and distance is the x-axis.
Code (EDL) Select

// with a room speed of 30 steps/frames per second you will get these speeds
// distance = 5, 5/10th pixel per step * 30 steps per second = 15 pixels per second
// distance = 10, 10/10th pixel per step * 30 steps per second = 30 pixels per second
// distance = 20, 20/10th pixel per step * 30 steps per second = 60 pixels per second
var spd;
spd = distance_to_point(xx, yy) / 10;
// you could also limit the speed if you like to some arbitrary value
if (spd > 30) {
  spd = 30;
}
move_towards_point(xx, yy, spd);


So let's just spruce up what I suggested first and you'll get a function similar to what you want that will accelerate up to a constant distance from any given point at which point it will slow down. You'll need to create a new variable called velocity in your create event and initialize it to 0.
Code (EDL) Select

var dist;
dist = distance_to_point(xx, yy);
if (dist > 100) {
  // accelerate
  velocity += dist / 10;
} else {
  // we are getting close, so decelerate
  velocity -= (100 - dist) / 10;
}

// you could also limit the speed if you like to some arbitrary value
if (velocity > 30) {
  velocity = 30;
}
move_towards_point(xx, yy, spd);


If you like you can also use the built in variables with this alternative so you don't have to create a new one and you could also use the friction variable then as well.
Code (EDL) Select

var dist;
dist = distance_to_point(xx, yy);
if (dist > 100) {
  // accelerate
  speed += dist / 10;
} else {
  // we are getting close, so decelerate
  speed -= (100 - dist) / 10;
}

// you could also limit the speed if you like to some arbitrary value
if (speed > 30) {
  speed = 30;
}

direction = point_direction(x, y, xx, yy);
Title: Re: Script problem
Post by: Garo on October 19, 2014, 06:41:38 PM
I was a tad busy lately, but this weekend I decided to sit down by this again and solve it.

Turns out the word I was looking for was interpolation, and after studying a bit followed by tons of testing, I made it work! It was a shame though that I can't move an object using just the script itself, as that was my aim (it warped the object instead)

:pseudo:

Either way, There's the file below with what I've come up with. It's probably not the most efficient, could someone take a look at it?