How To Make My Crosshair Red When Over An Enemy?

Started by
11 comments, last by Thinias 7 years, 8 months ago

My crosshair is a 2D GUI element. The enemy is a 3D guy. Should I somehow try to do the calculations in the vertex shader somehow, or make some complicated collision shape for the player and somehow check if the bullet ray intersects with the collision box. ( I have one math book for 3d games, I'm gonna figure it out how if I know that this is the best way ).

Advertisement

When you shoot, how do you detect whether you hit?

I'd suggest performing the same kind of check (would guess some ray cast) and just use that.

If going that route, you could also cache the result, so you don't have to do another check when firing, but that might depend on your weapon (e.g. if you have a weapon with a high amount of spread, your crosshair might be red, even though bullets might miss).

Hello to all my stalkers.

Just to clarify, this isn't a shader/GPU problem. Do your check in the CPU like you normally would and then either set a color value on your crosshair when rendering or just render a different crosshair.

You definitely want to use the exact same calculation you use for firing (if possible) as otherwise your crosshair runs the risk of "lying" to the player (e.g. if the crosshair calculation says "you can hit" but the actual bullet sim would not).

You might have to accept some inaccuracy here if you have a complicated sim. For instance, if your bullets have mass and complex physics interactions or are heat-seeking missiles or the like, you'll probably just want to fall back to a simple ray cast for your crosshair.

Sean Middleditch – Game Systems Engineer – Join my team!

Guys, thanks for the clear answers. Knowing that I won't have to touch the shaders gives me hope, I'm really bad on getting feedback from them, so it's kind of hard to debug.

On the other hand, my game is pretty simple. I have neither bullets, nor a proper player model, I just have a moving camera that can jump and rotate properly, a 2d crosshair and an enemy that has few anims.

But the problem is that the enemy's shape is quite complicated because I exported the human from world of warcraft and sounds hard to me to actually generate a proper collision shape for the guy. I know that the easiest collision shape is a bounding sphere, but it doesn't really work for a human model.

Can you give me something that is good for a start and that gives not so bad results? ( I have no xp in 3d collisions )

Can you give me something that is good for a start

A box might work. Another typical collider shape is a capsule.

Alternatively, several spheres stacked on top of each other -- if either of them are intersected, you've got a hit.

Hello to all my stalkers.

First thing tomorrow will be to try a box for the body and maybe a separate sphere for the head in order to make headshots tricky. And then I'm gonna see if it works fine.

I really like the idea with the stacked spheres, will try that too.

Thanks.

If you have a physics engine, the physics engine can do a raycast for you from the center of the camera to the target.

If not, then you can actually break up the target into tight fitting hitboxes and cylinders. With a bit of Raycasting math, you can easily implement this feature.

Ok guys, I managed to generate an AABB for every enemy based on its dimensions, now how do I proceed?

I can't find any material on collision detection between a ray and a box in any of my books. This means that it should be easy, but nothing comes to mind, any ideas? ( Bear in mind that when my enemy rotates, the bounding box rotates with him, so I don't know if it still counts as axially aligned, and I don't really know if that helps me or not )

I can't find any material on collision detection between a ray and a box in any of my books. This means that it should be easy, but nothing comes to mind, any ideas?


Figure out how to compute the intersection between a ray and a plane. You can do it in such a way that the result is expressed in a frame of reference in the plane such that both coordinates will be between 0 and 1 precisely when the rectangular face has been hit. Now use that six times.

Figure out how to compute the intersection between a ray and a plane.

I think I figured how this works mathematically, can you see if my calculations are right.

What I did so far:

First, I found a representation for the ray.

Q(t) = S + tV seems to be the best one for this purpose. ( Where S is the starting point of the line and V is the direction )

2. I found how to represent a plane.

I need only two things: a normal ( let's call it N ) and one point that belongs to the plane( let's call it P ).

3. I found how to check if a point is on the plane ( let's name it Q ). Basically, to check if Q belongs to the plane, I just find Q-P ( which is the vector pointing from P to Q ) and then I check the dot product with the normal and if the dot product is 0, that means that the signed length of the projection of Q-P onto N is 0, which means that the two vectors are perpendicular, therefore Q belongs to the plane. Cool.

( this is the formula: N (dot) ( Q - P ) = 0 ).

So, ultimately, what I need to find is the 't' variable in the line equation, which is Q(t) = S + Vt.

If we continue the above formula, and apply the distributive law, we get this: N (dot) Q - N (dot) P = 0.

and when we substitute Q with S + tV and we solve the equation for 't', we get this: t = - ( N(dot)S - N(dot)P ) / N(dot)V .

So if I can somehow tell this to my pc, I will see where the intersection happened.

It's late. I'm going to sleep. If someone bothered to read all of this, please tell me if I'm on the right track.

EDIT : I'm on the right track.

This topic is closed to new replies.

Advertisement