Skip to content

Commit 87b0004

Browse files
author
John Ludlow
committed
Set Transform to make models remember their positions
1 parent f2b4fc1 commit 87b0004

File tree

2 files changed

+19
-23
lines changed
  • Community/Models/RaylibCsExamples.Community.Core.Models.MeshPicking
  • Local/Objects/RaylibCsExamples.Local.Objects.ObjectOcclusionTest

2 files changed

+19
-23
lines changed

Community/Models/RaylibCsExamples.Community.Core.Models.MeshPicking/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public unsafe static int Main()
3131
var texture = Raylib.LoadTexture("Resources/Models/turret_diffuse.png");
3232
Raylib.SetMaterialTexture(ref tower, 0, MaterialMapIndex.Albedo, ref texture);
3333

34-
Vector3 towerPos = new(5.0f, 0.0f, 5.0f);
35-
var towerBBox = Raylib.GetMeshBoundingBox(tower.Meshes[0]);
34+
tower.Transform = Raymath.MatrixTranslate(5.0f, 0.0f, 5.0f);
35+
Vector3 towerPos = Vector3.Zero;
36+
var towerBBox = Raylib.GetModelBoundingBox(tower);
3637

3738
// Ground quad
3839
Vector3 g0 = new(-50.0f, 0.0f, -50.0f);

Local/Objects/RaylibCsExamples.Local.Objects.ObjectOcclusionTest/Program.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ private static void Main(string[] args)
118118
Projection = CameraProjection.Perspective
119119
};
120120

121-
var planeModel = Raylib.LoadModelFromMesh(Raylib.GenMeshPlane(20, 20, 3, 3));
121+
var planeModel = Raylib.LoadModelFromMesh(Raylib.GenMeshPlane(20, 20, 3, 3));
122122

123123
var greenCubeModel = Raylib.LoadModelFromMesh(Raylib.GenMeshCube(3, 3, 3));
124-
var greenCubePosition = new Vector3(-5, 2, 0);
124+
greenCubeModel.Transform = Raymath.MatrixTranslate(-5, 2, 0);
125125

126126
var blueCubeModel = Raylib.LoadModelFromMesh(Raylib.GenMeshCube(3, 3, 3));
127-
var blueCubePosition = new Vector3(5, 2, 0);
127+
blueCubeModel.Transform = Raymath.MatrixTranslate(5, 2, 0);
128128

129129
Raylib.SetMousePosition(screenWidth / 2, screenHeight / 2);
130130

@@ -139,15 +139,15 @@ private static void Main(string[] args)
139139
Raylib.DrawModel(planeModel, Vector3.Zero, 1, Color.Gray);
140140
Raylib.DrawModelWires(planeModel, Vector3.Zero, 1, Color.LightGray);
141141

142-
Raylib.DrawModel(greenCubeModel, greenCubePosition, 1, Color.Green);
143-
Raylib.DrawModelWires(greenCubeModel, greenCubePosition, 1, Color.Black);
142+
Raylib.DrawModel(greenCubeModel, Vector3.Zero, 1, Color.Green);
143+
Raylib.DrawModelWires(greenCubeModel, Vector3.Zero, 1, Color.Black);
144144

145-
Raylib.DrawModel(blueCubeModel, blueCubePosition, 1, Color.Blue);
146-
Raylib.DrawModelWires(blueCubeModel, blueCubePosition, 1, Color.Black);
145+
Raylib.DrawModel(blueCubeModel, Vector3.Zero, 1, Color.Blue);
146+
Raylib.DrawModelWires(blueCubeModel, Vector3.Zero, 1, Color.Black);
147147
}
148148
Raylib.EndMode3D();
149149

150-
if (IsBoundingBoxVisible(camera, cameraPositions, [(greenCubeModel, greenCubePosition), (blueCubeModel, blueCubePosition)], (greenCubeModel, greenCubePosition), "green cube"))
150+
if (IsBoundingBoxVisible(camera, cameraPositions, [greenCubeModel, blueCubeModel], greenCubeModel, "green cube"))
151151
{
152152
Raylib.DrawText("Green cube is visible", 10, 30, 30, Color.Green);
153153
Raylib.TraceLog(TraceLogLevel.All, "Green cube is visible");
@@ -158,13 +158,13 @@ private static void Main(string[] args)
158158
Raylib.TraceLog(TraceLogLevel.All, "Green cube is NOT visible");
159159
}
160160

161-
if (IsBoundingBoxHovered(camera, greenCubeModel, greenCubePosition))
161+
if (IsBoundingBoxHovered(camera, greenCubeModel))
162162
{
163163
Raylib.DrawText("Green cube is hovered", 10, 60, 30, Color.DarkGreen);
164164
Raylib.TraceLog(TraceLogLevel.All, "Green cube is hovered");
165165
}
166166

167-
if (IsBoundingBoxVisible(camera, cameraPositions, [(greenCubeModel, greenCubePosition), (blueCubeModel, blueCubePosition)], (blueCubeModel, blueCubePosition), "blue cube"))
167+
if (IsBoundingBoxVisible(camera, cameraPositions, [greenCubeModel, blueCubeModel], blueCubeModel, "blue cube"))
168168
{
169169
Raylib.DrawText("Blue cube is visible", 10, 120, 30, Color.Blue);
170170
Raylib.TraceLog(TraceLogLevel.All, "Blue cube is visible");
@@ -175,7 +175,7 @@ private static void Main(string[] args)
175175
Raylib.TraceLog(TraceLogLevel.All, "Blue cube is NOT visible");
176176
}
177177

178-
if (IsBoundingBoxHovered(camera, blueCubeModel, blueCubePosition))
178+
if (IsBoundingBoxHovered(camera, blueCubeModel))
179179
{
180180
Raylib.DrawText("Blue cube is hovered", 10, 150, 30, Color.DarkBlue);
181181
Raylib.TraceLog(TraceLogLevel.All, "Blue cube is hovered");
@@ -193,7 +193,7 @@ private static void Main(string[] args)
193193
Raylib.CloseWindow();
194194
}
195195

196-
private static bool IsBoundingBoxVisible(Camera3D camera, Vector2[] cameraPositions, (Model model, Vector3 position)[] allModels, (Model model, Vector3 position) targetModel, string id)
196+
private static bool IsBoundingBoxVisible(Camera3D camera, Vector2[] cameraPositions, Model [] allModels, Model targetModel, string id)
197197
{
198198
foreach (var cameraPosition in cameraPositions)
199199
{
@@ -203,21 +203,18 @@ private static bool IsBoundingBoxVisible(Camera3D camera, Vector2[] cameraPositi
203203

204204
foreach (var model in allModels)
205205
{
206-
var boundingBox = Raylib.GetModelBoundingBox(model.model);
207-
boundingBox.Min += model.position;
208-
boundingBox.Max += model.position;
209-
206+
var boundingBox = Raylib.GetModelBoundingBox(model);
210207
var collision = Raylib.GetRayCollisionBox(ray, boundingBox);
211208

212209
if (collision.Hit && (collision.Distance < closestCollision?.Distance || closestCollision is null))
213210
{
214211
Raylib.DrawBoundingBox(boundingBox, Color.Purple);
215212
closestCollision = collision;
216-
closestModel = model.model;
213+
closestModel = model;
217214
}
218215
}
219216

220-
if (closestCollision is not null && closestModel.Equals(targetModel.model))
217+
if (closestCollision is not null && closestModel.Equals(targetModel))
221218
{
222219
Raylib.TraceLog(TraceLogLevel.All, $"{id} is visible at position {ray.Position} ({closestCollision})");
223220
Raylib.DrawText("X", (int)cameraPosition.X, (int)cameraPosition.Y, 10, Color.DarkGreen);
@@ -233,12 +230,10 @@ private static bool IsBoundingBoxVisible(Camera3D camera, Vector2[] cameraPositi
233230
return false;
234231
}
235232

236-
private static bool IsBoundingBoxHovered(Camera3D camera, Model model, Vector3 position)
233+
private static bool IsBoundingBoxHovered(Camera3D camera, Model model)
237234
{
238235
var ray = Raylib.GetScreenToWorldRay(Raylib.GetMousePosition(), camera);
239236
var boundingBox = Raylib.GetModelBoundingBox(model);
240-
boundingBox.Min += position;
241-
boundingBox.Max += position;
242237

243238
var rayCollision = Raylib.GetRayCollisionBox(ray, boundingBox);
244239
if (rayCollision.Hit)

0 commit comments

Comments
 (0)