Results 1 to 16 of 16

Thread: 3D field

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Angry 3D field

    Hi, I'm trying to make a 3D field in QBasic, and I'm having a few problems. Mainly in 2 functions (I think).

    VB Code:
    1. Sub Movement (X,Y,Z,Speed,Angle1, Angle2)
    2. 'angle1 is XY direction, angle2 is Z direction (supposedly)
    3. 'Sin(0) = 1 and Cos(90) = 1 right???
    4. X = X + Speed* Sin(Angle1) * Cos(Angle2)
    5. Y = Y + Speed * Cos(Angle1) * Cos(Angle2)
    6. Z = Z + Speed * Cos(Angle1) * Sin(Angle2)
    7. End sub
    8.  
    9. Sub Placement (X,Y,Z)
    10. 'This assumes your viewpoint doesn't move, to start
    11.  
    12. '(I have a function for angles)
    13. AngleXY = Angle from 0,0 to X,Y
    14.  
    15. Diagonal = Sqr((X^2) + (Y^2))
    16. 'Get difference on x,y axises
    17. Distance = Sqr((Diagonal^2) + (Z^2))
    18. 'A^2 + B^2 = C^2, I use this to set the size
    19. TrueAngle = anglesub(0,0,Diagonal,Z)
    20. 'I use this with the XY angle to place the object
    21.  
    22. TargetX = 320 + (((TrueAngle) / 90) * 320) * Sin(AngleXY)
    23. TargetY = 240 + (((TrueAngle) / 90) * 320) * Cos(AngleXY)
    24. 'Use TrueAngle and XYangle to place, expand to fit screen size
    25. '240 is the center of Y, 320 is center of X, (320,240)=centerscreen
    26.  
    27. '*****Set size of object here, change color to grey if FAR*****
    28. 'Sorry bout not including size code.. its not important
    29.  
    30. 'Draw the point...
    31. Circle(TargetX, TargetY), Size, Color
    32. Paint(TargetX, TargetY), Color
    33. End Sub

    Any ideas?
    Last edited by alkatran; Dec 11th, 2002 at 03:20 PM.
    Don't pay attention to this signature, it's contradictory.

  2. #2

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Angry Come on people

    Where are those super math geniuses?! Let's get going!




    ...Just kidding...
    I added comments and removed a bit of obselete code.
    Don't pay attention to this signature, it's contradictory.

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    I have some suggestions:

    sin(0) = 0, cos(0) = 1
    sin(90) = 1, cos(90) = 0
    sin(180) = 0, cos(180) = -1
    sin(270) = -1, cos(270) = 0

    Keep in mind that in VB (and probably also QBasic) you should use half pi (about 1.57), instead of 90 degrees.

    I'm not sure about speed (and what you want to achieve), but I think it should be as following:
    x = x + speed * cos(angle1) * cos(angle2)
    y = y + speed * sin(angle1) * cos(angle2)
    z = z + speed * sin(angle2)
    These formulas can be used to calculate rectangular coordinates, if you have sphere coordinates (radius = speed, latitude = angle2, longitude = angle1)

    Distance can be calculated using one line: distance = sqr(x * x + y * y + z * z)
    I removed the powers, since multiplying by itself is MUCH faster than taking the power of two.

    I hope this will help. Good luck!

  4. #4

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    so..

    sin(0) = 0, cos(0) = 1
    sin(90) = 1, cos(90) = 0
    sin(180) = 0, cos(180) = -1
    sin(270) = -1, cos(270) = 0

    That will be helpfull, good call on taking angle1 out of Z.

    But I think the big problem is with Placement, I've tried just doing x = x + 1.. and I get a.. spiral..

    Dot starts at center, begins moving in a circular motion, counter-clockwise, moving further and further away from center, reaching edge of screen at 0 degrees (where it started)
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    What does the function anglesub do? Can you please post its code?
    What do you mean with TrueAngle?
    Is your 3d field in perspective?

    What coordinate system are you using (Left hand or right hand?) and in what direction are the axises pointing.

    I think more information is necessary in order to solve your problem.



    Suppose, you're having no perspective and you're using a left hand coordinate system, and x is pointing out of the screen (towards the user, and for perspective purposes I'm letting it point towards lower left in an angle of about 30 degrees, actually atan(0.5)), y to the right and z to above, then you can draw a position like this:

    TargetX = 320 + y - 0.5x
    TargetY = 240 - z + 0.25x

  6. #6

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    TrueAngle is the angle from the point diagonal touches (the .. hypotenus (french math of x,y). Basicly I use it to decide how far out the dot will be, and use XY angle for the direction it's in (XY rotates it around center, while trueangle decides distance from center)

    X is left-right, y is up-down, z is forward-back

    What do you mean by perspective?

    I need to get target from home... I'm at school
    Don't pay attention to this signature, it's contradictory.

  7. #7

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    here

    here's the function I was asked to post (to get angles)

    VB Code:
    1. FUNCTION Target (SourceX, SourceY, DestX, DestY)
    2. DIM Vertical AS SINGLE
    3. DIM Horizontal AS SINGLE
    4. DIM Diagonal AS SINGLE
    5. DIM Vector AS SINGLE
    6.  
    7. 'Get the difference on each axis (I made this for 2d first, horizontal could be depth, etc
    8. Horizontal = ABS(SourceX - DestX)
    9. Vertical = ABS(SourceY - DestY)
    10.  
    11. 'If they = to 0, arcsin will return an error
    12. IF Horizontal AND Vertical <> 0 THEN
    13. 'Get the angle
    14.  Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2))
    15.  Vector = (Arcsin(Vertical / Diagonal))
    16.  
    17. 'Because it returns a number between 0 and 90, I need to put it between 0 and 360
    18.  IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
    19.  IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90
    20.  IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270
    21.  IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90
    22. ELSE
    23.  
    24. 'Arcsin can't do this for some reason, so I put em in
    25.  IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0
    26.  IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180
    27.  IF Vertical = 0 AND DestX > SourceX THEN Vector = 90
    28.  IF Vertical = 0 AND DestX < SourceX THEN Vector = 270
    29.  IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION
    30. END IF
    31.  
    32. 'Put vector between 0 and 360 if it isn't
    33. DO UNTIL Vector <= 360 AND Vector > 0
    34.  IF Vector <= 0 THEN Vector = Vector + 360
    35.  IF Vector > 360 THEN Vector = Vector - 360
    36. LOOP
    37.  
    38. Target = Vector
    39. END FUNCTION
    Last edited by alkatran; Dec 15th, 2002 at 09:23 AM.
    Don't pay attention to this signature, it's contradictory.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    maybe it would help if you explained what you're trying to do
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Sounds like you are trying to move submarines or aircraft ( I guess).
    I hope you have looked at the post from riis, 'cause you MUST convert your angles to radians, otherwise all trig formulas wil fail.

    We are waiting for some more explanation on what you're trying to do, that would make helping you more easy

    BTW I'm working with a 2D modell to move ship's, see my code for 2D movements
    VB Code:
    1. Public Sub Bewegung(PosX, PosY, Course, Speed)
    2. 'Movement of an object using course (0-359) and speed(Kn) for 1 second
    3. 'Input PosX, PosY,Course, Speed
    4. 'Output PosX ,PosY
    5. PosX = Sin(Course * 1.74532925199433E-02) * Speed * (1 / 3600) + PosX
    6. PosY = Cos(Course * 1.74532925199433E-02) * Speed * (1 / 3600) + PosY
    7. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Oops

    I didn't post that I converted angles to radians

    VB Code:
    1. Private sub Rad(X as single)
    2. X = X * (Pi/180)
    3. End sub
    4.  
    5. '.... means code heh
    6. Private Sub Movement(....)
    7. ......sin(rad(x)).......cos(rad(x))
    8. .....
    9. .....
    10. End sub
    Don't pay attention to this signature, it's contradictory.

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    OKay
    Your angle Angle2 is the angel inthe Z-Plane, Why are you using
    Z = Z + Speed * Cos(Angle1) * Sin(Angle2)
    instead of
    Z = Z + Speed * Sin(Angle2)
    I don't get it why Angle1 should be used here.
    What is your speed, a 2D or 3d speed?

    Where are your exact problems? Do you have false numbers/positions, if yes discribe what you get.
    Last edited by opus; Dec 16th, 2002 at 08:55 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    .

    Somebody already pointed out the Z thing..

    The problem is now in the Placement sub, it doesn`t work correctly.

    I tried bypassing movement (Just x+1, or y+1 or z+1)

    Z -/+ 1 gave a nice starfield but y and x just made the dot move in a spiral.

    The spiral starts at center, then goes the wrong way, quickly changes direction (in a spiral form, getting further away from center, distance from center and turn rate slow as it reaches the side) to reach the appropriate spot... but the path is wrong.

    Oh, I'm going to lower field of view below 180*180 degrees hehehe
    Don't pay attention to this signature, it's contradictory.

  13. #13
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Do one step at a time,
    did you try to change z ,x or y seperatly or together. Since a spiral should not come out by changing just one of them.

    Look at the values you have for x,y and Z before and after they go thru Movement! What values do you want to have, do they match with what you see? Only if you are sure about this part, start on the next (Placement)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  14. #14

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    ..

    Main rule of experimenting:

    ONE variable.
    That's why I bypassed the movement after I checked the x,y, and z values going through it (I noticed they were a bit off, but Z fix stopped that) so yes, only one at a time, although I did try 2 at a time once...

    I'm POSITIVE it's in placement now because my loop looks like so:

    VB Code:
    1. do until a$ = chr$(27)
    2. a$ = inkey$
    3. 'call movement(x,y,z,speed,direction) im commented!
    4. x=x+1
    5. call placement(x,y,z)
    6. loop
    Don't pay attention to this signature, it's contradictory.

  15. #15
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    OK, the Placement
    I looked at it again:

    Your code movement changes the x y z of your object according to the speed and direction, in Placement you only place the object on the display.
    Why on earth do you calculate (or better recalc) the directions again?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Well

    VB Code:
    1. AngleXY = Angle from 0,0 to X,Y
    2.  
    3. Diagonal = Sqr((X^2) + (Y^2))
    4.  
    5. Distance = Sqr((Diagonal^2) + (Z^2))
    6.  
    7. TrueAngle = anglesub(0,0,Diagonal,Z)

    I use these for positioning object:

    AngleXY = Direction from center
    TrueAngle = how far from center
    Distance = Size

    Diagonal = only used to find trueangle
    Don't pay attention to this signature, it's contradictory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width