|
-
Nov 28th, 2002, 06:07 PM
#1
Thread Starter
Fanatic Member
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:
Sub Movement (X,Y,Z,Speed,Angle1, Angle2)
'angle1 is XY direction, angle2 is Z direction (supposedly)
'Sin(0) = 1 and Cos(90) = 1 right???
X = X + Speed* Sin(Angle1) * Cos(Angle2)
Y = Y + Speed * Cos(Angle1) * Cos(Angle2)
Z = Z + Speed * Cos(Angle1) * Sin(Angle2)
End sub
Sub Placement (X,Y,Z)
'This assumes your viewpoint doesn't move, to start
'(I have a function for angles)
AngleXY = Angle from 0,0 to X,Y
Diagonal = Sqr((X^2) + (Y^2))
'Get difference on x,y axises
Distance = Sqr((Diagonal^2) + (Z^2))
'A^2 + B^2 = C^2, I use this to set the size
TrueAngle = anglesub(0,0,Diagonal,Z)
'I use this with the XY angle to place the object
TargetX = 320 + (((TrueAngle) / 90) * 320) * Sin(AngleXY)
TargetY = 240 + (((TrueAngle) / 90) * 320) * Cos(AngleXY)
'Use TrueAngle and XYangle to place, expand to fit screen size
'240 is the center of Y, 320 is center of X, (320,240)=centerscreen
'*****Set size of object here, change color to grey if FAR*****
'Sorry bout not including size code.. its not important
'Draw the point...
Circle(TargetX, TargetY), Size, Color
Paint(TargetX, TargetY), Color
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.
-
Dec 11th, 2002, 03:21 PM
#2
Thread Starter
Fanatic Member
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.
-
Dec 12th, 2002, 03:50 PM
#3
Fanatic Member
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!
-
Dec 12th, 2002, 07:11 PM
#4
Thread Starter
Fanatic Member
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.
-
Dec 13th, 2002, 07:35 AM
#5
Fanatic Member
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
-
Dec 13th, 2002, 09:32 AM
#6
Thread Starter
Fanatic Member
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.
-
Dec 14th, 2002, 08:51 PM
#7
Thread Starter
Fanatic Member
here
here's the function I was asked to post (to get angles)
VB Code:
FUNCTION Target (SourceX, SourceY, DestX, DestY)
DIM Vertical AS SINGLE
DIM Horizontal AS SINGLE
DIM Diagonal AS SINGLE
DIM Vector AS SINGLE
'Get the difference on each axis (I made this for 2d first, horizontal could be depth, etc
Horizontal = ABS(SourceX - DestX)
Vertical = ABS(SourceY - DestY)
'If they = to 0, arcsin will return an error
IF Horizontal AND Vertical <> 0 THEN
'Get the angle
Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2))
Vector = (Arcsin(Vertical / Diagonal))
'Because it returns a number between 0 and 90, I need to put it between 0 and 360
IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90
IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270
IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90
ELSE
'Arcsin can't do this for some reason, so I put em in
IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0
IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180
IF Vertical = 0 AND DestX > SourceX THEN Vector = 90
IF Vertical = 0 AND DestX < SourceX THEN Vector = 270
IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION
END IF
'Put vector between 0 and 360 if it isn't
DO UNTIL Vector <= 360 AND Vector > 0
IF Vector <= 0 THEN Vector = Vector + 360
IF Vector > 360 THEN Vector = Vector - 360
LOOP
Target = Vector
END FUNCTION
Last edited by alkatran; Dec 15th, 2002 at 09:23 AM.
Don't pay attention to this signature, it's contradictory.
-
Dec 15th, 2002, 12:14 AM
#8
transcendental analytic
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.
-
Dec 16th, 2002, 07:17 AM
#9
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:
Public Sub Bewegung(PosX, PosY, Course, Speed)
'Movement of an object using course (0-359) and speed(Kn) for 1 second
'Input PosX, PosY,Course, Speed
'Output PosX ,PosY
PosX = Sin(Course * 1.74532925199433E-02) * Speed * (1 / 3600) + PosX
PosY = Cos(Course * 1.74532925199433E-02) * Speed * (1 / 3600) + PosY
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!
-
Dec 16th, 2002, 07:34 AM
#10
Thread Starter
Fanatic Member
Oops
I didn't post that I converted angles to radians
VB Code:
Private sub Rad(X as single)
X = X * (Pi/180)
End sub
'.... means code heh
Private Sub Movement(....)
......sin(rad(x)).......cos(rad(x))
.....
.....
End sub
Don't pay attention to this signature, it's contradictory.
-
Dec 16th, 2002, 08:25 AM
#11
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!
-
Dec 16th, 2002, 08:59 AM
#12
Thread Starter
Fanatic Member
.
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.
-
Dec 16th, 2002, 09:51 AM
#13
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!
-
Dec 16th, 2002, 02:16 PM
#14
Thread Starter
Fanatic Member
..
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:
do until a$ = chr$(27)
a$ = inkey$
'call movement(x,y,z,speed,direction) im commented!
x=x+1
call placement(x,y,z)
loop
Don't pay attention to this signature, it's contradictory.
-
Dec 17th, 2002, 03:30 AM
#15
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!
-
Dec 17th, 2002, 10:53 AM
#16
Thread Starter
Fanatic Member
Well
VB Code:
AngleXY = Angle from 0,0 to X,Y
Diagonal = Sqr((X^2) + (Y^2))
Distance = Sqr((Diagonal^2) + (Z^2))
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|