admin管理员组文章数量:1278978
I'm working on a first-person shooter in Raylib, and I have a working system for controlling the camera and drawing a weapon model in front of the player. However, I’m having trouble with making sure the weapon stays in the camera's view when the camera moves vertically (up and down). The weapon correctly follows the left and right movement of the camera, but when I look up or down, the weapon shifts out of view.
Here’s the part of the code that handles rendering the weapon relative to the camera:
// Position and rotate the M4 model based on the camera's position and orientation
Vector3 forward = Vector3Normalize(Vector3Subtract(camera.target, camera.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, camera.up));
// Adjust the position of the M4 relative to the camera
Vector3 weaponPosition = {
camera.position.x + forward.x * 0.5f + right.x * -0.2f, // Adjust for left/right and forward offset
camera.position.y - 1.0f, // Offset down for first-person view
camera.position.z + forward.z * 0.5f + right.z * -0.2f
};
// Calculate weapon rotation to match the camera's forward direction
float angle = atan2f(forward.x, forward.z);
angle += PI / 2;
Vector3 rotationAxis = {0.0f, 1.0f, 0.0f}; // Rotation around the Y axis
// Draw the M4 model
DrawModelEx(m4Model, weaponPosition, rotationAxis, angle * RAD2DEG, {1.0f, 1.0f, 1.0f}, WHITE);
The problem occurs when I move the camera up or down — the weapon moves out of view. I’ve only adjusted its position relative to the camera’s forward and right vectors, but it seems like the vertical movement is not accounted for properly.
What I've tried:
I've attempted to adjust the weapon’s Y position based on the camera's Y position, but it doesn't solve the issue when I move the camera vertically. I’m using camera.target to orient the weapon, but I’m unsure if this is the best approach. Question: Is there a better way to handle the camera-weapon positioning so that the weapon stays in the camera's view even when moving up or down? How can I modify the positioning logic to make sure the weapon always stays in front of the camera, regardless of vertical movement?
I'm working on a first-person shooter in Raylib, and I have a working system for controlling the camera and drawing a weapon model in front of the player. However, I’m having trouble with making sure the weapon stays in the camera's view when the camera moves vertically (up and down). The weapon correctly follows the left and right movement of the camera, but when I look up or down, the weapon shifts out of view.
Here’s the part of the code that handles rendering the weapon relative to the camera:
// Position and rotate the M4 model based on the camera's position and orientation
Vector3 forward = Vector3Normalize(Vector3Subtract(camera.target, camera.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, camera.up));
// Adjust the position of the M4 relative to the camera
Vector3 weaponPosition = {
camera.position.x + forward.x * 0.5f + right.x * -0.2f, // Adjust for left/right and forward offset
camera.position.y - 1.0f, // Offset down for first-person view
camera.position.z + forward.z * 0.5f + right.z * -0.2f
};
// Calculate weapon rotation to match the camera's forward direction
float angle = atan2f(forward.x, forward.z);
angle += PI / 2;
Vector3 rotationAxis = {0.0f, 1.0f, 0.0f}; // Rotation around the Y axis
// Draw the M4 model
DrawModelEx(m4Model, weaponPosition, rotationAxis, angle * RAD2DEG, {1.0f, 1.0f, 1.0f}, WHITE);
The problem occurs when I move the camera up or down — the weapon moves out of view. I’ve only adjusted its position relative to the camera’s forward and right vectors, but it seems like the vertical movement is not accounted for properly.
What I've tried:
I've attempted to adjust the weapon’s Y position based on the camera's Y position, but it doesn't solve the issue when I move the camera vertically. I’m using camera.target to orient the weapon, but I’m unsure if this is the best approach. Question: Is there a better way to handle the camera-weapon positioning so that the weapon stays in the camera's view even when moving up or down? How can I modify the positioning logic to make sure the weapon always stays in front of the camera, regardless of vertical movement?
Share Improve this question asked Feb 24 at 12:15 XimBoneZXimBoneZ 215 bronze badges 4- Create a third-person shooter, and change the camera perspective once all movement seems to work? – Some programmer dude Commented Feb 24 at 12:21
- Use a separate Raylib Camera that uses the default coordinate system and render your weapon to that. – Botje Commented Feb 24 at 12:38
- Thanks @Botje thats working very well – XimBoneZ Commented Feb 24 at 12:59
- Suggest you write a self-answer with your solution so other people can find it. – Botje Commented Feb 24 at 13:18
1 Answer
Reset to default 1Thats the updated version when you use a own camera for the weapon like these it works very well.
Camera weaponCamera = {
.position = {0.0f, 0.0f, 0.0f}, // Centered at origin
.target = {-1.0f, 0.0f, 0.0f}, // Looking forward
.up = {0.0f, 1.0f, 0.0f}, // Up direction
.fovy = 90.0f, // Field of view
.projection = CAMERA_PERSPECTIVE // Perspective projection
};
Camera camera = {
.position = {10.0f, 2.0f, 10.0f}, // Camera's position in the world
.target = {0.0f, 1.5f, 0.0f}, // Look at a point closer to the ground
.up = {0.0f, 1.0f, 0.0f}, // Up direction
.fovy = 90.0f, // Field of view in degrees
.projection = CAMERA_PERSPECTIVE // Perspective projection
};
// Begin 3D rendering for the main camera
BeginMode3D(camera);
// Draw the map model
DrawModel(mapModel, {0, 0, 0}, 1.0f, WHITE);
DrawGrid(200, 3);
EndMode3D();
// Begin 3D rendering for the weapon camera
BeginMode3D(weaponCamera);
// Position the M4 model relative to the weapon camera
Vector3 weaponPosition = {-0.5f, -1, -0.2}; // Adjust as needed for proper placement
Vector3 rotationAxis = {0.0f, 1.0f, 0.0f}; // Rotation around Y axis
float angle = 0.0f; // Static rotation for the weapon
// Draw the weapon model
DrawModelEx(m4Model, weaponPosition, rotationAxis, angle, {1.0f, 1.0f, 1.0f}, WHITE);
EndMode3D();
本文标签:
版权声明:本文标题:c++ - How to Keep the Weapon in the Camera's View When Moving Up and Down in a First-Person Shooter (Raylib)? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741271391a2369370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论