Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Kumpel on Tue 22/12/2015 16:25:40

Title: Executing a couple of movement vectors
Post by: Kumpel on Tue 22/12/2015 16:25:40
Hello,

I want to make a table tennis ball move a certain ballistic way and wrote down a list of vectors. How can I put this heap in a format like "(vecX,vecY)" in one line and let the programm execute every vector as oTTBall.Move(moveX,moveY...) without writing every moveX & moveY down one after one another?

I hope you get what I mean :D
Cheers Kumpel



Title: Re: Executing a couple of movement vectors
Post by: Khris on Tue 22/12/2015 20:26:57
Use arrays:

int x[20], y[20];

  // game_start
  x[0] = 5; y[0] = 7;
  x[1] = 12; y[1] = 13;
  ..

  // moving the ball
  int i = 0;
  while (i < 20) {
    oTTBall.Move(x[i], y[i], ...);
    i++;
  }
Title: Re: Executing a couple of movement vectors
Post by: Kumpel on Tue 22/12/2015 23:46:45
Yeah I thought about that. But it's a huge amount of vectors (at least 60). Maybe you know a way of putting them in one (x1,y1,x2,y2,...) or two (for x and y) lists. Maybe with a string to int conversion. So each char of a sequence gets checked, converted, used in the function and erased until the sequence is clear. (But how to handle negative ints then?)

If not, the x/y[1]= to x/y[60] writing job is cool... 8-)
Title: Re: Executing a couple of movement vectors
Post by: Gurok on Wed 23/12/2015 00:00:06
Expanding on Khris' example:

#define MAXVECTOR  80

int x[MAXVECTOR], y[MAXVECTOR], v;

function AddVector(xx, yy)
{
  x[v] = xx;
  y[v] = yy;
  v++;
}
     
  // game_start
  AddVector(5, 7); AddVector(12, 13); ...

  // moving the ball
  int i = 0;
  while (i < v) {
    oTTBall.Move(x[i], y[i], ...);
    i++;
  }


I don't normally recommend it, but if you're coming from a maths background, calling the function something short (e.g. vv) might make it easier to visualise on one line. Regardless, there are text editors with find/replace tools that could do the job of changing (5, 7) (12, 13) -> AddVector(5, 7); AddVector(12, 13);. There's not really much more you can do (outside of string parsing) to make it any easier. Hope this helps.

EDIT: Also, you might simplify this whole thing by just making a wrapper for the Move function on the ball, e.g.

function v(x, y)
{
  oTTBall.Move(x, y, ...);
}

  // moving the ball
  v(5, 7); v(12, 13); ...


Depending on whether you actually need to separate storing the vectors and using them.
Title: Re: Executing a couple of movement vectors
Post by: Cerno on Wed 23/12/2015 03:26:00
Since I just made my peace with this technique, why not define the vectors in a separate script?
They'd be out of your hair and you can give each vector its separate line (as it should be) without having to skip a huge chunk of code every time you look for something.

An additional advantage would be that you can write a code generator script in python that writes your .asc file containing the vector stuff.
Handy if you ever need to change anything and don't want to recompute all the vectors manually. Or if you need the setup more than once.
Title: Re: Executing a couple of movement vectors
Post by: Gurok on Wed 23/12/2015 05:58:23
Here's one that parses strings.

int Vector_X[ ], Vector_Y[ ], Vector_Capacity = -1, Vector_Count = 0;

int[ ] ExpandIntArray(int source[ ], int from, int to)
{
int result[ ];

result = new int[to];
if(from > to)
from = to;
while(from > 0)
{
from--;
result[from] = source[from];
}

return(result);
}

int NextIndex(String source, int start, String characterSet, bool invert)
{
int length;
int next;

length = source.Length;
next = -1;
while(start < length)
if((characterSet.IndexOf(String.Format("%c", source.Chars[start])) > -1) != invert)
{
next = start;
start = length;
}
else
start++;

return(next);
}

int LoadVector(String source, int start)
{
int xStart;
int xStop;
int yStart;
int yStop;
String xValue;
String yValue;
int result;
String numeric;
String nonNumeric;

numeric = "0123456789-";
nonNumeric = "0123456789";
xStart = NextIndex(source, start, numeric, false);
if(xStart > -1)
{
xStop = NextIndex(source, xStart + 1, nonNumeric, true);
if(xStop > -1)
{
xValue = source.Substring(xStart, xStop - xStart);
yStart = NextIndex(source, xStop + 1, numeric, false);
if(yStart > -1)
{
yStop = NextIndex(source, yStart + 1, nonNumeric, true);
if(yStop > -1)
{
yValue = source.Substring(yStart, yStop - yStart);
result = yStop + 1;
}
else
{
yValue = source.Substring(yStart, source.Length - yStart);
result = -2; // Should be source.Length, but if you're not using it anywhere else, this is more efficient
}
if(Vector_Count == Vector_Capacity)
{
Vector_Capacity = Vector_Capacity * 2;
Vector_X = ExpandIntArray(Vector_X, Vector_Count, Vector_Capacity);
Vector_Y = ExpandIntArray(Vector_Y, Vector_Count, Vector_Capacity);
}
Vector_X[Vector_Count] = xValue.AsInt;
Vector_Y[Vector_Count] = yValue.AsInt;
Vector_Count++;
}
else
result = -1;
}
else
result = -1;
}
else
result = -1;

return(result);
}

int LoadVectorList(String source)
{
int index;

if(Vector_Capacity == -1)
{
Vector_Capacity = 1;
Vector_X = new int[Vector_Capacity];
Vector_Y = new int[Vector_Capacity];
}
Vector_Count = 0;
while(index > -1)
index = LoadVector(source, index);

return(Vector_Count);
}

void MoveAlongVectorList(Object *target)
{
int index;

while(index < Vector_Count)
{
//Display("%d/%d: %d,%d", index + 1, Vector_Count, Vector_X[index], Vector_Y[index]);
target.Move(Vector_X[index], Vector_Y[index], 3);
index++;
}

return;
}

void room_AfterFadeIn()
{
String myList1 = "(33, 4) (5, -6) (7, 8)";

LoadVectorList(myList1);
MoveAlongVectorList(oTTBall);

return;
}


The parser is fairly tolerant. You should be able to specify x/y values with any delimiter as long as they're interlaced. (1, 2) (3, 4) will work, but 1, 2, 3, 4 is also fine.
Title: Re: Executing a couple of movement vectors
Post by: Kumpel on Wed 23/12/2015 11:10:40
(http://www.intro.de/ckfinder/userfiles/images/be49e_orig-boy_that_escalated_quickly.jpg)

I didn't think it would be that complex ^^ Its only a 5 to 10 second cutscene (laugh) (for now)

Thanks Gurok. I'll try that!

Title: Re: Executing a couple of movement vectors
Post by: Crimson Wizard on Wed 23/12/2015 14:07:38
There is also monkey's Stack Module that does about same thing:
http://www.adventuregamestudio.co.uk/forums/index.php?topic=37232.0

E: I just realized latest version requires AGS 3.4... so it might not be applicable.
Title: Re: Executing a couple of movement vectors
Post by: Mandle on Thu 24/12/2015 01:43:52
Those movement vectors are political prisoners! Executing them is against international law!

FREE THE TENNIS BALL 60!!!