summaryrefslogtreecommitdiff
path: root/movement.c
blob: 24c8b69038429aaa6496f5ed6b1c89cec7c6478b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "snowglobe-internal.h"
#include <math.h>
#include <float.h>


void
SnowflakeTransform (snowflakeRec * snow)
{

	glTranslatef (snow->y, snow->z, snow->x);
	glRotatef (-snow->psi, 0.0, 1.0, 0.0);
	glRotatef (snow->theta, 1.0, 0.0, 0.0);
}

void newSnowflakePosition(SnowglobeScreen *as, int i) {
	int sector = NRAND(hSize);
	float ang = randf(2*PI/q)-PI/q;
	float r = (radius-0.01*as->snow[i].size/2);
	float factor = sinf(PI*(0.5-1/q))/sinf(PI*(0.5-1/q)+fabsf(ang));
	ang += (0.5+((float) sector))*2*PI/q;
	ang = fmodf(ang,2*PI);
	
	float d = randf(r)*factor;
	as->snow[i].x = d*cosf(ang);
	as->snow[i].y = d*sinf(ang);
	as->snow[i].z = 0.5;
}
void
SnowflakeDrift (SnowglobeScreen *as, int index)
{
	snowflakeRec * snow = &(as->snow[index]);
	
	float speed = snow->speed*speedFactor;
	speed/=1000;
	float x = snow->x;
	float y = snow->y;
	float z = snow->z;
	
	z-=speed;
	x+=2*(randf(2*speed)-speed);
	y+=2*(randf(2*speed)-speed);
	
	float bottom = (renderGround ? getHeight(as->ground, x, y) : -0.5);
	
	if (z<bottom) { 
		z = 0.5; newSnowflakePosition(as, index);
	}

	float ang = atan2(y,x);
	
	int i;
	for (i=0; i<hSize; i++) {
		float cosAng = cosf(fmodf(2*i*PI/q-ang, 2*PI));
		if (cosAng<=0) continue;
		
		float d = (distance-0.01*snow->size/2)/cosAng;
		float r = hypot(x,y);
		if (r>d) {
			x = d*cosf(ang);
			y = d*sinf(ang);
		}
	}
	
	snow->x = x;
	snow->y = y;
	snow->z = z;
	
	snow->psi  = fmodf(snow->psi+snow->dpsi*speedFactor, 360);
	snow->theta= fmodf(snow->theta+snow->dtheta*speedFactor, 360);
	
}