Hey, ik ben me onlangs beginnen verdiepen in 3D programmeren met XNA omdat me dat nogal interessant leek, ondanks het feit dat wiskunde één van mijn zwakke punten is. Nu heb ik al een paar van de basics onder de knie en ben momenteel bezig met een FPS-style camera class te maken aan de hand van deze tutorial.
Alles lijkt goed te werken, behalve dat bij een lage framerate de camera rotatie sneller/gevoeliger is dan bij een hoge framerate. Nu begrijp ik niet goed waarom. Ik dacht dat dit stukje dit moest verhelpen (* gt):
Ik heb nog geprobeerd verschillende variables aan te passen aan de gametime maar zonder succes
. Iemand die weet waar het probleem zit?
Alvast bedankt.
C#:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
| public class Camera3D { public const float RotationSpeed = 0.5f; public const float MoveSpeed = 30.0f; private float xRot; private float yRot; private MouseState initialMouseState; private Viewport viewport; public Vector3 Position { get; set; } public Vector3 LookAt { get; set; } public Matrix Rotation { get; set; } public Matrix Projection {get; set;} public Matrix View { get; set; } public Camera3D(GraphicsDevice graphicsDevice, Vector3 position, Vector3 lookAt, float fieldOfView) { float aspectRatio = (float)graphicsDevice.DisplayMode.Width / (float)graphicsDevice.DisplayMode.Height; float fov = MathHelper.ToRadians(fieldOfView); this.Position = position; this.LookAt = lookAt; this.View = Matrix.CreateLookAt(Position, LookAt, Vector3.Up); this.Projection = Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, 0.01f, 10000.0f); this.viewport = graphicsDevice.Viewport; Mouse.SetPosition(viewport.Width / 2, viewport.Height / 2); this.initialMouseState = Mouse.GetState(); this.xRot = MathHelper.PiOver2; this.yRot = -MathHelper.Pi / 10.0f; } public Camera3D(GraphicsDevice graphicsDevice, float fieldOfView) : this(graphicsDevice, Vector3.Zero, Vector3.Zero, fieldOfView) { } public Camera3D(GraphicsDevice graphicsDevice) : this(graphicsDevice, Vector3.Zero, Vector3.Zero, 45.0f) { } public void Update(GameTime gameTime) { var gt = (float)gameTime.ElapsedGameTime.TotalSeconds; var ks = Keyboard.GetState(); var ms = Mouse.GetState(); var moveVector = Vector3.Zero; if (ks.IsKeyDown(Keys.Z)) moveVector += Vector3.Forward; if (ks.IsKeyDown(Keys.S)) moveVector += Vector3.Backward; if (ks.IsKeyDown(Keys.Q)) moveVector += Vector3.Left; if (ks.IsKeyDown(Keys.D)) moveVector += Vector3.Right; if (ks.IsKeyDown(Keys.C)) moveVector += Vector3.Down; if (ks.IsKeyDown(Keys.Space)) moveVector += Vector3.Up; moveVector *= gt; if (ms != initialMouseState) { float xDifference = ms.X - initialMouseState.X; float yDifference = ms.Y - initialMouseState.Y; this.xRot -= Camera3D.RotationSpeed * xDifference * gt; this.yRot -= Camera3D.RotationSpeed * yDifference * gt; Mouse.SetPosition(this.viewport.Width / 2, this.viewport.Height / 2); } Vector3 rotatedVector, originalTarget, originalUpVector, rotatedTarget, rotatedUpVector; this.Rotation = Matrix.CreateRotationX(this.yRot) * Matrix.CreateRotationY(this.xRot); rotatedVector = Vector3.Transform(moveVector, this.Rotation); this.Position += Camera3D.MoveSpeed * rotatedVector; originalTarget = new Vector3(0, 0, -1); originalUpVector = new Vector3(0, 1, 0); rotatedTarget = Vector3.Transform(originalTarget, this.Rotation); this.LookAt = this.Position + rotatedTarget; rotatedUpVector = Vector3.Transform(originalUpVector, this.Rotation); this.View = Matrix.CreateLookAt(this.Position, this.LookAt, rotatedUpVector); } public override string ToString() { return String.Format("CAM Pos:{0}\nCAM Pov:{1}\n", this.Position, this.LookAt); } } |
Alles lijkt goed te werken, behalve dat bij een lage framerate de camera rotatie sneller/gevoeliger is dan bij een hoge framerate. Nu begrijp ik niet goed waarom. Ik dacht dat dit stukje dit moest verhelpen (* gt):
C#:
1
2
| this.xRot -= Camera3D.RotationSpeed * xDifference * gt; this.yRot -= Camera3D.RotationSpeed * yDifference * gt; |
Ik heb nog geprobeerd verschillende variables aan te passen aan de gametime maar zonder succes
Alvast bedankt.
Asus P8P67 EVO | i5 2500k (4.8 GHz) | Sapphire HD 7970 Vapor-X GHz Ed. | 8 GB DDR3 1600 | 1 TB HDD