PDA

View Full Version : Trigonometry Problem...



matthew
13-03-2007, 22:17
Hi everyone, I'm having a small problem with Trigonometry. ;)


TBGL_Vertex (A * sin(j * 10) * cos(i *10)), (A * sin(j * 10) * sin(i * 10)), (A * cos(j * 10))

In thinBASIC the Trig Commands return the Answer in Radians but I need this Routine to return Degrees.

I've tried using the 'RadToDeg' Command but I got in a bit of a mess. :D

Can you help? :)

Petr Schreiber
13-03-2007, 22:25
Hi matthew,

if I understand it ok - COS, SIN, ... work with radians as input.

If you mean j*10 to be number in degrees, then you don't need RadToDeg, but DegToRad :)

So maybe something like that ?:


TBGL_Vertex (A * sin(DegToRad(j * 10)) * cos(DegToRad(i *10))), _
(A * sin(DegToRad(j * 10)) * sin(DegToRad(i * 10))), _
(A * cos(DegToRad(j * 10)))


To make it maybe little bit faster and more clear I would suggest following for this case:


tempAngle1 = DegToRad(j * 10)
tempAngle2 = DegToRad(i * 10)
TBGL_Vertex (A * sin(tempAngle1) * cos(tempAngle2)), _
(A * sin(tempAngle1) * sin(tempAngle2)), _
(A * cos(tempAngle1))


... but it is not necessary of course, just my mad idea :)

I am not sure what you want to achieve, so here is my try with points:


'
' TBGL test
'

Uses "TBGL"
Uses "MATH"

Dim hWnd As Dword

hWnd = TBGL_CreateWindowEx("My Demo - press ESC to quit", 640, 480, 32, 0)
TBGL_ShowWindow

tbgl_GetAsyncKeyState(-1) ' Resets ESC key status before checking

dim tempAngle1, tempAngle2 as single
DIM i,j as long
Dim A as single = 1

tbgl_PointSize 3

While tbgl_IsWindow(hWnd)

tbgl_ClearFrame

tbgl_Translate 0,0,-3
tbgl_Rotate GetTickcount/100,1,1,1

tbgl_BeginPoly %GL_POINTS

for i = 0 to 36
for j = 0 to 36

tempAngle1 = DegToRad(j * 10)
tempAngle2 = DegToRad(i * 10)
TBGL_Vertex (A * sin(tempAngle1) * cos(tempAngle2)), (A * sin(tempAngle1) * sin(tempAngle2)), (A * cos(tempAngle1))

next
next

tbgl_EndPoly

tbgl_DrawFrame

If tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow


Maybe you could save some calculations when doing FOR as following ( no need for multiplication ! ):


for i = 0 to 360 STEP 10
for j = 0 to 360 STEP 10

tempAngle1 = DegToRad(j)
tempAngle2 = DegToRad(i)
TBGL_Vertex (A * sin(tempAngle1) * cos(tempAngle2)), (A * sin(tempAngle1) * sin(tempAngle2)), (A * cos(tempAngle1))

next
next



So the result looks quite spherical. It reminds me of hologram of Death Star from starwars :)

To render sphere you can use also this( where A is radius ):


tbgl_Sphere A


To set sphere quality you could use:


TBGL_SetPrimitiveQuality QualityCoef

... where QualityCoef is 24 by default, but you can go up and down as you wish

If you want to make sphere look like dots/lines just try:


tbgl_PolygonLook %GL_POINT

' or

tbgl_PolygonLook %GL_LINE

' following is default

tbgl_PolygonLook %GL_FILL


Hope it helps,
if not, please tell me !

Bye,
Petr

P.S. Little tip - when you are posting sources, use "GeShi" combobox for thinBasic code block. You will get all the nice colors :)
Or even easier - just mark text in thinAir, go to menu Tools\Utilites\TBCode to clipboard

matthew
14-03-2007, 00:12
You're right I'm trying to create a Sphere. :)

I thought that the best way to learn thinBASIC would be to rewrite some of the programmes that I've got in other languages.

But when I run this programme it doesn't look as if the Texture is being applied correctly. :P

You'll probably need to convert the Attached Texture into the .bmp Format. :)



' The Planet Mars in ThinBASIC
'

Uses "TBGL" ' ThinBASIC OpenGL Library
Uses "UI" ' Keyboard Functions
uses "MATH" ' For Trigonometry

DIM hWnd AS dword

hWnd = TBGL_CreateWindow("The Planet Mars - Press ESC to Quit") ' Create Our Window, Return Handle.

dim Xrot, Yrot, Zrot as single
dim sphereCircum as integer
dim i, j as integer
dim oldTick, currentTick, timeTick, Frames as integer
dim framesSecond as single
dim Texture as integer
dim one36th, one18th as single

TBGL_ShowWindow ' Show Window

tbgl_LoadTexture App_SourcePath+"MarsMap_1024x512.bmp",1, %tbgl_tex_mipmap ' Load in Our Texture

TBGL_UseTexture 1 ' I want to use textures
TBGL_BindTexture 1 ' I want to use texture #1


GetAsyncKeyState(%VK_ESCAPE) ' Reset 'ESC' Key Status

one36th = 1.0 / 36.0
one18th = 1.0 / 18.0

oldTick = gettickcount

While isWindow(hWnd)

currentTick = gettickcount
timeTick = currentTick - oldTick

If timeTick >= 1000 then framesSecond = Frames * (timeTick/1000)
Frames = 0
oldTick = currentTick
endif

Frames = Frames + 1

TBGL_ClearFrame ' Clear Screen
TBGL_Camera 0,0,1,0,0,0 ' Set-Up Camera, look From 0,0,1 To 0,0,0.
tbgl_resetmatrix ' Clear the Current Matrix
TBGL_Translate 0.0, 0.0, -6.0 ' Move Into the Screen -6.0

tbgl_Rotate Xrot, 1.0, 0.0, 0.0 ' Rotation around the X-Axis
tbgl_Rotate Yrot, 0.0, 1.0, 0.0 ' Rotation around the Y-Axis
tbgl_Rotate Zrot, 0.0, 0.0, 1.0 ' Rotation around the Z-Axis

sphereCircum = 2 ' Circumference of Sphere

For j = 0 to 17

tbgl_beginpoly %gl_quad_strip

for i = 0 to 36

' Draw Top Vertex

tbgl_texcoord2d ((36 - i) * one36th), (j * one18th)
TBGL_Vertex sphereCircum * sin(degtorad(j * 10)) * cos(degtorad(i * 10)), sphereCircum * sin(degtorad(j * 10)) * sin(degtorad(i*10)), sphereCircum * cos(degtorad(j*10))

' Draw Bottom Vertex

tbgl_texcoord2d ((36 - i) * one36th), (j + 1.0) * one18th
tbgl_vertex sphereCircum * sin((degtorad(j*10)+10)) * cos(degtorad(i*10)), sphereCircum * sin((degtorad(j*10)+10)) * sin(degtorad(i*10)), sphereCircum * cos(degtorad(j*10)+10)

next

tbgl_endpoly

next

TBGL_DrawFrame ' Swap the Buffers - Display the Rendered Image

Xrot = Xrot + 0.1
Yrot = Yrot + 0.2
Zrot = Zrot + 0.15

If GetAsyncKeyState(%VK_ESCAPE) then Exit While

Wend

TBGL_DestroyWindow ' Closes OpenGL Window

ErosOlmi
14-03-2007, 00:58
Hi matthew,

not an experct as Petr so do not know where the problem is.
I just attach an old example made by Petr on planets. Maybe can help.

Regards
Eros

matthew
14-03-2007, 01:11
^^
That's a really good Example. :)

kryton9
14-03-2007, 04:03
Mathew welcome aboard. Glad to see a new tbgl/thinBasic user. You will soon be amazed by what power there is with thinBasic and all the cool modules like tbgl and tbdi.

Petr Schreiber
14-03-2007, 11:11
Hi matthew,

I am not on PC with thinBASIC now ( scary, painfull, totally terrible :) ! )

TBGL_LoadTexture supports BMP and TGA only now.

Is the problem in fact the JPG texture was not loaded or the mapping did not matched generally ?


Bye,
Petr

ErosOlmi
14-03-2007, 11:29
Hi Petr,

problem I think is a vertex problem. See image.

Ciao
Eros

Michael Hartlef
14-03-2007, 12:41
That's not Mars, that's Saturn on Steroids ;D

matthew
14-03-2007, 13:25
Hi everyone...

@kryton9 - Thanks for the welcome. :)

@Psch - I made sure that when I used the Texture I converted it to a Bitmap (.bmp). :)

@ ErosOlmi - I believe you're right, it does look like a Vertex problem.

@ MikeHart - Ha... :D

Petr Schreiber
14-03-2007, 17:56
Hi matthew,

I'm still on non thinBASIC PC so another wild guess is coming from me :)
I think the problem is still in passing "right" parameter to SIN and COS.

Instead of:


(degtorad(j*10)+10)


Should be:



( degtorad ( j * 10 + 10 ) )

' or if you don't want to think about operator precedence

( degtorad ( ( j * 10 ) + 10 ) )

... and the same for i*10

What are you doing now is that you pass "+ 10" but in your sample it does not mean + 10 degrees, but + 10 radians.
But it is possible I missed something else, when I'll be back on my working PC I will tell you more :)


Let me know,
Petr

matthew
14-03-2007, 18:21
^^
Yes, it appears to have worked. ;D

I'll Upload the completed Programme later on. ;)

Edit...
Download from here (http://matthew-4gl.wikispaces.com/space/showimage/thinBASIC_Planet_Mars.zip). (685 KB) :)

http://matthew-4gl.wikispaces.com/space/showimage/thinBASIC_Planet_Mars_Pic.png

ErosOlmi
14-03-2007, 18:54
It seems we have another master here.

Just a little note matthew: you can attach your work files to the post.
Both zip and/or images. Limit is quite high (10 Mb * 10) or limited by upload time out (I can increase).
This will let people get your work even if for any reason your server is down or you will delete the files.
Just for future reference.

Thanks a lot matthew!

matthew
14-03-2007, 18:58
^^
Hey, it's Petr you should be thanking. :)

Without his help I would never have got this Programme completed. :D

ErosOlmi
14-03-2007, 19:00
Well, Petr is my Master of the Universe on TBGL :D
I'm telling "thanks" not for the results but also for you engagement and passion I see in thinBasic.
Hope to give you more power for future.

So thanks again.
Eros

kryton9
14-03-2007, 20:59
Mathew cool code, neat to see how to make a sphere with quads. Just to let you know tbgl has a sphere command.

TBGL_SPHERE RadiusParameter

It is so much fun to see the planets at are fingers like that. Are you planning on making our solar system?

ErosOlmi
14-03-2007, 21:03
Great idea.

Making the full solar system with free to fly over space ship could be a great project.
But should be professional taking into account proportion and distances.

8)

matthew
14-03-2007, 22:17
Mathew cool code, neat to see how to make a sphere with quads. Just to let you know tbgl has a sphere command.

TBGL_SPHERE RadiusParameter

It is so much fun to see the planets at are fingers like that. Are you planning on making our solar system?




Making an entire Solar System would be a good thing to try and do one day. :)

But I'd probably get confused with all the Mathematics involved. :D

Petr Schreiber
14-03-2007, 22:26
Hi,

I am happy it works now :)
Making solar system is one of my secret dreams,
it would need some learning of astrology stuff but maybe it would not be impossible...


Bye,
Petr

ErosOlmi
14-03-2007, 23:00
I can create a new dedicated forum if you like this project.
Let me know.

kryton9
15-03-2007, 07:07
It wouldn't have to be totally accurate, in fact it would probably be more interesting not being so. The reason, I just did a version in darkbasic pro awhile back. I just had all the planets in a line our from the sun. The only things real were the distance from the sun and the scale of all the planets in relation to the sun. You can not believe how small planets are when you look at them this way. If they weren't in a straight line it would be almost impossible to find them flying around.

Even when moving on the line at any speed to make it easy to cover the distance it was easy to miss them.

So a fun Solar System where you could see all the planets easilly and fly around would be neat. With some moons or all the moons if ambitious.

Here are my notes I made back then if it helps or if interested:
If you copy and paste and use courier new 12 point, the columns should line up.


research for game, space mode: last modified: June 11, 2006
by kryton9 from numerous sites on the web

record speeds and others to get an idea:
autogyro 120
helicopter 250
biplane 300
normal aircraft 500
jet 2,200
rocket air flyer 4,520
scram jet 7,000
shuttle type rocket entry speed 17,000 orbiting speed about 13,000
apollo type vehicle 24,000 on entry
voyager nasa not the star trek show one 39,000
ion engine 150,000 relative lenght of burn and gases used. Xenon has 10 times the thrust than normal chemical rockets.
impulse under light speed warp 1 is light speed
warp speed is cubing the warp factor X speed of light 8,000 x 186000 is warp 20 the max before transwarp.
Each jumpgate, is a parsec (a parsec is 3.27 light years).
hyperdrive 127 ly/hr Chakotay says in Scorpion that it will take 5 days at maximum warp to go 40 lightyears. That's 8 ly/day. As we saw above, a hyperdrive can cover that 8 lys in under a minute.
wormhole you travel less than the speed of light in the hole, but cover vast distances so can seem like incredible speed.

Our Solar System approximate info to give me some idea on the scales and distances. Pretty mind numbing.

average distances rounded miles: radius x Earth:: gravity x Earth: escape velocity x Earth:
earth - moon 240,000 mi 0.2724 0.166 0.213
sun 1 93m 109 28 55
mercury 0.387 36m 0.3825 0.38 0.38
venus 0.723 68m 0.9488 0.91 0.927
earth 1 93m 4,000 mi 32ft/sec/sec 25,000
mars 1.524 142m 0.5326 0.38 0.45
jupiter 5.203 483m 11.209 2.14 5.33
saturn 9.537 839m 9.449 0.74 3
uranus 19.191 1.8b 4 0.86 1.904
neptune 30.069 2.8b 3.883 1.1 2
pluto 39.482 3.6b 0.18 0.08 0.1

Nearest Star is 4.22 light years away. That is 24,753,237,120,000 24.7 trillion miles

The Oort Cloud is 18 trillion miles, that is the farthest extent of our solar system where sun's gravitational
force ends.

Most of the planets are on a similar elliptical plane, but for the game to make it more interesting I will make mine not so.

Initially thinking of 27 solar systems for the game. Each with many planets, moons, space platforms/stations nearer the inner solar system. The outer solar system will be like outlands, planetoids, asteroids, asteroid belts.

In between solar systems will be nebulas and other space cloud type thingys.

There will be one bizzare solar system, where anything can happen, like a planet where it would be like toon style or planets with bizzare coloring schemes and designs. This is where the sky is the limit for imagination and the introduction of fantasy type things if wanted. Another idea is to take out game character.ship etc and use it in classic type games like invaders, asteroid, galaga and defender (space defender).

Petr Schreiber
15-03-2007, 09:59
Thanks for the data kryton,

do you work for NASA :) ?


Bye,
Petr

kryton9
16-03-2007, 07:33
No, thanks though, just research for my game when I get around to it. But making that simple scale of our solar system really had an impact on me when you think of how huge the universe is. The numbers are mind numbing and soooooo coooooool, and then add the 11 or so dimensions they say now exists, what a great place!!