views:

194

answers:

6

I'm designing a 3D game with a camera not entirely unlike that in The Sims and I want to prevent the player character from being hidden behind objects, including walls, pillars and other objects.

One easy way to handle the walls case is to have them face inwards and not have an other side, but that won't cover the other cases at all.

What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.

One probably not so good idea I had in mind is to scan from the camera to the player in a straight line and see if you hit a non-hidden object, continuing until you reach the player. Unfortunately, I am an almost complete newbie on 3D programming.

Demonstration SVG illustration < that wall panel obscures the player, so it must be hidden. Another unrelated and pretty much already solved problem is removing all three wall panels on that side, which is irrelevant to this question and only caused by the mapping system I came up with.

+1  A: 

One easy way, at least for prototyping, would be to always draw the player after you draw the rest of the scene. This would ensure that the player is rendered on top of anything else in the scene. Crude but effective.

Aric TenEyck
No, that won't do. That would, as you say, work for prototyping but it isn't at all what I'm looking for.
Kawa
+3  A: 

What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.

This is a good plan. You'll want to incorporate some kind of bounding volume onto the player, so the entire player (plus a little extra) is visible at all times. Then, simply run the intersection algorithm for each corner of the bounding volume.

Jon Seigel
Excuse me but /what/ did you just say? As far as I can understand your answer, it doesn't seem to be even close to what I meant.
Kawa
When figuring out which surfaces to remove or alpha-blend, think about drawing a straight line between the camera and the player. Then, every surface that hits that line needs alteration. However, if only a single line is drawn from the center of the player, then the player may still become partially obscured. Adding a "buffer" to the outside the player will ensure the entire player is visible. This is a little tough to describe without drawing a picture.
Jon Seigel
I see your point, but partial obscurity isn't a problem.
Kawa
Also, it's not just "any surface that hits that line", but the entire model that surface belongs to.
Kawa
If you set up the surfaces and objects in a heirarchical/OO way, if you know you have to hide a surface, simply hide the parent object which owns the surface.
Jon Seigel
Yes, that's entirely too correct. My GameObject class holds an XNA Model, which in turn has ModelMeshes. The entire Model should disappear.
Kawa
But all this and I still don't know how to actually check if the line between the camera and the player hits an object on its way down :(
Kawa
Me neither lol... I'm sure someone else on here does, though. Do a search, and if you come up empty, start a new question. :)
Jon Seigel
The keywords you're looking for in a google search are "Object Intersection" and also "collision detection". This process will allow you to "check if the line between the camera and the player hits an object...", as you mentioned. You want to draw a ray from the camera to certain points in space, checking for collisions with other objects. See: (for example) http://www.realtimerendering.com/intersections.html
JPDecker
A: 

The simplest thing I can think of that should work relatively well is to model all obstacles by a plane perpendicular to your ground (assuming you have a ground.) Roughly assuming everything that is an obstacle is a wall with some height.

Model your player as a point somewhere, and model your camera as another point. The line in 3d that connects these two points lies in a plane that is particularly interesting to you, because if this plane intersects an "obstacle plane" below the height of the obstacle, that means that that obstacle is blocking your view of the player point.

I hope thats somewhat clear. To make this into an algorithm you'd have to implement a general method for determining where two planes intersect (to determine if the obstacle is tall enough to block view.)

ldog
+1  A: 

Create a bounding volume from the camera to the extents of the player, determine what objects intersect that volume, and then render them in whatever alternate style you want?

There might be some ultra-clever way to do this, but this seems like the pretty straightforward version, and shouldn't be too much of a perf hit (you're probably doing collision every frame anyway....)

kyoryu
+1  A: 

Finding which object is at a given point on screen is called picking. Here's an XNA link for you which should direct you to an example. The idea is that you retrieve the 3D point in the game from the 2D point, and then can use standard collision detection methods to work out which object is occupying that space. Then you can elect to render that object differently.

One hack which might suffice if you have trouble with the picking approach is to render the character once as part of the scene, and then render it again at the end at half-alpha on top of everything. That way you can see the whole character and the wall, though you won't see through the wall as such.

Kylotan
It's funny -- that second approach you speak of is exactly what I came up with for the exact same situation. +1 for hivemind.
Kawa
I ended up using the Triangle Picking sample instead, but it ended up working out great. You win.
Kawa
A: 

A few ray tests between the camera origin and the player would do it. Then dont render the objects that obstruct.

tm1rbrt