flying leaf ...

Posted By: pegamode

flying leaf ... - 11/12/09 13:22

Hi,

I'd like to add some flying leaves to my outdoor level.

Has someone an idea how to do this best?

I have a script that calculates a random wind force and direction. Now I'd like to move the leaves with those forces.
If the wind is still the leaves should fall to the ground and start flying if the wind blows again. The leaves shouldn't be able to fly through models.

Do I have to use the physics engine or is there a better way that saves fps?

Regards,
Pegamode.
Posted By: pegamode

Re: flying leaf ... - 11/16/09 11:10

I have a code that randomly generates wind and stores it into a variable that holds the wind strength (w_speed_current) and a vector for the wind direction (d_direction_current).

Then I wrote this little test-script:

Code:
action leaf() {	
	my.flags |= FLAG2;
	var accUp = 0;	
	var accDown = -0.5;
	while(me) {					
		if (w_speed_current > 0.3) {
			accUp = w_speed_current * 2;
			my.roll += 2;
			my.tilt += 5;
		} else {
			if (w_speed_current > 0) {
				my.roll += 2;
				my.tilt += 5;
			} else {
				my.tilt = 90;
			}
			accUp = 0;			
		}
		vec_to_angle(my.pan, d_direction_current);				
		accDown = -0.5 - random(1);		
		c_move(me, vector((d_direction_current.x+random(2))*w_speed_current*0.5, (d_direction_current.y+random(2))*w_speed_current*0.5, accUp),vector(0,0,accDown), IGNORE_PASSABLE | IGNORE_PASSENTS | GLIDE);
		wait(1);
	}	
}



Has someone an idea or hints how to make it look a bit more realistic?

Regards,
Pegamode.
Posted By: Germanunkol

Re: flying leaf ... - 11/16/09 17:18

Leaves always turn a lot. Look for reference videos maybe?
I find this one's good, but it lacks the wind:
http://www.youtube.com/watch?v=-NYYWp7Pa7s

another one where they turn and twist a lot:
http://www.youtube.com/watch?v=DLr1oWjIC44

I think if wind blows, they often fly back up a little (while being blown away) before tilting to make a looping and then come down at a steeper angle.
Posted By: pegamode

Re: flying leaf ... - 11/17/09 07:47

Thanks for the links ... I'll take a look at them.
Posted By: FoxHound

Re: flying leaf ... - 11/18/09 06:59

Just have the leaves rotate and base how fast off of how fast the wind is. Without looking at your script to much it looks pretty good and you should be able to pull this off.
Posted By: pegamode

Re: flying leaf ... - 11/18/09 09:03

I think there's one part I have to improve a lot ...

When the wind is still, but the leaf is in the air it has to fall down slowly from the sky ... but a leaf doesn't fall down in a straight line. I think I have to add some sinus / cosinus stuff ... hints are welcome :-)
Posted By: FoxHound

Re: flying leaf ... - 11/18/09 22:13

way to complicated. When the wind stops, and wind doesn't really stop it slows own to a still, just have the leave lose it's Z and go toward the earth in the same direction it was going. throw in some random left and right and you should do fine.
Posted By: pegamode

Re: flying leaf ... - 11/19/09 21:41

This code works quite good for me:

Code:
action leaf() {	
	my.flags |= FLAG2;
	my.push = -100;
	var accUp = 0;	
	var accDown = -0.5;
	var distDown = 0;
	trace_mode = IGNORE_ME + IGNORE_PASSABLE + IGNORE_PASSENTS;
	
	VECTOR vDirection;
	VECTOR vDistance;
	VECTOR vDifference;
	
	ANGLE targetPan;
	
	vec_set(vDirection, vector(0,0,0));
	vec_set(vDistance, vector(0,0,0));
	vec_set(vDifference, vector(0,0,0));
	
	while(me) {					
		
		vec_diff(vDifference, vDirection, d_direction_current);
		vec_accelerate(vDistance, vDirection, vDifference, 2.5);
		
		distDown = c_trace(my.x,vector(my.x,my.y,my.z-1000),trace_mode);									
		
		if (w_speed_current > 0) {
			if (w_speed_current > 0.5) {
				accUp = w_speed_current * 2 * time_step;				
				my.roll += 5 * time_step;
				my.tilt += 8 * time_step;				
			} else {							
				my.roll += w_speed_current * 10 * time_step;
				my.tilt += w_speed_current * 10 * time_step;							
				accUp = 0;			
			}	
		} else {
			if (my.tilt < 0) {
				if (0 - my.tilt - 5 > 5) {
					my.tilt += 5 * time_step;
				} else {
					my.tilt = 0;
				}
			} else {
				if (0 - my.tilt + 5 < 5) {
					my.tilt -= 5 * time_step;
				} else {
					my.tilt = 0;
				}
			}			
			if (my.roll < 90) {
				if (80 - my.roll - 5 > 5) {
					my.roll += 5 * time_step;
				} else {
					my.roll = 0;
				}
			} else {
				if (90 - my.roll + 5 < 5) {
					my.roll -= 5 * time_step;
				} else {
					my.roll = 0;
				}
			}
		}		
		
		vec_to_angle(targetPan, d_direction_current);				
		if (my.pan < targetPan.pan) {
			if (targetPan.pan - my.pan > 5) {
				my.pan += 5 * time_step;
			} else {
				my.pan = targetPan.pan;
			}
		} else {
			if (targetPan.pan - my.pan < 5) {
				my.pan -= 5 * time_step;
			} else {
				my.pan = targetPan.pan;
			}
		}
		accDown = accUp - 0.2 - random(0.5) * time_step;		
		if (distDown < 0.4) {
			accDown = 0;
		}		
		
		c_move(me, vDistance, vector(0,0,accDown), IGNORE_PASSABLE | IGNORE_PASSENTS | GLIDE);
		wait(1);
	}	
}



I'll refactor this later on.
Posted By: testDummy

Re: flying leaf ... - 11/20/09 07:00

Quoting pegamode.
Quote:
This code works quite good for me:

For how many leaves?
Posted By: pegamode

Re: flying leaf ... - 11/20/09 08:05

Currently I'm using it only for 3 leaves at a time, but I think you can use it for quite a lot of leaves as it doesn't cost that much.

The only slow function is the c_trace, but if it should be too slow for plenty of leaves you could take it out and do the ground detection slightly different.

If you use this code you will have to adjust the parameters to fit into you game as the code stands and falls with the values in w_speed_current.

Regards,
Pegamode.
© 2024 lite-C Forums