-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarField.java
More file actions
66 lines (57 loc) · 1.84 KB
/
StarField.java
File metadata and controls
66 lines (57 loc) · 1.84 KB
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
/**
* StarField
*/
public class StarField {
private final float m_spread;
private final float m_spead;
private final float m_starX[];
private final float m_starY[];
private final float m_starZ[];
public StarField(int numStars,float spread,float speed)
{
m_spead =speed;
m_spread = spread;
m_starX = new float[numStars];
m_starY = new float[numStars];
m_starZ = new float[numStars];
for(int i=0;i<m_starX.length;i++)
{
InitStar(i);
}
}
private void InitStar(int i){
m_starX[i] = 2*((float)Math.random()-0.5f)* m_spread;
m_starY[i] = 2*((float)Math.random()-0.5f)* m_spread;
m_starZ[i] = ((float)Math.random()+0.00001f)* m_spread;
}
public void UpdateAndRender(Bitmap target, float delta)
{
target.clear((byte) 0x00);
System.out.println(delta);
//byte white = (byte) 0xFF;
float halfWidth = target.Get_width()/2.0f;
float halfHeight = target.Get_height()/2.0f;
target.clear((byte) 0x00);
for (int i=0;i<m_starX.length; i++)
{
m_starZ[i] -= delta * m_spead;
//System.out.println(m_starZ[i]);
// System.out.println(delta);
if(m_starZ[i] <=0)
{
System.out.println(m_starZ[i]);
InitStar(i);
}
int x= (int)(((m_starX[i]/m_starZ[i])*halfWidth) + halfWidth );
int y= (int)(((m_starY[i]/m_starZ[i])*halfHeight) + halfWidth );
if(x<0|| x>=target.Get_width()||(y<0||y>=target.Get_height()))
{
InitStar(i);
}
else
{
target.drawPixel(x, y,(byte) 0xFF ,(byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
}
}
}
}