Highlighting objects problem (using TFormVector)

Blitz3D Forums/Blitz3D Programming/Highlighting objects problem (using TFormVector)

Ghost Dancer(Posted 2005) [#1]
I have a strange problem with the setBox function below. Basically, when the player clicks on a unit in my game, the setBox function determines the 4 corners of the entity, and the drawRectCorners function draws lines at these 4 points to highlight the unit.

However, the highlighting seems to be much bigger than the entity. You can see what I mean below:


Now here is the strange thing - if I replace the 3d models with cubes (using createCube), it works pefectly which indicates that the code is correct and maybe it is the models that are the problem (but the models seem fine to me).

Any help would be most appreciated.


Function setBox(obj)
;calculate corners (screen coords) of entity

	map\boxW#  = MeshWidth(obj)
	map\boxH# = MeshHeight(obj)
	depth#  = MeshDepth(obj)

	width2# = map\boxW# / 2
	height2# = map\boxH# / 2
	depth2# = depth# / 2
	
	CenterX# = EntityX(obj)
	CenterY# = EntityY(obj)
	CenterZ# = EntityZ(obj)
	
	map\boxX# = scrnWidth
	map\boxY# = scrnHeight
	x2# = 0
	y2# = 0
	
	TFormVector -width2#, +height2#, +depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(0,0) = ProjectedX()
	Points(0,1) = ProjectedY()
	
	TFormVector +width2#, +height2#, +depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(1,0) = ProjectedX()
	Points(1,1) = ProjectedY()
	
	TFormVector -width2#, +height2#, -depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(2,0) = ProjectedX()
	Points(2,1) = ProjectedY()
	
	TFormVector +width2#, +height2#, -depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(3,0) = ProjectedX()
	Points(3,1) = ProjectedY()
	
	TFormVector -width2#, -height2#, +depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(4,0) = ProjectedX()
	Points(4,1) = ProjectedY()
	
	TFormVector +width2#, -height2#, +depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(5,0) = ProjectedX()
	Points(5,1) = ProjectedY()
	
	TFormVector -width2#, -height2#, -depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(6,0) = ProjectedX()
	Points(6,1) = ProjectedY()
	
	TFormVector +width2#, -height2#, -depth2#, obj, 0
	CameraProject cam\camera, CenterX#+TFormedX#(), CenterY#+TFormedY#(), CenterZ#+TFormedZ#()
	Points(7,0) = ProjectedX()
	Points(7,1) = ProjectedY()

	;set box coords & size
	For n = 0 To 7
		If Points(n,0) < map\boxX# Then map\boxX# = Points(n,0)
		If Points(n,1) < map\boxY# Then map\boxY# = Points(n,1)
		If Points(n,0) > x2# Then x2# = Points(n,0)
		If Points(n,1) > y2# Then y2# = Points(n,1)
	Next
	
	;calculate width & height of box
	map\boxW#  = x2# - map\boxX#
	map\boxH# = y2# - map\boxY#

	;add offset of camera view
	map\boxX# = map\boxX# + cam\viewX
	map\boxY# = map\boxY# + cam\viewY
End Function

Function drawRectCorners(x, y, w, h, size = 10, lw = 2)
;draws corners of a rectangle

	xw = x + w
	yh = y + h
	
	;top left corner
	Rect x, y, size, lw, True
	Rect x, y, lw, size, True

	;top right corner
	Rect xw - size + lw, y, size, lw, True
	Rect xw, y, lw, size, True

	;bot right corner
	Rect xw - size + lw, yh, size, lw, True
	Rect xw, yh - size + lw, lw, size, True

	;bot left corner
	Rect x, yh, size, lw, True
	Rect x, yh - size + lw, lw, size, True
End Function




Neochrome(Posted 2005) [#2]
nice graphics!!
although i cant help with the vector stuff :(

you might need to count children
if your meshes have more than one "object" per mesh
i had this problem.


Picklesworth(Posted 2005) [#3]
What I would do, is to test it with 3d primitive objects, such as cubes, and see how they behave.


Yah, that looks great. And that screenshot just solidified an idea of mine :)


Ghost Dancer(Posted 2005) [#4]
Neomancer - I've tried your suggestion of countChildren and I have discovered a new problem. Now this might be related to my original problem, or it might just be that I don't understand enough about 3d models ;)

The models are .3ds files which I have purchased. Looking at them in Milkshape I can see that there are 2 groups - body & turret - I would assume that the turret would be a child object of body, but Blitz does not detect any children when I do countChildren.

Like I said, a lot of this 3D stuff is new to me, so is this just my lack of knowledge or is it a problem with the models? I will need to sort out this child problem since I want the turrets to turn independantly to the body.


Mr Picklesworth - yes, I've tried using cubes instead of the models and it works fine!


Rhyolite(Posted 2005) [#5]
You will need to use 'LoadAnimMesh' to access (or count) children.

No idea about your 'select' problem, except 'maybe' the cube is rotated at 45 degrees or something weird - rather than being aligned correctly with the model??? I always use .b3d models as have had various problems with other types (not that that will help you - sorry!).

Looking nice :)

Rhy

EDIT: Search the code archives for 'Child'. Their is at least one useful function which will recursively return all child entities of an object. It took me a while to get my head around model heirachies, so suggest you do some searches and reading up on the subject ;)


Ghost Dancer(Posted 2005) [#6]
Thanks Rhyolite. I've tried LoadAnimMesh - but it I lose all the textures, scales etc. of the models. I think I need to read up on some of that ;)


Rhyolite(Posted 2005) [#7]
You will have to apply operations either to the 'root node' of the heirarchic model or directly to one of its children. Also, you may have to load and apply textures manualy (if not doing so already, I cant even remember if 3DS automaticaly loads textures with model?).

Basicaly, a heirarchic model has a tree like structure starting with the 'trunk'. It may be a useful exercise to write a test program which simply parses the model structure and prints all the child entity names to the screen. You could then modify it to show parent/child relationships. Once you have done that, you are well on the way ;)

All the best,
Rhy

PS - here is the child entity function. There are others, but this is the one I use (credit to Beaker).

http://www.blitzbasic.com/codearcs/codearcs.php?code=796


big10p(Posted 2005) [#8]
If your models have been scaled (using ScaleEntity), then your vectors get scaled when TForming from model space to world space (I think - been a while since I've messed with 3D). Try using ScaleMesh instead as this scales the actual vertex positions and not the coord system itself.

Also, I think you may want to use TFormPoint, rather than TFormVector.


Neochrome(Posted 2005) [#9]
Long shot, but could you export the models as native Blitz ? .b3d


Ghost Dancer(Posted 2005) [#10]
I've converted a model to b3d which seems to have solved the texturing problem.

When I load the 3ds models into Milkshape, some of the child objects are displaced (e.g. the turret is floating above the body) - I guess this is either a problem with Milkshape or the models themselves, but a bit of tweeking and exporting to b3d should solve some of my problems :-)

When I have sorted this out I can get back to the original problem but one thing at a time...


lo-tekk(Posted 2005) [#11]
Looks nice !
I had problems with local coordinates after scaleing entities within blitz. So it's always better to scale your models in your external modeller as needed. Btw. there is a boxmesh code in the codearchives that outline your mesh rectangular.


------------------------
www.moonworx.de


Ghost Dancer(Posted 2005) [#12]
Thanks lo-tekk, I'll bear that in mind. Looks like I need to spend some time tweeking all the models.