Placing 3d objects on a rotating/tilting map

Blitz3D Forums/Blitz3D Beginners Area/Placing 3d objects on a rotating/tilting map

hockings(Posted 2006) [#1]
This is my first attempt at a 3d project so bear with me (the concepts are taking a while to sink in :D

I'm programming a racing game. My "map" on which I build my track is an 8x8 grid. On this grid I plan to put track pieces (each made of 3 meshes - a top then two sides which are the support beams) and these pieces are created dynamically depending what the player picks.

So far I have my map which I can rotate and zoom and select individual squares on. I also have track pieces I can create (push a button and I get my 3 dynamic meshes created). What I can't figure out is this...

If the map was stationary, putting a piece at an X,Y location and elevating it a bit to put it "on" the map (rather than "in" or "underneath" it) would be easy. Instead though, my map can be tilted and rotated. I want to know how I can rotate/tilt my track, then when I select a new piece have it end up in the correct spot.
The yaw/tilt/roll I figure I match to whatever the map is, but I can no longer just add 1 to the Z location on the map to make sure the piece ends up "on" the map (rather than in or underneath it). If the map has tilted towards the player, the piece now needs to be moved +Z and -Y from where it started to be put "on" the map. It might also have rotated which further changes X, Y and Z coords.

1) Any takers on how I can work out where to put my piece so it's 1 3D-space unit above the map regardless of how the map has tilted/rotated? It will be a vector command of some description, but I'm not sure what.
2) Also, rather than having to move up to 64 pieces every time I manipulate the map, can I "glue" the piece to the map so rotating the map correctly places the pieces? I'd need to be able to "unglue" the piece afterwards if the player deleted it.

Thanks a bunch people!!!


SoggyP(Posted 2006) [#2]
Hello.

I'm not too conversant with 3d side of things and will probably be shot down for the following:

2) Parent the pieces to the main map.
1) Could you have 64 individual map sections (again parented to a main map pivot so they all rotate/tilt together) and use a collision/pick command to select which one you're editing?

Just suggestions.

Best of luck.

Goodbye.


hockings(Posted 2006) [#3]
Thanks for the reply soggyp.
The map sections are individual sections, and I've managed to get it so you can select an individual tile out of the 64. They pivot together nicely, but they are all part of the same mesh so that makes that easy.

I can't figure out how to parent other meshes to the main map mesh (such that they can be deleted later if needed), and how to place one on the other at the right location.

I'm sure this'll all click once I've done it once :D


b32(Posted 2006) [#4]
You can calculate the right position by using TFormPoint:

TFormPoint 0, 1, 0, scene, 0
means: transform point (0,1,0) from "scene" to "0" (=global world).
(0,0,0) is the objects position itself. Any rotations/scaling is taking in account using TFormPoint.
After that, read TFormedX(), TFormedY() and TFormedZ() to get the transformed position:
PositionEntity obj, TFormedX(), TFormedY() and TFormedZ()

You can schtick objects to the scene using EntityParent:
EntityParent obj, scene
To detach them, use EntityParent obj, 0 (<-global world)


SoggyP(Posted 2006) [#5]
Hello.

Ok, how about having 64 pivots 1 unit above each block parented to the block so that they'll rotate and tilt with it, then parenting the new block to that?

Also, you might want to check out the local and world(?) values of the objects and also the pitch, yaw and roll functions. I think I've got that right :o)

Goodbye.


Stevie G(Posted 2006) [#6]
It as easy as ....

entityparent section, map
rotateentity section, 0, 0, 0
positionentity section, Xpos, 1, Zpos

Where Xpos and Zpos should already be known depending on which grid square is being changed. How exactly are you determining which part of the 8x8 grid is being edited/changed? CameraPicking?

All sections will move with the map when rotated.

Stevie


hockings(Posted 2006) [#7]
Thanks for the replys guys.

I'm camera picking to determine which square on the 8x8 grid is being acted on (and that bit's working fine).

Thanks for the info on entityparent and tformpoint - I'll have a play with them tonight and see how it goes.

Stevie G - Will positionentity X,1,Z work if the plane is pointing towards you at a 45 degree angle for example? (ie to get the piece 1 unit above the ground but perpendicular to it, it would need to go a little bit up and a little towards you, not just 1 unit straight up)


Stevie G(Posted 2006) [#8]

Stevie G - Will positionentity X,1,Z work if the plane is pointing towards you at a 45 degree angle for example? (ie to get the piece 1 unit above the ground but perpendicular to it, it would need to go a little bit up and a little towards you, not just 1 unit straight up)



Yes, because you're positioning it within the maps local space, rather than global. It therefore takes into consideration the map's rotation when positioning.

No need for tform commands here.

Stevie


hockings(Posted 2006) [#9]
Cheers Stevie!!! I'll give it a whirl.


hockings(Posted 2006) [#10]
Thanks Stevie G, that worked perfectly. Much appreciated.

Loving the look of your verlet physics engine test game - looking forward to the final product!


hockings(Posted 2006) [#11]
Any takers on how I rotate a mesh around its centre rather than 0,0,0? I'm looking through the docs for something like mesh origin but can't find anything that looks right.

*EDIT* CreatePivot seems to be heading in the right direction, but doesn't look quite right...


b32(Posted 2006) [#12]
Objects rotate around their own centre, so their own (0, 0, 0). To change this (on static objects), use PositionMesh/FitMesh. For animated objects, I believe a pivot it best. There is a routine in the archives: http://www.blitzbasic.com/codearcs/codearcs.php?code=732
or you could use this:
Function CenterMesh(mesh)

	a_minx# = 100000
	a_maxx# = -100000
	a_miny# = 100000
	a_maxy# = -100000
	a_minz# = 100000
	a_maxz# = -100000

	validmesh = (mesh <> 0)
	If validmesh Then validmesh = EntityClass$(mesh) = "Mesh"
	If validmesh Then
		For i = 1 To CountSurfaces(mesh)
			s = GetSurface(mesh, i)
			For v = 0 To CountVertices(s) - 1
				TFormPoint VertexX(s, v), VertexY(s, v), VertexZ(s, v), mesh, 0
				If TFormedX() < a_minx Then a_minx = TFormedX()
				If TFormedX() > a_maxx Then a_maxx = TFormedX()
				If TFormedY() < a_miny Then a_miny = TFormedY()
				If TFormedY() > a_maxy Then a_maxy = TFormedY()
				If TFormedZ() < a_minz Then a_minz = TFormedZ()
				If TFormedZ() > a_maxz Then a_maxz = TFormedZ()
			Next
		Next
	End If
	
	PositionMesh mesh, -(a_minx+a_maxx)/2, -(a_miny+a_maxy)/2, -(a_minz + a_maxz)/2
	
End Function	



Stevie G(Posted 2006) [#13]
Hockings, I'm a wee bit pissed but ...

Mw# = messhwidth( mymesh )
Mh# = meshheight( mymesh )
Md# = meshdepth( mymesh )
fitmesh mymesh, -.5*mw, -.5*mh, -.5*md, mw,mh,md

Will recenter your mesh a wee bit quicker than the above ;)

Stevie


hockings(Posted 2006) [#14]
b32 and Stevie G, thanks muchly!!!! This 3d stuff is a bit hard to get your head around.


hockings(Posted 2007) [#15]
I've just tried Stevie G's method and it didn't work - any thoughts while I try b32's method?

My mesh is a flat grid (effectively a plane) with vertex's in the X and Z coords, height is 0.

FitMesh with a height of 0 completes, but when you then do a MeshHeight on the Mesh, the debugger returns "NaN" rather than 0. I've tried hard coding the height to either 0 or 1, but it keeps happening. Thoughts?

In the meantime I might try PositionMesh (Mesh, -0.5*Width, -0.5*Height,-0.5*Depth).