Subversion Repositories distributed

Rev

Rev 26 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
22 daniel-mar 1
package test.fraktal3d;
2
import com.jme.app.SimpleGame;
3
import com.jme.math.Vector3f;
4
import com.jme.renderer.ColorRGBA;
5
import com.jme.scene.shape.Sphere;
6
import com.jme.scene.state.MaterialState;
7
import com.jme.util.Timer;
8
 
9
public class BouncingBall extends SimpleGame {
10
 
11
        // Modellvariablen
12
        private Room room = new Room(50, 50, 50, 1);
13
        private Vector3f mBallMove = new Vector3f();;
14
        private Sphere ball;
15
 
16
        // Kräfte
17
        private Vector3f g_mPerSec = new Vector3f(0, -9.81f, 0);
18
        private Vector3f bounceLoss_mPerSec = new Vector3f(0.99f, 0.9f, 0.99f);
19
 
20
        // Anfangswert
21
        private Vector3f v_mPerSec = new Vector3f(100, 0, 50);
22
 
23
        @Override
24
        protected void simpleInitGame() {
25
                this.ball = createBall("Ball", 50, 50, 2);
26
 
27
                // Scene einstellen
28
                // Kamaraentfernung erhöhen
29
                cam.setLocation(new Vector3f(0, 50, 150));
30
 
31
                // Elemente an rootNode anhängen
32
                rootNode.attachChild(this.room.getRoomNode());
33
                rootNode.attachChild(this.ball);
34
        }
35
 
36
        private Sphere createBall(String str, int zSam, int radSam, float radius) {
37
                Sphere ball = new Sphere(str, zSam, radSam, radius);
38
                ball.setLocalTranslation(this.mBallMove);
39
 
40
                MaterialState materialState = display.getRenderer()
41
                                .createMaterialState();
42
 
43
                float opacityAmount = 0.3f;
44
                materialState
45
                                .setAmbient(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
46
                materialState
47
                                .setDiffuse(new ColorRGBA(0.1f, 0.5f, 0.8f, opacityAmount));
48
                materialState
49
                                .setSpecular(new ColorRGBA(1.0f, 1.0f, 1.0f, opacityAmount));
50
                materialState.setShininess(128.0f);
51
                materialState
52
                                .setEmissive(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
53
 
54
                materialState.setEmissive(new ColorRGBA(0.5f, 0.0f, 0.0f, 0.1f));
55
 
56
                ball.setRenderState(materialState);
57
                ball.updateRenderState();
58
 
59
                this.mBallMove.y = room.getBorderPosition(RoomBorderEnum.UP).y
60
                                - ball.getRadius();
61
                this.mBallMove.x = room.getBorderPosition(RoomBorderEnum.LEFT).x
62
                                + ball.getRadius();
63
                this.mBallMove.z = room.getBorderPosition(RoomBorderEnum.LEFT).z
64
                                + ball.getRadius();
65
 
66
                return ball;
67
        }
68
 
69
        @Override
70
        protected void simpleUpdate() {
71
                // Frames werden an die Systemzeit angepasst und für das realistische
72
                // Model variiert
73
                float time = Timer.getTimer().getTimePerFrame() * 4;
74
 
75
                calculateForces(this.mBallMove.x >= room
76
                                .getBorderPosition(RoomBorderEnum.LEFT).x
77
                                + ball.getRadius()
78
                                && this.mBallMove.x <= room
79
                                                .getBorderPosition(RoomBorderEnum.RIGHT).x
80
                                                - ball.getRadius(), this.mBallMove.y >= room
81
                                .getBorderPosition(RoomBorderEnum.DOWN).y
82
                                + ball.getRadius()
83
                                && this.mBallMove.y <= room
84
                                                .getBorderPosition(RoomBorderEnum.UP).y
85
                                                - ball.getRadius(), this.mBallMove.z >= room
86
                                .getBorderPosition(RoomBorderEnum.BACK).z
87
                                + ball.getRadius()
88
                                && this.mBallMove.z <= room
89
                                                .getBorderPosition(RoomBorderEnum.FRONT).z
90
                                                - ball.getRadius(), time);
91
 
92
                this.mBallMove.addLocal(this.v_mPerSec.mult(time));
93
        }
94
 
95
        private void calculateForces(boolean xCollision, boolean yCollision,
96
                        boolean zCollision, float time) {
97
 
98
                // Kollision mit den Wänden
99
                if (!yCollision) {
100
                        this.bounceLoss_mPerSec.mult(0.9f);
101
 
102
                        this.v_mPerSec.x *= this.bounceLoss_mPerSec.x;
103
                        this.v_mPerSec.y *= -this.bounceLoss_mPerSec.y;
104
                        this.v_mPerSec.z *= this.bounceLoss_mPerSec.z;
105
 
106
                        if (this.mBallMove.y < room.getBorderPosition(RoomBorderEnum.DOWN).y
107
                                        + ball.getRadius()) {
108
                                this.mBallMove.y = room.getBorderPosition(RoomBorderEnum.DOWN).y
109
                                                + ball.getRadius();
110
 
111
                        } else {
112
                                this.mBallMove.y = room.getBorderPosition(RoomBorderEnum.UP).y
113
                                                - ball.getRadius();
114
                        }
115
 
116
                } else if (!xCollision) {
117
                        this.v_mPerSec.x *= -this.bounceLoss_mPerSec.y;
118
                        this.v_mPerSec.y *= this.bounceLoss_mPerSec.x;
119
                        this.v_mPerSec.z *= this.bounceLoss_mPerSec.z;
120
 
121
                        if (this.mBallMove.x < room.getBorderPosition(RoomBorderEnum.LEFT).x
122
                                        + ball.getRadius()) {
123
                                this.mBallMove.x = room.getBorderPosition(RoomBorderEnum.LEFT).x
124
                                                + ball.getRadius();
125
 
126
                        } else {
127
                                this.mBallMove.x = room.getBorderPosition(RoomBorderEnum.RIGHT).x
128
                                                - ball.getRadius();
129
                        }
130
                } else if (!zCollision) {
131
                        this.v_mPerSec.x *= this.bounceLoss_mPerSec.x;
132
                        this.v_mPerSec.y *= this.bounceLoss_mPerSec.z;
133
                        this.v_mPerSec.z *= -this.bounceLoss_mPerSec.y;
134
 
135
                        if (this.mBallMove.z < room.getBorderPosition(RoomBorderEnum.BACK).z
136
                                        + ball.getRadius()) {
137
                                this.mBallMove.z = room.getBorderPosition(RoomBorderEnum.BACK).z
138
                                                + ball.getRadius();
139
 
140
                        } else {
141
                                this.mBallMove.z = room.getBorderPosition(RoomBorderEnum.FRONT).z
142
                                                - ball.getRadius();
143
                        }
144
                }
145
 
146
                // Kräfte setzen
147
                this.v_mPerSec.addLocal(this.g_mPerSec.mult(time));
148
 
149
        }
150
 
151
        /**
152
         * @param args
153
         */
154
        public static void main(String[] args) {
155
                BouncingBall ball = new BouncingBall();
156
                ball.setConfigShowMode(ConfigShowMode.AlwaysShow);
157
                ball.start();
158
        }
159
}