Jump to content


Photo

Ex3d - Coordinate Conversions, 3d Sprite Depth


  • Please log in to reply
16 replies to this topic

#1 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 17 November 2009 - 10:55 PM

Hi,

Ex3d is a dll i built that might be useful for making 3d games

it can convert 3d coordinates to 2d screen space and screen coordinates to 3d world coordinates.

it can calculate the depth of a 2d point calculated from a 3d one so that when a sprite is drawn at that depth it will overlap correctly with 3d objects (except the sprite will appear parallel to the screen instead of appearing like a vertical wall)

the dll also lets you do mesh picking

supports transformations

list of functions :

Ex3d_initDll
Ex3d_updateViewport
Ex3d_setAspectRatio
Ex3d_setProjection
Ex3d_setClippingPlanes
Ex3d_get2dCoord
Ex3d_get3dCoord
Ex3d_setTriangle
Ex3d_getScreenCoordTriangle
Ex3d_getRayTriangleIntersection
Ex3d_getIntersectionDist
Ex3d_calculateMouseRay
Ex3d_getRayOrig
Ex3d_getRayDir
Ex3d_getNormalizedVector
Ex3d_getTransformedRay
Ex3d_getX
Ex3d_getY
Ex3d_getZ
Ex3d_get3dPointDistance
Ex3d_addTranslation
Ex3d_addScaling
Ex3d_addRotation
Ex3d_clearTransformation
Ex3d_getDepth
Ex3d_freeDll

d3d_model_create2
d3d_model_primitive_begin2
d3d_model_vertex2
d3d_model_vertex_texture2
d3d_model_vertex_normal2
d3d_model_vertex_normal_texture2
d3d_model_vertex_color2
d3d_model_primitive_end2
d3d_model_destroy2
pickModelMouse
pickModelRay

(updated 22 / 11 / 09)

a good tool that would work well with this dll is blender2gml.
it allows you to make models in blender and export them
to gml code. and to ensure they are triangle meshes, you can first
export a .3ds model, import it and then export it using blender2gml

link :

http://host-a.net/doug25/Ex3d.zip (gm6, gm7)

Posted Image

Enjoy :)

Edited by doug25, 22 November 2009 - 06:10 AM.

  • 0

#2 Revel

Revel

    ɹǝqɯǝɯ ɔɯƃ

  • GMC Member
  • 4935 posts
  • Version:GM8

Posted 18 November 2009 - 02:18 AM

This dll looks extremely useful. However, I never do 3D development.

So this would allow you to "click" 3D objects within the game, right?


Again, it looks great!
  • 0

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 18 November 2009 - 05:57 AM

Please use a file host like skydrive or host-a.net. There is no download link in the page you refer. only ads to create an account.

Edited by icuurd12b42, 18 November 2009 - 05:57 AM.

  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#4 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 18 November 2009 - 12:15 PM

Please use a file host like skydrive or host-a.net. There is no download link in the page you refer. only ads to create an account.


sorry, the download link did look like an ad, i didn't realize there was a link either when i first used that site.

i've changed to host-a.net
  • 0

#5 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 18 November 2009 - 02:05 PM

So this would allow you to "click" 3D objects within the game, right?


yes :) , you can make a 3d model primitive as you would normally in gm code and let the dll know the triangles in the model that should be tested for clicks, i've added some functions (d3d_model_*2 and pickModel(id)) to make this easier
  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 18 November 2009 - 11:27 PM

I have a gripe with get3dCoords...

Change the aspect ratio of your projection so it dont match your screen/window and you will see what I mean.


You need to pass the aspect ratio to your Ex3d_setProjection function

Edited by icuurd12b42, 18 November 2009 - 11:28 PM.

  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#7 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 19 November 2009 - 01:57 AM

I have a gripe with get3dCoords...

Change the aspect ratio of your projection so it dont match your screen/window and you will see what I mean.


You need to pass the aspect ratio to your Ex3d_setProjection function


by calling Ex3d_setViewport(room_width, room_height..), this tells the dll to set the aspect ratio to room_width / room_height. but i don't think that's the problem.
what i've noticed is that when i change the room size, for some reason mouse_x is always from 0 to 640 inside the room and mouse_y 0 to 480. this means that the mouse doesn't correspond correctly with the viewport, know what i mean ?, so by modifying the code to Ex3d_get3dCoord(mouse_x*(room_width/640),mouse_y*(room_height/480),0); the correct 3d coordinate will be retrieved for whatever room dimension is set.

i don't know why mouse_x and mouse_y always output values of a 640 by 480 room ?
  • 0

#8 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 19 November 2009 - 04:30 AM

i don't know why mouse_x and mouse_y always output values of a 640 by 480 room ?


Because it matches the coords in the room as in top down. And since you are using view[0] to force the window to be 640x480, the mouse is always in the top left area of the room because you dont move the view arround the room arround in 3d...

the only mouse data you can use in 3d is the window mouse coords, like Yourself's script here

//CameraMouseOnGround (same arguments as d3d_set_projection_ext)

//This function is called every draw of the camera.

//You can used 
//global.placex 
//and 
//global.placey 
//to see where the mouse in the room relative to the camera
//So you can use those in global mouse click to detect which object you clicked
//more useful in top view mode
//Other useful values (use for ray tracing. not dependent on "On ground" information)
//  global.mouse_dx = x deviation;
//  global.mouse_dy = y deviation;
//  global.mouse_dz = z deviation;
 

/*
http://gmc.yoyogames.com/index.php?showtopic=111414&hl=\mouse+coordinate\
d3d_set_projection_ext(xfrom,yfrom,zfrom,xto,yto,zto,xup,yup,zup,angl
e,aspect) 
*/
var mm,dX,dY,dZ,uX,uY,uZ,vX,vY,vZ,mX,mY,mZ, width, height, tFOV;
  dX = argument3-argument0;
  dY = argument4-argument1;
  dZ = argument5-argument2;
  mm = sqrt(dX*dX+dY*dY+dZ*dZ);
  if mm = 0 exit;
  dX /= mm;
  dY /= mm;
  dZ /= mm;
  uX = argument6;
  uY = argument7;
  uZ = argument8;
  mm = uX*dX+uY*dY+uZ*dZ;
  uX -= mm*dX;
  uY -= mm*dY;
  uZ -= mm*dZ
  mm = sqrt(uX*uX+uY*uY+uZ*uZ);
  if mm = 0 exit;
  uX /= mm;
  uY /= mm;
  uZ /= mm;
  // v = u x d
  vX = uY*dZ-dY*uZ;
  vY = uZ*dX-dZ*uX;
  vZ = uX*dY-dX*uY;
  tFOV = tan(argument9*pi/360);
  uX *= tFOV;
  uY *= tFOV;
  uZ *= tFOV;
  vX *= tFOV*argument10;
  vY *= tFOV*argument10;
  vZ *= tFOV*argument10;
  width = window_get_width();
  height = window_get_height();
  var msx,msy;
  msx = window_mouse_get_x();
  msy = window_mouse_get_y();
  mX = dX+uX*(1-2*msy/height)+vX*(2*msx/width-1);
  mY = dY+uY*(1-2*msy/height)+vY*(2*msx/width-1);
  mZ = dZ+uZ*(1-2*msy/height)+vZ*(2*msx/width-1);
  mm = sqrt(mX*mX+mY*mY+mZ*mZ);
  if mm = 0 exit;
  
  global.mouse_dx = mX/mm;
  global.mouse_dy = mY/mm;
  global.mouse_dz = mZ/mm;
  if mZ = 0 exit;
  
	global.placex=argument0-argument2*mX/mZ
	global.placey=argument1-argument2*mY/mZ


That setViewport function is meaningless (as far as I know, unless you want to limit the calculation inside that box???) in your 3d stuff. only the aspect ratio is.

The only data you have to map 2d coords is the x,y position relative to the viewport on screen (the window, using window_mouse_get_x();
window_mouse_get_y();
and where the camera is located (x,y,z), pointing (direction vector, vx,vy,vz), it's lens angle and aspect ratio.

The z is interestingly used here too. but that's fine

You should have a functions to get the normalized vector dx,dy,dz relative to the mouse too, like the code above. That is useful for shooting stuff towards where the mouse points to.

The direction vector where the camara points too, we already know, but it would be usedful to get the normalized vector to that too.

Edited by icuurd12b42, 19 November 2009 - 04:35 AM.

  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#9 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 20 November 2009 - 08:11 AM

ah, i didn't realize a view was enabled :(

thanks for the input

i've made quite a few modifications

the reason for the setViewport function was really because D3DXVec3Project requires a D3DVIEWPORT9 passed to it, but i don't imagine this will be a problem having the viewport structure's width and height set to the view width and height, it seems to work well.

i've added these functions to provide more control over where the mouse is pointing as you suggested

Ex3d_calculateMouseRay
Ex3d_getRayOrig
Ex3d_getRayDir
Ex3d_getNormalizedVector
  • 0

#10 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 20 November 2009 - 10:32 AM

Once in a blue moon people may change the lens angle or the aspect ratio (which should usually be the same as you viewport on screen, not the view in room BTW, though usually they have the same values) in order to create some effects like say a sniper zoom. That is why you should not take the width and height of the view or room into consideration... I mean directly. Like I stated, you should provide a parameter to change those directly via a lens and aspect ratio parameter or function and you leave your users the responsibility to calculate it.
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#11 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 22 November 2009 - 06:32 AM

i've fixed some of the issues

now the viewport correctly matches the window, i originally thought that's what it sould be

the main thing that threw me about setting the viewport and aspect ratio was that i'd forgot the ortho projection was set to

d3d_set_projection_ortho(0,0,room_width,room_heigh
t,0);

i don't quite get what you mean by changing the view via a lens and aspect ratio ?
  • 0

#12 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 22 November 2009 - 07:23 AM

i've fixed some of the issues

now the viewport correctly matches the window, i originally thought that's what it sould be

the main thing that threw me about setting the viewport and aspect ratio was that i'd forgot the ortho projection was set to

d3d_set_projection_ortho(0,0,room_width,room_heigh
t,0);

i don't quite get what you mean by changing the view via a lens and aspect ratio ?


d3d_set_projection_ext(xfrom,yfrom,zfrom,xto,yto,z
to,xup,yup,zup,angle,aspect,znear,zfar)

well, the aspect ratio is usually portwidth/port height, where some set the aspect ratio with the view width and height, which is itself improper but works most of the time as the values set are usualy the same, or at least the calculation yeild the same the result, by chance...
eg

the view in room view is default to 640x480 and so is the port size.

640/480 = 1.333

People decide to change the port size to have a bigger screen and set (often) to 800x600
800/600 = 1.333

So the problem is lerking there...

What I suggest is passing the aspect ratio and not passing the width and height. But I guess it resolves as you calculate this anyway. My point is that some people changes that aspect ratio for special camera effect


The lensAngle is required in your calculations to convert mouse on screen to 3d vector. It's

argument9 : field of view (angle)


So I must have missed that...

Good job. I'll be sure to remember this little dll.

Edited by icuurd12b42, 22 November 2009 - 07:25 AM.

  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#13 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 22 November 2009 - 08:10 AM

i've made it so you can set the aspect ratio using setAspectRatio and the window (viewport) size can be set in game maker (window_set_rectangle, Ex3d_updateViewport(window_handle)), i think that should cater for the camera effects you are referring to ?

thanks for your help btw, i've quite enjoyed this wee project
  • 0

#14 equinox

equinox

    GMC Member

  • GMC Member
  • 123 posts

Posted 28 November 2009 - 01:58 PM

___________________________________________
ERROR in
action number 1
of Create Event
for object Camera:

Error defining an external function.
  • 0

#15 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 28 November 2009 - 03:28 PM

___________________________________________
ERROR in
action number 1
of Create Event
for object Camera:

Error defining an external function.


i downloaded the file and both the gm6 and gm7 are working for me,
did you extract the zip file ?
you need to extract it,

opening the files directly from the zip file means they are saved to a temp folder,
and this causes the working directory for the game to be different from the dll's directory.
the dll must be in the same folder as the gm6 / gm7 project files

does that solve the problem ?
  • 0

#16 equinox

equinox

    GMC Member

  • GMC Member
  • 123 posts

Posted 28 November 2009 - 05:57 PM

Yes,TNK1000. Problem resolvde.

Good work,good code :))
  • 0

#17 doug25

doug25

    GMC Member

  • New Member
  • 35 posts

Posted 28 November 2009 - 09:04 PM

ty :)
  • 0