sega 500 placing target boxes on the hud jeff “ezeikeil” giles jgiles@artschool.com jgiles

Post on 20-Jan-2016

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Sega 500

Placing target boxes on the HUD

Jeff “Ezeikeil” Gilesjgiles@artschool.comhttp://gamestudies.cdis.org/~jgiles

Today

Carrying on with our game type, we’re going to continue to expand on our pawn detection system by building a “lock on” system into the HUD.

So the goal is to draw some sort of targeting recticle or target box over where the enemy pawn is displayed on the HUD.

What we’re after

The idea is to help the player spot distant or hidden pawns.

This target recticle will indicate their approximate position.

What we’re after

What we’re after

In effect, we are going to be translating a 3D coordinate from world space to a 2D coordinate on the HUD.

What we’re after

Now that doesn’t look to tough now does it?

After all, we did pretty much just that with the radar blips didn’t we?

Well….sorta.

What we’re after

I know it doesn’t look too tough. But trust me, there is some funky math going on here.

At a minimum, we need to know 3 things about the world to make this work.

What we’re after

Where the target pawn is. Where we are. Which way we are facing.

What we’re after

As we learned yesterday, given this information we can infer: How far away they are. What direction they are from us.

Are they in front of us?

We have to figure out if the are in our visible field.

E.g. In front of us…and this, obviously depends on which way we are facing.

To find out what our facing is, we can simply grab our rotational value out of PawnOwner.

Are they in front of us?

At this point, we have a our rotation and a vector to the target pawn.

…But how can we tell if they are in front of us?

Look closer at what we have…a rotator & a vector.

Are they in front of us?

Starting with a rotator.

Functionally, what is it?

Are they in front of us?

We talked about this yesterday, essentially nothing more that an unit vector.

And a unit vector is like any other vector but with a magnitude of 1.

Are they in front of us?

So what about a vector?

Functionally, what does it tell us?

Are they in front of us?

A vector tells us a couple of things A position in space…but we’re not interested I

that right now. A size…how BIG it is. And a direction...which way it’s pointing.

Are they in front of us?

Remember, rotators and vectors are interchangeable. It just takes a bit of twiddling.

In effect, we have 2 vectors to work with.

Are they in front of us?

Look at it this way, we have our 2 vectors.

We are at the origin (0,0,0) and A is our orientation

The target it at B

(0,0,0)

A

B

C

Are they in front of us?

So, what we are REALLY interest in is the angle between these 2 vectors

E.g. Angle ACB

A

B

(0,0,0)C

Are they in front of us?

Now we can do all sorts of high school algebra & trigonometry to figure this out.

A

B

(0,0,0)C

Are they in front of us?

Or we could use a DOT product.

A

B

(0,0,0)C

Are they in front of us?

The DOT product is one of these handy little items used all the time in 3D. Commonly found in lighting calculations.

It’s primary purpose in life is to allow us to find the angle between 2 vectors.

It returns a scalar float value which ranges from -1 to 1.

Are they in front of us?

Depending on the relative directions of the 2 vectors, will determine if the value is positive or negative.

Are they in front of us? So if vector V is

parallel to the positive X axis, as vector U approaches it, the DOT product will approach 1

X+

Y+

(0,0,0) V

U

Are they in front of us? As vector U

moves towards the perpendicular (parallel to the positive Y axis), the DOT product will approach 0

X+

Y+

(0,0,0) V

U

Are they in front of us? And as vector U

approaches being parallel to the negative X axis, the DOT product will approach -1

X+

Y+

(0,0,0) V

U

Are they in front of us?

Therefore, knowing that the X positive in UT 2003 is forward of the player,

The DOT of our forward vector and the direction to the target is approaching 1, we know it’s in front of us.

Are they in front of us?

Right, time to code. For clarity, I’m going to create a new

function called DrawTargetRectical . Which will iterate over all near by pawns & test if they are in front of us.

For now, we’ll just throw some text at the screen when they are.

Are they in front of us?

Function & required variables

function DrawTargetRectical (Canvas c){ local vector x,y,z, direction; local float distance; local Pawn targetP;

Are they in front of us?

GetAxes(pawnowner.Rotation,x,y,z);

foreach RadiusActors(class'pawn',targetP,range,PawnOwner.Location) { direction= targetP.Location-PawnOwner.Location; distance = VSize( direction); direction= vector(rotator( direction)); if( direction dot x > 0.7) { c.SetPos(20,100); c.DrawText("-->"$targetP.PlayerReplicationInfo.Playername$" ahead"); } }

Are they in front of us?

What’s going on

Hey cool! It actually pops up when someone is in front of us.

But what’s going on?

What’s going on

GetAxes(pawnowner.Rotation,x,y,z);

From the actor class

native(229) static final function GetAxes ( rotator A, out vector X, out vector Y, out vector Z );

Get X,Y and Z axes values for the given rotator and sets X,Y,Z Accordingly. Where X is forward direction, Y points right and Z points upwards. (relative to the rotation expressed by A)

What’s going on

foreach RadiusActors(class'pawn',targetP,range,PawnOwner.Location)

{

direction= targetP.Location-PawnOwner.Location;

distance = VSize(direction);

This should look familiar. It’s the same code from the Radar screen.

We simply calculate it’s direction & distance.

What’s going on

direction= vector( Normalize( rotator (direction)));

Looks a bit sticky, but were really just doing a bunch of casting to get our normalized vector.

What’s going on

From the Wiki

rotator Normalize (rotator Rot) [static] Returns the corresponding rotator with

components between 0 and 65535.

What’s going on

if( direction dot x > 0.7)

Calculating the DOT product. Remember, as the DOT of 2 vectors

approach 1, they line up.

Notice that the “dot” keyword is in fact an operator in UT2003

However…

We have a problem.

For some reason, I’m occasionally “in front of myself”.

Notice that there is no one on the radar.

An easy fix

We have 2 options. The obvious solution is to exclude myself

from the test using an if condition.

Or we could be clever…

An easy fix

Swap this line direction= vector(Normalize( rotator(direction)));

with…direction= direction/distance;

Same end result. What are getting in both cases is the normalized unit vector. This works because a distance to myself is GOING to be zero and UT2003 seems to deal with divide by zero quite well. Treating them as an ignore case.

Drawing the recticle

Now that we’ve fixed that, Lets go about adding a target recticle overtop of the pawn.

My texture: target=Texture'Crosshairs.HUD.Crosshair_Circle2‘

Drawing the recticle

And I just used DrawIcon similar to how we placed the blips yesterday.

c.SetPos( Xpos-16, Ypos-16);

c.DrawIcon(target, 1);

16 being ½ the texture dimension

Drawing the recticle

To set our position to the screen, we need to do 2 more dot products.

One of the z axis (vertical) and the Y (left & right)

And then set our position. I created 2 int’s to make this a bit easier.

local int XPos, YPos;

Drawing the recticle

Casting it to screen space. The Clip/2 places us at the center of the

screen

Xpos = c.ClipX/2 * (1+1.4*(direction dot Y));Ypos = c.ClipY/2 * (1-1.4*(direction dot Z));

Drawing the recticle

The DOT products tell us it we need to move the cursor position up, down, left, or right from the center of the screen.

Remember that they give us a value between -1 and 1

Xpos = c.ClipX/2 * (1+1.4*(direction dot Y));Ypos = c.ClipY/2 * (1-1.4*(direction dot Z));

Drawing the recticle

As for the 1+1.4 and 1-1.4 they are important for the positioning of the recticle when the player pitches their point of view.

When the player pitches, the DOT moves away from zero causing these to be relevant.

In effect they are corrections.

Xpos = c.ClipX/2 * (1+1.4*(direction dot Y));Ypos = c.ClipY/2 * (1-1.4*(direction dot Z));

But there is one more thing

Notice that as I pitch up and down, the rectical pulls away from the target.

Although it’s nearly perfectly lined up if my pitch is parallel to the horizontal.

But there is one more thing

But I’m going to leave this to you as part of your assignment. After all, there’s not much point in me simply *giving* you the bonus part of the assignment.

It’s actually a fairly easy fix, although somewhat obscure.

Drawing the recticle

Also, this is not the only way to do this. There are many was to skin the proverbial “cat”.

However, I feel it does a reasonable job illustrating how to go from world coordinates to screen coordinates;

Drawing the recticle

But if your interest, Chimeric has an interesting tutorial on this matter.

http://www.unrealscript.com/tutorials/tut35.php

That’s all folks

Have a good weekend & start researching your assignment.

Next class, we start messing with the camera system.

top related