admin管理员组文章数量:1315792
I'm presently trying to do a sorta Starfox clone with Raylib, using the heightmap example here: .html?name=models_heightmap
In order to handle collision in this environment, I think we need to check the player's current height against the grey value of the current pixel the craft is within. If the grey value of the currently 'occupied' ground pixel is 'higher' than the player's current Y, then the plane is currently touching the ground and has crashed.
In my illustration, we'll assume that if the player is at the height level indicated by C05 in that box, then they're in contact with the ground and is crashing. However, if the player is at the height of total white while positioned on a black square (XZ), eg. B04, then they're high over the canyon and not colliding.
I can't seem to work out how to identify the right pixel in the heightmap to match the player's position from within Raylib - does anybody know how to extract that info?
I'm presently trying to do a sorta Starfox clone with Raylib, using the heightmap example here: https://www.raylib/examples/models/loader.html?name=models_heightmap
In order to handle collision in this environment, I think we need to check the player's current height against the grey value of the current pixel the craft is within. If the grey value of the currently 'occupied' ground pixel is 'higher' than the player's current Y, then the plane is currently touching the ground and has crashed.
In my illustration, we'll assume that if the player is at the height level indicated by C05 in that box, then they're in contact with the ground and is crashing. However, if the player is at the height of total white while positioned on a black square (XZ), eg. B04, then they're high over the canyon and not colliding.
I can't seem to work out how to identify the right pixel in the heightmap to match the player's position from within Raylib - does anybody know how to extract that info?
Share Improve this question asked Jan 30 at 0:34 Reverend SpeedReverend Speed 2153 silver badges15 bronze badges1 Answer
Reset to default 0Turns out, it's reasonably easy to normalise world coords, apply that to image UV, get the heightmap brightness, normalise those, multiply by world height and then test against player position.
// Get Normalised Coord
float worldNormalX = (playerPosition.x + abs(mapPosition.x)) / mapSize.x;
float worldNormalZ = (playerPosition.z + abs(mapPosition.z)) / mapSize.z;
float texUcoord = worldNormalX * texture.width;
float texVcoord = worldNormalZ * texture.height;
// Clampity clamp (make this a helper function?) 0.001f - just to be sure we don't get OOBounds error
if (texUcoord > texture.height - 0.001f) texUcoord = texture.height - 0.001f;
if (texUcoord < 0) texUcoord = 0;
if (texVcoord > texture.width - 0.001f) texVcoord = texture.width - 0.001f;
if (texVcoord < 0) texVcoord = 0;
Color colorFromPosition = GetImageColor(image, texUcoord, texVcoord);
float worldYNormalFromCol = colorFromPosition.r / 255.0f;
float worldYPos = worldYNormalFromCol * mapSize.y;
playerPosition.y = worldYPos;
Hope this is useful to someone.
本文标签: imageHow do I get a 3D collision with a Heightmap in RaylibStack Overflow
版权声明:本文标题:image - How do I get a 3D collision with a Heightmap in Raylib? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741991110a2409123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论