Advertisement

programming strings (the bendy type)

Started by July 14, 2001 08:16 AM
3 comments, last by Pierspo 23 years, 7 months ago
I've programmed this string for use in a game (realistic ammo belts and stuff). The string in the program follows the mouse, the problem is that when you move it to the right the rest of the string comes to rest one pixel to the left of the cursor if you move the cusor back in line with the string the string remains directly below as it should and when moving to the left the srtring comes to rest directly below the cursor as it should. I just cant see why its happening and was hoping someone here could help. Here's the string function.
  
void do_string(void) //works except for a 1 pixel x axis anomaly to the right.

{

	int i;
	float y_vect1, x_vect1, y_vect2, x_vect2; 
	float magnitude;
	float ratio;

	s[0].point[0].x = mouse_x;
	s[0].point[0].y = mouse_y;

	for(i=1; i<50; i++)
	{
		magnitude = 0;	//magnitude is the distance between two points on the string	

		
		s[0].point[i].y += GRAVITY;
		
		x_vect1 = s[0].point[i-1].x - s[0].point[i].x;//find the x distance

		y_vect1 = s[0].point[i-1].y - s[0].point[i].y;//find y distance

		magnitude = sqrt( (x_vect1*x_vect1)+(y_vect1*y_vect1) );
				
		if(magnitude1>INIT_DISTANCE)//INIT_DISTANCE is the maximum extension of the string

		{
			ratio = INIT_DISTANCE/magnitude;//finds the ratio of the max lengt and actual lenth


			x_vect2 = x_vect1 * ratio;//multiply the actual leght by the ratio

			y_vect2 = y_vect1 * ratio; 
			
			x_vect1 = x_vect1 - x_vect2;//subtract this to find the chang in distance needed

			y_vect1 = y_vect1 - y_vect2;
			
			s[0].point[i].x += x_vect1;//adjust the coordinates of the array element

			s[0].point[i].y += y_vect1;
		}		
	}
}
  
Anyway if anyone can see whats going wrong it would be great if you could help me out Edited by - pierspo on July 14, 2001 9:20:44 AM
yeah boy
Nice effect, there!

Is s[0].point an array of integers? I tested it assuming it was, and I found that this was the problem:

s[0].point.x += x_vect1;<br><br>That was rounding x_vect1 downwards rather than to the nearest integer. I replaced it with this line:<br><br>s[0].point.x += x_vect1 + 0.5;<br><br>And it worked just fine. Strangely, if I did the same thing with the y values, it made the effect even worse.<br><br>EDIT: added those spaces around the letter "i" because an i surrounded by square brackets indicates italicised text on this board.<br><br>Edited by - Beer Hunter on July 14, 2001 12:12:01 AM
Advertisement
Unfortunately s[0].point is an array of floats so that is not the problem. I tried changing it to ints but the program just behaves really oddly, thanks for your help anyway. If its not too much trouble could you post your code with the ints so I can take a look.
yeah boy
If they''re floats then when you draw them you''re going to be casting to integers. Seeing as casting from a float to an int rounds down (I think), that would explain your problem... because it''s essentially what Beer Hunter was talking about, just at a later stage (i.e not when you assign the value, but when you blit the coordinate).

Insomnia
Insomnia
When you move the cursor right, say to x=56, then any point's x value might go:

55
55.9
55.99
55.999
55.9999
55.99999

But all of those will be rounded down to 55 when they are drawn... You need to either add 0.5 when converting to an integer, or if you're feeling bored, draw everything anti-aliased.

EDIT: To make the string act more like a real string, try changing the definition of s[0].point so that it has four float members (x, y, xv, yv), and then try this code out. I used a value of 15 for gravity and 5 for init_distance, by the way.
  void do_string(void){    int i;    float y_vect1, x_vect1, y_vect2, x_vect2;    float magnitude;    float ratio;    point[0].x = mouse_x;    point[0].y = mouse_y;    for(i=1; i<50; i++)    {        point[i].xv *= 0.9;        point[i].yv += GRAVITY;        point[i].yv *= 0.9;        point[i].x += point[i].xv;        point[i].y += point[i].yv;        magnitude = 0;        x_vect1 = point[i-1].x - point[i].x;        y_vect1 = point[i-1].y - point[i].y;        magnitude = sqrt( (x_vect1*x_vect1)+(y_vect1*y_vect1) );        if(magnitude>INIT_DISTANCE)        {            ratio = INIT_DISTANCE/magnitude;            x_vect2 = x_vect1 * ratio;            y_vect2 = y_vect1 * ratio;            x_vect1 -= x_vect2;            y_vect1 -= y_vect2;            point[i].xv += x_vect1 * 0.8;            point[i].yv += y_vect1 * 0.8;            point[i].x += x_vect1;            point[i].y += y_vect1;        }    }}  


Edited by - Beer Hunter on July 15, 2001 9:47:42 PM

This topic is closed to new replies.

Advertisement