Results 1 to 4 of 4

Thread: World To Screen

  1. #1
    Banned
    Join Date
    Jan 2011
    Posts
    13

    World To Screen

    Well I was never a prodigy at math, and I need help with the conversion from World to Screen based of the following concepts that I will elaborate on below.

    According to my tests for the FPS game that i'm hacking, the middle of the resolution of the game for a game whose resolution is 800x600 is 400x300. Corresponding to the middle of the screen is ALSO the players position. With this fact in hand I was able to formulate this picture (the player is at the origin):

    1.jpg

    I was then able to understand that your player will always be the center of the screen. Wherever you move, your field of view shifts to the right if you moved to the right or to the left if you moved to the left. Your players position doesn't move. It remains as the origin of the screen. You feel a sense of movement when your field of view shifts according to your movements and you're able to see objects which you weren't able to see in your previous field of view. With that in mind, if you drew a circle in the middle of the resolution it will always remain tagged on the players crosshairs which represents his position.

    Now, my problem was to determine the 2D coordinates based of the 3D coordinates I place in. In the next picture, I will include enemies.

    2.jpg

    IN this picture, you have two enemies. One is a bit to your right and the other is a bit to your left. My theory is that if your player is the origin of the screen regardless of where he moves and his 3D coordinates translate to the middle of the resolution, then its safe to say that since his coordinates are 400x300. Because of that, we can apply ratios to determine the 2D coordinates of enemy #1 and enemy #2 assuming that you have that information.

    So lets say that enemy #1 is located at a position in the game which was 3000, 2000 not including the z axis. WE can that the ratio of the difference between the enemy X and the player X must be the same ratio of the enemy screen X to player screeen X respectively.

    So assuming your player is located at 1000, 1500 in the game, the difference between the enemies X and the players X would be 2000. That ratio is 2X greater than the origin. We found that number with the following calculation:

    ( EnemyX - playerX ) / playerX = Ratio of how far the enemy is from us.

    Applying that ratio to our screen coordinates, we can then find our the enemies position based of the ratio with the following calculation:

    float Ratio = ( EnemyX - playerX ) / playerX;

    400 + ( 400 * Ratio ) = EnemyScreenX;

    After completing the calculation your enemy #1 position should according to my theory be at 600.

    The same process applies with Enemy #2 in determining his X position, and the same concept appies for solving 'y'.

    However, my coordinates never match up in game.

    Can you guys point out my mistakes? Thanks
    Last edited by iPromise; 01-20-2013 at 10:56 AM.

  2. #2
    Banned
    Join Date
    Jan 2011
    Posts
    13

    Re: World To Screen

    This is my code:

    Code:
    struct W2SPOINT 
    {
    	float x;
    	float y;
    	float z;
    };
    Code:
    void WorldToScreen ( int GameHeight, int GameWidth, W2SPOINT Target, W2SPOINT Player, W2SPOINT* Screen )
    {
    	// Obtain the Width/Height
    	float Height = (float) ( GameHeight / 2 );
    	float Width  = (float) ( GameWidth  / 2 );
    
    	// Determine the ratio
    	float RatioX = ( Target.x - Player.x ) / Player.x;
    	float RatioY = ( Target.y - Player.y ) / Player.y;
    
    	bla << RatioX << " " << RatioY << endl;
    
    	// Determine values
    	Screen->x = Width  + ( Width  * RatioX );
    	Screen->y = Height + ( Height * RatioY );
    
    	bla << Screen->x << " " << Screen->y << endl;
    
    	//la ( Screen->x );
    	//la ( Screen->y );
    }

  3. #3
    Banned
    Join Date
    Jan 2011
    Posts
    13

    Re: World To Screen

    Return values from the file:

    Code:
    -1.872 -0.621899
    -446.462 145.191
    -1.8157 -0.621899
    -417.64 145.191
    -1.81647 -0.621899
    -418.035 145.191
    -1.88607 -0.621899
    -453.668 145.191
    -1.88851 -0.621899
    -454.917 145.191
    0 0
    512 384
    -1.87384 -0.621622
    -447.408 145.297
    -1.81647 -0.621622
    -418.035 145.297
    -1.81728 -0.621622
    -418.448 145.297
    -1.88851 -0.621622
    -454.917 145.297
    -1.88939 -0.621622
    -455.366 145.297
    0 0
    512 384
    0 0.000188457
    512 384.072
    0 -0.00062867
    512 383.759
    -1.89255 -0.621622
    -456.986 145.297
    -1.89514 -0.621622
    -458.31 145.297
    -1.79437 -0.621622
    -406.716 145.297
    -1.7929 -0.621622
    -405.967 145.297
    -1.89584 -0.621622
    -458.671 145.297
    -1.89855 -0.621622
    -460.057 145.297
    -1.83141 -0.621622
    -425.682 145.297
    -1.83379 -0.621622
    -426.898 145.297
    -1.872 -0.621622
    -446.462 145.297
    -1.87569 -0.619019
    -448.355 146.297
    -1.81728 -0.619019
    -418.448 146.297
    -1.81809 -0.619019
    -418.862 146.297
    -1.88939 -0.619019
    -455.366 146.297
    -1.89026 -0.619019
    -455.815 146.297
    0 0
    512 384
    0 0.00706839
    512 386.714
    0 0.00624564
    512 386.398
    -1.89514 -0.619019
    -458.31 146.297
    -1.89772 -0.619019
    -459.633 146.297
    -1.7929 -0.619019
    -405.967 146.297
    -1.79143 -0.619019
    -405.213 146.297
    -1.89855 -0.619019
    -460.057 146.297
    -1.90126 -0.619019
    -461.443 146.297
    -1.83379 -0.619019
    -426.898 146.297
    -1.83616 -0.619019
    -428.115 146.297
    -1.87384 -0.619019
    -447.408 146.297
    -1.87754 -0.613873
    -449.301 148.273
    -1.81809 -0.613873
    -418.862 148.273
    -1.8189 -0.613873
    -419.275 148.273
    -1.89026 -0.613873
    -455.815 148.273
    -1.89114 -0.613873
    -456.264 148.273
    0 0
    512 384
    0 0.0142577
    512 389.475
    0 0.0134238
    512 389.155
    -1.89772 -0.613873
    -459.633 148.273
    -1.9003 -0.613873
    -460.956 148.273
    -1.79143 -0.613873
    -405.213 148.273
    -1.78996 -0.613873
    -404.459 148.273
    -1.90126 -0.613873
    -461.443 148.273
    -1.90396 -0.613873
    -462.829 148.273
    -1.83616 -0.613873
    -428.115 148.273
    -1.83854 -0.613873
    -429.331 148.273
    -1.87569 -0.613873
    -448.355 148.273
    0 0.0153265
    512 389.885
    0 0.0144756
    512 389.559
    -1.9003 -0.605991
    -460.956 151.3
    -1.90289 -0.605991
    -462.279 151.3
    -1.78996 -0.605991
    -404.459 151.3
    -1.78848 -0.605991
    -403.704 151.3
    -1.90396 -0.605991
    -462.829 151.3
    -1.90667 -0.605991
    -464.215 151.3
    -1.83854 -0.605991
    -429.331 151.3
    -1.84091 -0.605991
    -430.547 151.3
    -1.87754 -0.605991
    -449.301 151.3
    -1.87939 -0.605991
    -450.247 151.3
    -1.8189 -0.605991
    -419.275 151.3
    -1.8197 -0.605991
    -419.688 151.3
    -1.89114 -0.605991
    -456.264 151.3
    -1.89202 -0.605991
    -456.713 151.3
    0 0
    512 384
    0 0.0244754
    512 393.399
    0 0.0235999
    512 393.062
    -1.90289 -0.5946
    -462.279 155.673
    -1.90382 -0.5946
    -462.754 155.673
    -1.78848 -0.5946
    -403.704 155.673
    -1.78979 -0.5946
    -404.375 155.673
    -1.90667 -0.5946
    -464.215 155.673
    -1.90938 -0.5946
    -465.601 155.673
    -1.84091 -0.5946
    -430.547 155.673
    -1.84329 -0.5946
    -431.764 155.673
    -1.87939 -0.594601
    -450.247 155.673
    -1.88124 -0.594601
    -451.193 155.673
    -1.8197 -0.5946
    -419.688 155.673
    -1.82151 -0.5946
    -420.614 155.673
    -1.89202 -0.5946
    -456.713 155.673
    -1.8935 -0.5946
    -457.474 155.673
    0 0
    512 384
    0 0.0322355
    512 396.378
    0 0.0313296
    512 396.031
    -1.90382 -0.5805
    -462.754 161.088
    -1.90475 -0.5805
    -463.23 161.088
    -1.78979 -0.5805
    -404.375 161.088
    -1.79133 -0.5805
    -405.162 161.088
    -1.90938 -0.5805
    -465.601 161.088
    -1.91208 -0.5805
    -466.987 161.088
    -1.84329 -0.5805
    -431.764 161.088
    -1.84566 -0.5805
    -432.98 161.088
    -1.88124 -0.5805
    -451.193 161.088
    -1.88308 -0.5805
    -452.139 161.088
    -1.82151 -0.5805
    -420.614 161.088
    -1.82332 -0.5805
    -421.539 161.088
    -1.8935 -0.5805
    -457.474 161.088
    -1.89499 -0.5805
    -458.235 161.088
    0 0
    512 384
    0 0.0420926
    512 400.164
    0 0.041146
    512 399.8
    -1.90475 -0.561699
    -463.23 168.307
    -1.90567 -0.561699
    -463.705 168.307
    -1.79133 -0.561699
    -405.162 168.307
    -1.79287 -0.561699
    -405.95 168.307
    -1.91208 -0.561699
    -466.987 168.307
    -1.91479 -0.561699
    -468.373 168.307
    -1.84566 -0.561699
    -432.98 168.307
    -1.84804 -0.561699
    -434.197 168.307
    -1.88308 -0.561699
    -452.139 168.307
    -1.88493 -0.561699
    -453.085 168.307
    -1.82332 -0.561699
    -421.539 168.307
    -1.82513 -0.561699
    -422.465 168.307
    -1.89499 -0.561699
    -458.235 168.307
    -1.89648 -0.561699
    -458.996 168.307
    0 0
    512 384
    -0.0012215 -0.00198307
    511.375 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.85323 0.00134373
    -436.853 384.516
    -1.86334 0.00134373
    -442.03 384.516
    2.02393 0.00134373
    1548.25 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.733566 1.70841
    887.586 1040.03
    0.723662 1.68194
    882.515 1029.86
    1.68202 1.90751
    1373.19 1116.49
    1.68202 1.93466
    1373.19 1126.91
    -2.99356 0.00134373
    -1020.7 384.516
    -3.00662 0.00134373
    -1027.39 384.516
    -0.00122929 -0.00198307
    511.371 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.86334 0.00134373
    -442.03 384.516
    -1.87314 0.00134373
    -447.046 384.516
    2.02393 0.00134373
    1548.25 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.723662 1.68194
    882.515 1029.86
    0.714068 1.66487
    877.603 1023.31
    1.68202 1.93466
    1373.19 1126.91
    1.68202 1.93621
    1373.19 1127.5
    -3.00662 0.00134373
    -1027.39 384.516
    -3.01927 0.00134373
    -1033.87 384.516
    -0.00122929 -0.00198307
    511.371 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.87314 0.00134373
    -447.046 384.516
    -1.88293 0.00134373
    -452.061 384.516
    2.02393 0.00289063
    1548.25 385.11
    2.22553 0.00134373
    1651.47 384.516
    0.714068 1.66487
    877.603 1023.31
    0.704455 1.66487
    872.681 1023.31
    1.68202 1.93466
    1373.19 1126.91
    1.68202 1.91378
    1373.19 1118.89
    -3.01927 0.00134373
    -1033.87 384.516
    -3.03192 0.00134373
    -1040.34 384.516
    -0.00121362 -0.00198307
    511.379 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.88293 0.00134373
    -452.061 384.516
    -1.89336 0.00134373
    -457.4 384.516
    2.02393 0.00134373
    1548.25 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.704455 1.66487
    872.681 1023.31
    0.694222 1.66487
    867.442 1023.31
    1.68202 1.91378
    1373.19 1118.89
    1.68202 1.86437
    1373.19 1099.92
    -3.03192 0.00134373
    -1040.34 384.516
    -3.04538 0.00134373
    -1047.24 384.516
    -0.00121362 -0.00198307
    511.379 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.89336 0.00134373
    -457.4 384.516
    -1.90379 0.00134373
    -462.739 384.516
    2.03085 0.00134373
    1551.8 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.694222 1.66487
    867.442 1023.31
    0.683989 1.66487
    862.203 1023.31
    1.68202 1.86437
    1373.19 1099.92
    1.68202 1.7878
    1373.19 1070.52
    -3.04538 0.00134373
    -1047.24 384.516
    -3.05885 0.00134373
    -1054.13 384.516
    -0.00122929 -0.00198307
    511.371 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.90379 0.00134373
    -462.739 384.516
    -1.91358 0.00134373
    -467.755 384.516
    2.03759 0.00134373
    1555.24 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.683989 1.66487
    862.203 1023.31
    0.674377 1.66487
    857.281 1023.31
    1.68202 1.7878
    1373.19 1070.52
    1.68202 1.69035
    1373.19 1033.09
    -3.05885 0.00134373
    -1054.13 384.516
    -3.07149 0.00134373
    -1060.6 384.516
    2.03085 0.00134373
    1551.8 384.516
    -0.00121362 -0.00198307
    511.379 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.91358 0.00134373
    -467.755 384.516
    -1.92401 0.00134373
    -473.094 384.516
    2.03759 0.00134373
    1555.24 384.516
    2.04806 0.00134373
    1560.61 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.674377 1.66487
    857.281 1023.31
    0.664144 1.63935
    852.042 1013.51
    1.68202 1.69035
    1373.19 1033.09
    1.68202 1.56108
    1373.19 983.454
    -3.07149 0.00134373
    -1060.6 384.516
    -3.08496 0.00134373
    -1067.5 384.516
    -0.0012215 -0.00198307
    511.375 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.92401 0.00134373
    -473.094 384.516
    -1.93412 0.00134373
    -478.271 384.516
    2.04806 0.00134373
    1560.61 384.516
    2.05835 0.00134373
    1565.88 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.664144 1.63935
    852.042 1013.51
    0.654221 1.61464
    846.961 1004.02
    1.68202 1.56108
    1373.19 983.454
    1.68202 1.40938
    1373.19 925.201
    -3.08496 0.00134373
    -1067.5 384.516
    -3.09801 0.00134373
    -1074.18 384.516
    -0.00121362 -0.00198307
    511.379 383.238
    0.454819 0.0371324
    744.867 398.259
    -1.93412 0.00134373
    -478.271 384.516
    -1.94455 0.00134373
    -483.61 384.516
    2.05835 0.00134373
    1565.88 384.516
    2.06896 0.00134373
    1571.31 384.516
    2.22553 0.00134373
    1651.47 384.516
    0.654221 1.61464
    846.961 1004.02
    0.643988 1.59674
    841.722 997.15
    1.68202 1.40938
    1373.19 925.201
    1.68202 1.22659
    1373.19 855.011
    -3.09801 0.00134373
    -1074.18 384.516
    -3.11148 0.00134373
    -1081.08 384.516
    -0.0020231 -0.00520079
    510.964 382.003
    -0.00208012 -0.00520079
    510.935 382.003
    -1.88736 -0.00188472
    -454.33 383.276
    -1.88762 -0.00188472
    -454.46 383.276
    -1.13836 -0.00188472
    -70.8414 383.276
    -1.14125 -0.00188472
    -72.321 383.276
    -0.123986 -0.00188472
    448.519 383.276
    -0.944307 -0.00188472
    28.515 383.276
    -0.946832 -0.00188472
    27.2222 383.276
    0.0784881 -0.00188484
    552.186 383.276
    0.0773172 -0.00188484
    551.586 383.276
    -0.0242702 -0.00188472
    499.574 383.276
    -0.0220151 -0.00188472
    500.728 383.276
    -1.86561 -0.00188472
    -443.19 383.276
    -1.86532 -0.00188472
    -443.045 383.276
    -0 0
    512 384
    -0.00198115 -0.00520079
    510.986 382.003
    -0.00203599 -0.00520079
    510.958 382.003
    -1.88614 -0.00188472
    -453.702 383.276
    -1.88638 -0.00188472
    -453.828 383.276
    -1.14102 -0.00188472
    -72.2005 383.276
    -1.1438 -0.00188472
    -73.623 383.276
    -0.125447 -0.00188472
    447.771 383.276
    -0.94692 -0.00188472
    27.1768 383.276
    -0.949347 -0.00188472
    25.9341 383.276
    0.0755209 -0.00188484
    550.667 383.276
    0.0743952 -0.00188484
    550.09 383.276
    -0.0236458 -0.00188472
    499.893 383.276
    -0.0214779 -0.00188472
    501.003 383.276
    -1.86388 -0.00188472
    -442.307 383.276
    -1.86361 -0.00188472
    -442.167 383.276
    -0 0
    512 384
    -0.00182818 -0.00520079
    511.064 382.003
    -0.00188293 -0.00520079
    511.036 382.003
    -1.885 -0.00188472
    -453.122 383.276
    -1.88525 -0.00188472
    -453.247 383.276
    -1.14357 -0.00188472
    -73.5085 383.276
    -1.14635 -0.00188472
    -74.9289 383.276
    -0.126807 -0.00188472
    447.075 383.276
    -0.949426 -0.00188472
    25.8937 383.276
    -0.95185 -0.00188472
    24.6529 383.276
    0.0727239 -0.00188484
    549.235 383.276
    0.0716 -0.00188484
    548.659 383.276
    -0.023 -0.00188472
    500.224 383.276
    -0.0208354 -0.00188472
    501.332 383.276
    -1.86226 -0.00188472
    -441.48 383.276
    -1.86199 -0.00188472
    -441.341 383.276
    -0 0
    512 384
    -0.001815 -0.00520079
    511.071 382.003
    -0.00187174 -0.00520079
    511.042 382.003
    -1.88375 -0.00188472
    -452.479 383.276
    -1.884 -0.00188472
    -452.609 383.276
    -1.1461 -0.00188472
    -74.8019 383.276
    -1.14897 -0.00188472
    -76.2744 383.276
    -0.128287 -0.00188472
    446.317 383.276
    -0.951931 -0.00188472
    24.6111 383.276
    -0.954444 -0.00188472
    23.3247 383.276
    0.0697841 -0.00188484
    547.729 383.276
    0.068619 -0.00188484
    547.133 383.276
    -0.0224947 -0.00188472
    500.483 383.276
    -0.0202506 -0.00188472
    501.632 383.276
    -1.86053 -0.00188472
    -440.593 383.276
    -1.86025 -0.00188472
    -440.449 383.276
    -0 0
    512 384
    -0.00184226 -0.00520079
    511.057 382.003
    -0.00189672 -0.00520079
    511.029 382.003
    -1.88247 -0.00188472
    -451.827 383.276
    -1.88272 -0.00188472
    -451.951 383.276
    -1.14872 -0.00188472
    -76.1426 383.276
    -1.15148 -0.00188472
    -77.558 383.276
    -0.129793 -0.00188472
    445.546 383.276
    -0.954523 -0.00188472
    23.2844 383.276
    -0.956938 -0.00188472
    22.0478 383.276
    0.0667726 -0.00188484
    546.188 383.276
    0.0656526 -0.00188484
    545.614 383.276
    -0.0219434 -0.00188472
    500.765 383.276
    -0.0197862 -0.00188472
    501.869 383.276
    -1.85877 -0.00188472
    -439.688 383.276
    -1.8585 -0.00188472
    -439.55 383.276
    -0 0
    512 384
    -0.00183613 -0.00520079
    511.06 382.003
    -0.00189257 -0.00520079
    511.031 382.003
    -1.88122 -0.00188472
    -451.185 383.276
    -1.88147 -0.00188472
    -451.314 383.276
    -1.15122 -0.00188472
    -77.4265 383.276
    -1.15409 -0.00188472
    -78.8939 383.276
    -0.131269 -0.00188472
    444.79 383.276
    -0.957011 -0.00188472
    22.0104 383.276
    -0.959515 -0.00188472
    20.7284 383.276
    0.0638454 -0.00188484
    544.689 383.276
    0.0626843 -0.00188484
    544.094 383.276
    -0.0214485 -0.00188472
    501.018 383.276
    -0.0192121 -0.00188472
    502.163 383.276
    -1.85704 -0.00188472
    -438.804 383.276
    -1.85676 -0.00188472
    -438.661 383.276
    -0 0
    512 384
    -0.00187265 -0.00520079
    511.041 382.003
    -0.00192693 -0.00520079
    511.013 382.003
    -1.87995 -0.00188472
    -450.533 383.276
    -1.88019 -0.00188472
    -450.657 383.276
    -1.15382 -0.00188472
    -78.7573 383.276
    -1.15658 -0.00188472
    -80.168 383.276
    -0.132773 -0.00188472
    444.02 383.276
    -0.959585 -0.00188472
    20.6925 383.276
    -0.961992 -0.00188472
    19.4601 383.276
    0.0608444 -0.00188484
    543.152 383.276
    0.0597281 -0.00188484
    542.581 383.276
    -0.0209103 -0.00188472
    501.294 383.276
    -0.0187604 -0.00188472
    502.395 383.276
    -1.85528 -0.00188472
    -437.901 383.276
    -1.85501 -0.00188472
    -437.763 383.276
    -0 0
    512 384

  4. #4
    Senior Member Jordan's Avatar
    Join Date
    Dec 2010
    Location
    Australia m8
    Posts
    104

    Re: World To Screen

    I'm a little confused by what I see here. So I'm going to try and answer some questions, but ask my own.

    Is this a 3D shooter? Or an overhead 2D shooter? (Silly question, but I thought I'd get that out of the way).

    Typically, most 3D games already contain the players X/Y/Z coordinates, and typically, the "least" you can get to is 0,0,0.

    It seems like you're trying to use the current VIEW of the player to determine where an enemy is. Which as I said before confuses me. - Can you get a screenshot of the game, or something so we've got something better to judge it from?

    Cheers!
    Main PC: i7 860 Quad Core 2.8GHz. 8GB DDR3 @ 1666mhz. 1GB nVidia GeForce GT 220. 2 x 1TB Samsung HD103SJ. Win 7 x64. Thermaltake Element V.

    Server: Pentium 4 HT 2.8GHz, 3 GB DDR @ 400MHz, Not important, 320GB WD3200AAKS, 1TB WD10EARX, 500GB Segate ST3500

    Laptop: i7 3667U @ 2.5GHz, 8GB DDR3 @ 800MHz, NVidia GeForce 710M, 1TB Segate ST1000, Touch Screen

    http://kingsley-muir.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •