Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (fairtrader, Quad, miwok, Martin_HH, AndrewAMD, alibaba, dpn), 581 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Print Thread
Rate Thread
Math is my anathema....or amathena #207751
05/22/08 03:04
05/22/08 03:04
Joined: Sep 2003
Posts: 733
Whitefish, Montana
JazzDude Offline OP
User
JazzDude  Offline OP
User

Joined: Sep 2003
Posts: 733
Whitefish, Montana
I hope I can explain the problem better than I can solve it.

I need to relate the player's location on the terrain to an icon's location on a full screen map panel.

The terrain is a square which is -2000 - 0 + 2000 (4000 high and 4000 wide total) with 0 in the middle.

The screen panel is 1024 x 768.

I'm thinking roughly something like:

vec_set(player.x) = icon_temp.x modified by some math function

I know some of you fine coders are math geniuses. Is it possible? If so, how?

Might be easier if I move the whole terrain so that 0 is in the upper left corner like the screen. Then I guess it would be some ratio that would accomodate the square and the rectangle. However that would require changing a couple hundred vec_sets in the game.

Last edited by JazzDude; 05/22/08 05:21.
Re: Math is my anathema....or amathena [Re: JazzDude] #207758
05/22/08 06:16
05/22/08 06:16
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
Xarthor Offline
Expert
Xarthor  Offline
Expert

Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
You want to calculate the players position into a screen coordinate?
Do something like:
Code:
var map_ratio[2]; // saves the ratio from real world coordinates to map coordinates
var icon_pos[2]; // where the icon can be placed on the 2d map (screen position)

var map_size[2] = 1024,768;
var terrain_size[2] = 4000,4000;

function map_CalcIconPos(&_WorldPos)
{
  icon_pos.x = _WorldPos[0] * map_ratio.x + (map_size.x / 2);
  icon_pos.y = _WorldPos[1] * map_ratio.y + (map_size.y / 2);
}

function map_startup()
{
  map_ratio.x = map_size.x / terrain_size.x;
  map_ratio.y = map_size.y / terrain_size.y;
}


Now if you happen to change the terrain to have its upper left corner at 0 you just have to remove the + (..) part of the icon_pos.x/y calculation lines.

If you want to have it the other way around:
Use the code from above plus this function
Code:
function map_CalcEntPos(&_MapPos, _ent)
{
  _ent.x = (_MapPos[0] - (map_size.x/2)) / map_ratio.x;
  _ent.y = (_MapPos[1] - (map_size.y/2)) / map_ratio.y;
}


How to call these functions:

map_CalcIconPos(&_WorldPos)
This function expects a vector as parameter. Thus you can call it like this:
map_CalcIconPos(player.x);
or:
map_CalcIconPos(vector(x,y,z));
(the z component will not be used in the calculation.

map_CalcEntPos(&_MapPos, _ent);
This function expects a vector and an entity pointer as parameters.
Example:
map_CalcEntPos(vector(256,20,0),player);
Now the player entity will be set at the world coordinates of the map location (256,20).

NOTE:
None of this code is tested! Used it at your own risk wink
If it crashes your computer its not my fault laugh

Re: Math is my anathema....or amathena [Re: Xarthor] #207844
05/22/08 16:05
05/22/08 16:05
Joined: Sep 2003
Posts: 733
Whitefish, Montana
JazzDude Offline OP
User
JazzDude  Offline OP
User

Joined: Sep 2003
Posts: 733
Whitefish, Montana
Very impressive, Xarthor, I will give it a try and report back.

Thank you, Thank you.

I had a system coded which divided the map and the terrain into 16 parts that I related to a player/icon postion. Of course it was only accurate to the sector the player was in on the terrain. It took me a week to figure it out and plot it and 112 lines of code. It's a good thing that I don't work for NASA.

Re: Math is my anathema....or amathena [Re: JazzDude] #207905
05/23/08 01:05
05/23/08 01:05
Joined: Sep 2003
Posts: 733
Whitefish, Montana
JazzDude Offline OP
User
JazzDude  Offline OP
User

Joined: Sep 2003
Posts: 733
Whitefish, Montana
Beautiful. Replaces 112 lines of klutz kode with 12 elegant lines that work better!!

And I learned something new: "In C-Script we prefix the parameter name by a "&" in order to indicate that a parameter is a var pointer instead of a single number."

This snippet could also be used to locate a player on a heads up display in real time.

Very useful. Thanks again, Xarthor.



Last edited by JazzDude; 05/23/08 17:13. Reason: found error in code
Re: Math is my anathema....or amathena [Re: JazzDude] #207974
05/23/08 17:24
05/23/08 17:24
Joined: Sep 2003
Posts: 733
Whitefish, Montana
JazzDude Offline OP
User
JazzDude  Offline OP
User

Joined: Sep 2003
Posts: 733
Whitefish, Montana
It appears that the ratio function needs to be reversed.

Code:
function map_startup()
{
  map_ratio.x = terrain_size.x / map_size.x;
  map_ratio.y = terrain_size.y / map_size.y;
}
 


and with "zero" in the upper left corner, all y numbers are negative, so:
Code:
  icon_pos.x = _WorldPos[0]   / map_ratio.x;
  icon_pos.y = _WorldPos[1]  / -map_ratio.y;  


This seems to be working okay. I'll test some more.

Re: Math is my anathema....or amathena [Re: JazzDude] #208107
05/24/08 20:43
05/24/08 20:43
Joined: Oct 2004
Posts: 1,655
T
testDummy Offline
Serious User
testDummy  Offline
Serious User
T

Joined: Oct 2004
Posts: 1,655
:deleted:


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1