Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Space game targeting help.

Author
Message
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Dec 2012 08:38
I am working on a 3D space sim. I have a targeting system that works (kinda). The problem is that when the target is off screen, I want the crosshairs to be at the edge of the screen pointing to the shortest direction the ship needs to turn (if that makes sense).

It works ok, except at certain times off-screen. I'm not sure why it jumps around.....
I'm sure someone has a better method......

The fastest code is the code never written.
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 1st Dec 2012 11:00
it would be jumping around if you are 180degrees facing the opposite direction and constantly changing direction... if that makes sense... it is perfectly normal... if you are using Trigonometric Functions you could use some sort of buffer to slow down the rapid movement of the arrow...

I believe there is a C code block... code lang=C ?

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Dec 2012 10:25
Ok. Forget the code I posted earlier. I can't find ANY reference to what I'm trying to accomplish on Google. I'm using projection to get the cursor to appear on the target, but when the target is off screen I want the cursor to be at the edge of the screen, pointing in the nearest direction to the target...... I hope that clears up what I want...

@MrValentine,
I looked at the page, but I don't understand how to use it to do what I want.

The fastest code is the code never written.
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 2nd Dec 2012 11:05
I was hinting at the requirement for EZRotate...

There is a basic free version lying around the forum you could try first

Fallout
21
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 2nd Dec 2012 13:08 Edited at: 2nd Dec 2012 13:09
I would try this:

Pseudo type code ...


I assume that's what you're after. Hopefully it helps a bit.

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 7th Dec 2012 16:14
I got it to work (for the most part). It still has issues when passing 90 and 270, but I have to expect that.


The fastest code is the code never written.
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Dec 2012 03:37
I'm not too well versed in Dark GDK, I'm afraid (so I can't give actual code), and it looks like you got it mostly working.

However I wrote some similar code for DBC a while ago and figured that I would post the breakdown of the math, in case working out the kinks in your code ends up becoming more trouble than its worth. This system tracked objects and placed them on a radar. If you think about it, the math would be nearly identical up until the display portion(essentially, now the radar is the screen size), so I'll give you a run down of the math. It would be helpful if you are familiar with vector algebra, but if you aren't, I'll try to explain things.

What you need for setup:

-Unit Vectors (a 3D vector that is 1 unit long) for the player's LOCAL X,Y,Z orientation (Essentially, the local Z axis is the player's heading, x and y are to the right and straight up, respectively).

The math:
-Construct a vector from the player to the object being tracked (I would suggest storing the data as a unit vector and a distance). This will be called the Tracking Vector.

-Perform the DOT PRODUCT on the Tracking Vector and the Heading (local Z axis) Vector. This will project the tracking vector onto the heading vector. This will be referred to as R.

-The angle between these two vectors can be found by: THETA=acos(R)

-This angle is only MILDLY useful, in that it is rotation about a non-standard axis, meaning that on it's own, it gives us nothing. The next step is to find that axis.

-To find the axis, perform a CROSS PRODUCT on the vectors (since order matters, lets do HEADING x TRACKING). Make sure you are using the UNIT VECTORS, otherwise your resultants will be in the right direction, but your magnitudes will be off (which is important). The cross product will return a vector that is perpendicular to the two vectors being crossed. This is the SAME AXIS THAT THE ROTATION IS ABOUT.

(Aside)
Why this works: When you dot the tracking vector onto the heading vector, you are essentially constructing a right triangle. The base is the heading vector and has a length of whatever the dot product returns. The hypotenuse is the tracking vector and has a length of whatever that length is. Viewed on a piece of paper, the math is straightforward for the angle. Unfortunately, in the program, we don't know what the orientation of that paper is. The cross product gives us a vector that is perpendicular to the paper. Thus, the angle of the between the base of the triangle (heading) and the hypotenuse (tracking) is an angle about the perpendicular vector.
(/aside)

-One thing that you might have not been exposed to is the concept that rotations can be expressed as vectors. When drawing them, they typically get a double arrow or have a circle drawn around the line to denote it as a rotation vector, rather than a linear one. Thus we will do the same with our rotation vector. It has the unit vector that was returned from the cross product (lets call it the AXIS VECTOR) and a magnitude of THETA. Multiplying Theta across the components of the unit vector will give us the components of the vector itself. Lets call this the ROTATION VECTOR

-Finally, perform the DOT PRODUCT of the ROTATION VECTOR and each of the local axis vectors. This will yield numbers showing how much of the angle is around each axis.

-Output this data to the screen. In my code, I used the Pitch offset for the Y coordinate of my display and the Yaw offset for the X coordinate. I divided each by 180 and then multiplied by a constant to position the dots at the correct location on the screen. The dividing by 180 came about because I was using a circular radar and the radius wasn't set in stone yet (I was toying with things), so by dividing it by 180 (the maximum angle value around any axis in this case), I got a fraction, which I could multiply by any angle to make it display correctly.


Some additional Vector Algebra Notes:

Unit Vectors: Basically, a unit vector is any vector with a length of 1. Any vector can be converted into a unit vector quite simply (shown below). Futhermore, any vector can be expressed as a scalar (non vector) magnitude and a unit vector denoting direction. I will "A" to show a normal vector and "a" for a unit vector. I will use L as the magnitude of vector A.


Note that to find the magnitude, you just apply the distance formula to the vector.


The Dot Product: The dot product returns a non vector quantity (also referred to as a scalar quantity). Here is how it is performed (I will be using a WEDGE notation style, such that a vector is expressed as V=<x,y,z> where vector V has components x,y, and z).



The CROSS PRODUCT: The cross product is a more complicated operation than the DOT PRODUCT. Typically, Matrices are used, but I will skip through all that and show you the end result. Note that the CROSS PRODUCT will return a VECTOR quantity, rather than a scalar quantity.



Note that in a 2D system, the X and Y components of the cross product result are 0 (since Az and Bz=0).

Hope this helps! If you have questions, ask, though to be honest, I don't frequent this forum much, but I'll pop in over the next couple days to check.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Friv gamesonline
11
Years of Service
User Offline
Joined: 24th Dec 2012
Location:
Posted: 25th Dec 2012 05:51
I'm using projection to get the cursor to appear on the target, but when the target is off screen I want the cursor to be at the edge of the screen, pointing in the nearest direction to the target. I hope that clears up what I want.
--------------
Wish all you relax in comfort and fun with friv games online daily.
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 25th Dec 2012 08:32
Quote: "I'm using projection to get the cursor to appear on the target, but when the target is off screen I want the cursor to be at the edge of the screen, pointing in the nearest direction to the target. I hope that clears up what I want."


Yeah, same math. The only difference is how you do the last step (Outputting the data).

The final steps could be (totally untested, so you might have to fudge some of the math, this is just for the sake of giving direction):

-Calculate the angle made by the return commands (use Tangent: where tan(angle)=PitchOffset/YawOffset). Once you calculate the angle (using arctangent), that is the angle from the center of the screen to the direction of the target. It would take some simple adjusting to ensure that it is on the edge of the screen, but again, the angle created is the same.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2024-04-19 18:41:39
Your offset time is: 2024-04-19 18:41:39