Executing a couple of movement vectors

Started by Kumpel, Tue 22/12/2015 16:25:40

Previous topic - Next topic

Kumpel

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




Khris

Use arrays:

Code: ags
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++;
  }

Kumpel

#2
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-)

Gurok

#3
Expanding on Khris' example:

Code: ags
#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.

Code: ags
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.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Cerno

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.
123  Currently working on: Sibun - Shadow of the Septemplicon

Gurok

Here's one that parses strings.

Code: ags
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.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Kumpel



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!


Crimson Wizard

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.

Mandle

Those movement vectors are political prisoners! Executing them is against international law!

FREE THE TENNIS BALL 60!!!

SMF spam blocked by CleanTalk