i got stuck developing a game in XNA so i decided i'd ask a community for some help, and i choose this one.
So i been working on a game, it's still very basic and was hoping to work it out to a rpg game. I've followed riemers technieque to implement a heightmap combined with microsoft's example of collision detection. all of that works but now i wanna be able to move my character with my mouse instead of with my mouse. I been looking for a way to do that and found that picking is the solution. After searching for a VERY long time i found (also on riemers) a way to do it. Everything works untill the characters position increases in height, then the picking starts to get inaccurate. I'm using the following code for picking:
Vector3 direction = farWorldPoint - nearWorldPoint;
if (direction.Y < 0)
{
float zFactor = -nearWorldPoint.Y / direction.Y;
Vector3 zeroWorldPoint = nearWorldPoint + direction * zFactor;
return zeroWorldPoint;
}
}
return Vector3.Zero;
}
Does anyone know what i'm doing wrong and can give me some hints on how to solve it?
Is it because that picks the point on the x-y plane ( or x-z plane if you prefer ) . Do you have terrain or is your world flat ? It won't work for terrain that is not flat or that is at a different height. When your character rises (jumps or climbs), the picking is then not picking beneath him but beyond him, because he is now above the ground or higher than z=0.
Hi there, this here is what you need to do follow this link
If you can't make XNA code out of that, let us know. I have picking code for XNA and SlimDX available. But the basic principle is to step along the mouse-ray and test for height and when your height is negative, you know that somewhere between the last test position and the current test position lies the terrain. So it's an approximation method, but it works really well. You'll also need a function that tests for the height at a given x,y coordinate, by first finding what tile quad is there, then determining which of the two triangles you're over, then determining the finalHeight. search for Reimer's terrain picking code for an example.
So after being on a break for a while, i decided to pick up where i left on my XNA project...
I been working on "mouse picking" where i was strungling with.
Now i got some working code, but it not accurate when you click on a hill the heightmap and theres ground behind the hill...
Then you have a method that takes a ray and intersects it with the terrain return a Vector3 point:
public Vector3 GetRayCollisionPoint(Ray ray)
{
ray = ClipRay(ray, boundingBox.Max.Y, boundingBox.Min.Y);
ray = LinearSearch(ray);
Vector3 collisionPoint = BinarySearch(ray);
if (Calc.Contains(collisionPoint, boundingBox))
{
return collisionPoint;
}
else
{
Calc.MapBounds(ref collisionPoint, boundingBox);
collisionPoint.Y = GetHeightAtLoc(collisionPoint.X, collisionPoint.Z);
return collisionPoint;
}
}
That method uses a Linear search along the ray until the ray goes under the terrain, it then performs a Binary search backwards until it reaches a close enough approximation, in this instance within 0.01f of the terrain height. This results in fast accurate results that are hard to break. A Linear only approach would not be accurate enough due to the large stepping size, decreasing the stepping size would drastically increase computation time. A Binary only approach is prone to breaking if for instance you are looking through one hill, out the other side of it above the terrain again and then into another hill, you might well get the intersection point with the second hill. If no intersection point is found then an intersection with the map bounds is performed and the height at that point used, so if you're looking at the map wall or roof it will give you the point just below it on the terrain.