PDA

View Full Version : TBGL_EntityGetPos



misthema
28-02-2008, 05:02
I don't get it... I use this function like it should be used, but it just gives me an error every time, that reads:

Error code: 254
Error description: Expected a variable but found something else.

And the code line is:


TBGL_ENTITYGETPOS(%MY_WORLD, %PLAYER, X, Y, Y)

The variables, that won't work on that, are defined like this:


DIM X As Long
DIM Y As Long
DIM Z As Long

Please tell me what's the problem... :'(



misthema

Michael Hartlef
28-02-2008, 07:06
The only thing I see here is that X,Y,Z should be single or double instead of long.

And welcome on the board. :)

ErosOlmi
28-02-2008, 09:00
Ciao misthema.

What version of thinBasic do you have installed? For latest TBGL module functionalities you need latest thinBasic preview version 1.6.0.1
I've attached a TBGL example recently developed by Petr to show entities data. Can you check it is working for you?
Another thing to check is that X, Y, Z are declared before they are used in TBGL_ENTITYGETPOS function. Even if they are declared LONG TBGL_ENTITYGETPOS whould work (but of course can get strange results in some cases).

Ciao
Eros

misthema
28-02-2008, 15:26
Hi,

thanks Michael. :)

I got that problem fixed, it works now. But now I have another problem... :D
How can I get my object to point at mouse?

I think the problem is that mouse has 2D co-ordinates, and object has 3D.
But can it still be able to make?

Here's my project source I'm working on:




USES "TBGL"
USES "Math"

Dim hWnd As Dword = TBGL_CreateWindowEx( "NetMatch2", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
DIM cn9 AS DWORD = TBGL_FontHandle("Courier New", 9) 'Using Courier New 'coz I like console-like fonts :)
TBGL_BuildFont( cn9 )

'----- EQEATES
'-- Scene
%MY_WORLD = 1

'-- Objects...
%PLAYER = 1
%CAM = 2
%LIGHT1 = 3


'----- VARIABLES
DIM Message As String
DIM a As Integer
DIM FrameRate As Long
DIM x As Integer = 0
DIM y As Integer = 0
DIM z As Integer = 2
DIM PlayerX As Single
DIM PlayerY As Single
DIM MouseX As Single
DIM MouseY As Single


'----- GLOBALS
Global Angle as Double


'----- THE SCENE
TBGL_SceneCreate(%MY_WORLD)

'----- LIGHTS
TBGL_EntityCreateLight(%MY_WORLD, %LIGHT1)
TBGL_EntitySetPos(%MY_WORLD, %LIGHT1, 15, 10, 5)

'----- CAMERA
TBGL_EntityCreateCamera %MY_WORLD, %CAM
TBGL_EntitySetPos(%MY_WORLD, %CAM, 0, 15, 0)
TBGL_EntitySetTargetPos(%MY_WORLD, %CAM, 0, 0, 0)

'----- "PLAYER"
TBGL_EntityCreateBox( %MY_WORLD, %PLAYER, 0, 1, 1, 3 )
TBGL_EntitySetPos(%MY_WORLD,%PLAYER, 1,0,1)


WHILE TBGL_IsWindow(hWnd)

FrameRate = TBGL_GetFrameRate
TBGL_EntityGetPos(%MY_WORLD, %PLAYER, PlayerX, z, PlayerY, %TRUE)
MouseX = TBGL_MouseGetPosX/100-3.20
MouseY = TBGL_MouseGetPosY/100-2.40

TBGL_ClearFrame
TBGL_SceneRender(%MY_WORLD)
TBGL_UseLIGHTing 1

TBGL_EntityTurn %MY_WORLD, %PLAYER, 0, GetAngle(PlayerX,PlayerY, MouseX,MouseY), 0

TBGL_UseLIGHTing 0
TBGL_Color 50,255,50
TBGL_PrintFont "FPS:"+STR$(FrameRate), 8,0,5.9
TBGL_PrintFont "MouseX: "+STR$(MouseX), 8,0,5.4
TBGL_PrintFont "MouseY: "+STR$(MouseY), 8,0,4.9
TBGL_PrintFont "PlayerX: "+STR$(PlayerX), 8,0,4.4
TBGL_PrintFont "PlayerY: "+STR$(PlayerY), 8,0,3.9
TBGL_PrintFont "Angle: "+STR$(GetAngle(PlayerX,PlayerY, MouseX,MouseY)), 8,0,3.4

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While


WEND


'**************************************************
'********** FUNCTIONS *****************************

Function GetAngle(x1 As Double, y1 As Double, x2 As Double, y2 As Double)
Return Angle = ATAN2((y2-y1),(x2-x1))
End Function

Michael Hartlef
28-02-2008, 16:14
Hi,

I'm at work right now but will look into this tonight. There is a function where can get the 3D coordinates from a 2D coordinate. Don't remember its name.

Michael

misthema
28-02-2008, 16:47
Hi,

I don't think that there's that kind of function, but I found this, that does the same, but backwards: TBGL_Pos3DtoPos2D( x, y, z, px, py )

It changes 3D position co-ordtinates to 2D and I don't think this's useful in this...

ErosOlmi
28-02-2008, 16:57
misthema,

see attached script example if can help.
It maps 2D mouse position to 3D objects but not using TBGL entitities.

Example is included in TBGL Bonus Pack you can get in thinBasic download area at http://www.thinbasic.com/index.php?option=com_docman&task=cat_view&gid=23&Itemid=66

Ciao
Eros

Petr Schreiber
28-02-2008, 17:57
Hi Misthema,

I apology for late reply, but exclusively on Thursdays I am not online before 18:00 usually.

Solution to get 3D position from mouse is in TBGL_GetPixelInfo statement.
Declare variables before main loop:


dim Pos3Dx, Pos3Dy, Pos3Dz as double


After TBGL_SceneRender put following:


TBGL_GetPixelInfo TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, Pos3Dx, Pos3Dy, Pos3Dz


... and to print on screen something like:


TBGL_PrintFont "Mouse3D X: "+format$(Pos3Dx, "#.00"), 8,0, 0.0
TBGL_PrintFont "Mouse3D Y: "+format$(Pos3Dy, "#.00"), 8,0,-0.5
TBGL_PrintFont "Mouse3D Z: "+format$(Pos3Dz, "#.00"), 8,0,-1.0


Hope it helps,
Petr

P.S. Thanks thanks thanks to all who helped! :-[

Petr Schreiber
28-02-2008, 19:20
Just read your question more precisely :)

To make one entity look at some position, you do not have to do the math yourself, just use the Force TBGL_EntitySetTargetPos.

I attached modified script for you, as you move mouse, "player" will look at it.

Good luck with your project, and please ask if something else is not clear.


Bye,
Petr

misthema
28-02-2008, 20:58
Oh my god! Thank you all so much!

Now I got it working properly and I can continue with it! Yay! \o/