Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 3d maths: Speed -> Angles & Angles -> Speed

Author
Message
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 11th Aug 2003 21:09
Ok, 2D trig is pretty easy, if you ever listened at school. Use all that soh cah toa stuff, sine rule and cosine rule stuff etc. and you can't go wrong. They never teach you any 3D maths though? Why is that?

Anyway, what I need to do is work both ways with vectors and angles. In my current game, I have an arrow which has an x and y angle of trajectory, and a speed it will leave the bow at. What I need to do is:
-Convert the X,Y Angles plus speed to X,Y,Z world vector speeds.
-Add certain values to these X,Y,Z vectors like wind and gravity.
-Convert them back to X,Y Angles to rotate the arrow.

In 2D its easy, and I've worked out the 3D maths in what seems like the correct way to me, but its not working, so I must be doing something wrong.

Basically my two questions are:
How do I derive X,Y,Z velocities from X,Y angles and Speed.
And how do I derive X,Y angles from X,Y,Z velocities.

Cheers for any help.
Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
Shadow Robert
23
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 11th Aug 2003 21:17
Z is based as Y
Y is based as a Single Sin which is also provided to the XZ to set thier values

object.p.y = sin(y+speed)
object.p.x = (cos(x+speed)*distance)/object.p.y
object.p.z = (sin(x+speed)*distance)/object.p.y

that should help out the calculations

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 11th Aug 2003 21:29
Cheers, but it makes no sense!

What is object.p.x? PostionX? AngleX? Or SpeedX?
I also don't understand why there is a variable speed and a variable distance?

Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 11th Aug 2003 21:40
Edit:
To make my confusion more clear.
What is object.p.x?
What is x,y?
What is distance?

I dont know which are angles, which are positions, which are speeds and what distance is for.

Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
adr
23
Years of Service
User Offline
Joined: 21st May 2003
Location: Job Centre
Posted: 11th Aug 2003 22:05
p = position.

If raven's naming conventions are similar to mine, then p will be a UDT - it holds the x, y and z values for whatever.



but that's all I know ... should brush up on my maths really

I opened the case to my PC today and thought, my exhaust fan sucks.
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 11th Aug 2003 22:10
That's what I thought, but it can't be a position. Look at this:

object.p.y = sin(y+speed)

You can't derive a position using sin, an angle and a speed value. Unless y is a position, and then the whole thing makes no sense.

Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
keeblerElf
23
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 12th Aug 2003 09:33
But object.p.y = sin(y+speed) is only one of the 3 equations raven gave you...all three of his equations put together give you your x,y, and z velocities

Live life and play video games
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 12th Aug 2003 13:32
object.p.y is only set once. So it has to be set to its value the first time. Doesnt matter what the other two do as far as p.y. is concerned.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
keeblerElf
23
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 13th Aug 2003 00:02
object.p.y, object.p.x, and object.p.z should make up a velocity vector. Thus what the other two do does matter as far as p.y is concerned

Live life and play video games
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 13th Aug 2003 01:51
If they're all velocity vectors, then thats possible, but I was under the impression they were positions and Raven hasn't been back to make any of this clear.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
keeblerElf
23
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 13th Aug 2003 05:04
All three of the make up one "position"...that position would be <object.p.x,object.p.y,object.p.z)

Live life and play video games
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 13th Aug 2003 06:30
Make up your mind. Firstly you said they were a velocity vectors, and now you're saying they're a position. They're one or the other, but they can't be a position because you can't derive a positional coordinate, even in one axis from an angle and a speed. It's like saying I'm travelling horizontally along this road, at 50mph ... how far down it am I? It's impossible to figure out. By Y has to be figured out in that first part of the equation before x and z can be resolved.

In which case, seeing as its impossible for it to be a positional coordinate, it must be vector speeds, although I'm not gonna assume this seeing as they're named .p.x .p.y .p.z.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
keeblerElf
23
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 13th Aug 2003 07:31
note how I put "position" in quotations because that was what you called it...

In which case, seeing as its impossible for it to be a positional coordinate, it must be vector speeds, although I'm not gonna assume this seeing as they're named .p.x .p.y .p.z.

You nailed it right there with the "vector speeds" part

Live life and play video games
EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 13th Aug 2003 07:45 Edited at: 13th Aug 2003 08:15
@Fallout: Let me see if I can offer some help...

Okay, you want to convert direction (y angle), azimuth (x angle) and speed (a "scalar" quantity which is the "magnitude of the velocity vector) into a velocity vector and back.

If I'm figuring correctly, the velocity vector with components <vx, vy, vz> should be calculated:

[edit] Let's try again...

rem x_angle is azimuth => straight up is -90, straight down is 90
rem y_angle is direction => north is 0, south is 180
vx = speed * cos(y_angle)
vy = speed * sin(x_angle)
vz = speed * cos(y_angle) * cos(x_angle)

To convert back:

speed = sqrt(vx*vx + vy*vy + vz*vz)
y_angle = acos(vx/speed)
x_angle = asin(vy/speed)

Give that a try!

Also, to compute position, you keep track of the time in "timer_last" which is the last time we moved the object, and then use the incremental variation of the "distance = rate * time" equation:

rem time is in seconds
rem speed and velocity are in world coordinates per second
timer_now = timer()
delta_t = (timer_now - timer_last) / 1000.0 : rem convert to seconds
px = px_prev + vx * delta_t
py = py_prev + vy * delta_t
pz = pz_prev + vz * delta_t
position object objnum, px, py, pz
px_prev = px
py_prev = py
pz_prev = pz
timer_last = timer_now

Hope this helps...

Ed

EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 13th Aug 2003 07:58 Edited at: 13th Aug 2003 08:12
Hmmmm.... it's late... the equations were wrong... original post edited.

They still may not be right, but give them a try and let me know how they work for you...

EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 13th Aug 2003 08:18
Can someone please ask Lee to make vectors and matricies into "real" DBP data types in DBP? Something like:

type Vector3
x as float
y as float
z as float
endtype

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 13th Aug 2003 15:15 Edited at: 13th Aug 2003 15:18
Nice one. Cheers for that, I'll give it a try. I'm actually moving the object using vector speeds and stuff, so I get away without using any of those equations for most of my objects. The thing is though, it's an archer game, so even though I'm moving my arrows using vectors, I want the arrow to point in the direction the vector speeds indicate. That's why I was after these equations. Also, I wanted to derive the vector speeds from the angle the bow is at when the arrow is released (currently I just get the old position, move the arrow once by it's speed using the MOVE OBJECT command, then record the new distance and retrieve the values from that). These equations will hopefully cut out that step.

Anyway thanks, I'll give them a try.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Aug 2003 15:31
@Ed, I've already been thinking along those lines myself ... I have converted my wavyflag code to use an array for the vertex buffer instead of using a memblock ... you wouldn't believe how short it made the code.

I might try doing the same with vectors and matrices sometime soon ...
EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 13th Aug 2003 18:42 Edited at: 13th Aug 2003 18:43
@IanM: Cool. Ya know... I think the main reason DBP doesn't support some native vector/matrix types is because it'd be pretty silly using them when you couldn't pass them as args to a function or have a function return them. At least with P5 (which I haven't tried yet - don't like the "makes full version into demo version" aspect, I have to admit - can I have p4.1 and p5 installed in parallel on the same system?), you can pass UDTs as args... but who knows when we'll be able to return a UDT. Hence, the whole sillyness with "make vector..." and stuff... they have to be referrable by "number" because that's all you could pass/return to/from functions, or they really would be pretty useless. I haven't played with the "make vector" stuff so I'm not exactly sure how silly/cool that are or can be in their current state. All I know is, even looking at an example that uses them, it's not obvious how they work exactly, and the docs don't explain how to use them (of course).

EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 13th Aug 2003 19:07 Edited at: 13th Aug 2003 19:17
@Fallout: Okay, good luck!

Things to watch out for... in parts of your program that want to make the bow "aim higher" it needs to *decrease* the X angle and to "aim lower" it needs to *increase* the X angle. For the Y angle... to "aim right" you increase the Y angle, to "aim left" you decrease it. Also, I'd probably use "set camera rotation xyz" (for 1st person "shooting" view) or "set object rotation xyz" (if you doing this for an object in 3rd person view) so that the X rotation is performed first, then the Y rotation. Finally, I'd constrain the X angle so it can't go outside the -90 to 90 range (the "ahead" azimuths - disallowing the "bending over backwards" azimuths).

If DBP can't handle negative numbers for rotation angles (haven't tried this) then you'll have to use wrapvalue() on them and make X angle constrained to be >= 270 and <= 90 and wrapvalue() itself should keep the Y angle under control.

Cheers!

Ed

Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 13th Aug 2003 19:12
People who are really confused about 3D Maths could do worse than get:

"Mathematics for 3D Game Programming and Computer Graphics" by Eric Lengyel.

Mind you, its not written with the mathematically inept in mind. Make sure you know trig before you look at it.

Phil

What do you mean, bears aren't supposed to wear hats and a tie?
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 13th Aug 2003 23:41
Yeah, was definitely toying with buying a 3D maths book at one point, but I'm too lazy to go to a book shop, and it's probably a bad idea to just use pot luck on Amazon or something. I think I'll have to actually go to a book shop or .. *shudders* .. a library.

As for the game, I'll post a little demo up soon. I have one uploaded, but it's too simple at the moment. Once I've coded it so that the woman whos running ramdomly around can be shot and have the arrow sticking out of her, I'll be happy. hehe.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Aug 2003 01:47
It's definitely helpful to know a bit of whats going on behind the scenes.

You could try looking at 'Tricks of the 3D Game Programming Gurus', but only if you know enough C to get by with.

There is quite a bit of maths in it, but it follows it up by walking you through a complete 3D software renderer ... I'm up to the lighting/shading part
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 14th Aug 2003 02:59
Actually, I was thinking more along the lines of a book that'd help me with physics. I've been wanting to make my own car physics engine for quite some time, and although I've got some physics planned out and coded, just like this problem here, I'm missing some key equations. I don't know how to, specifically, have a force in one direction, and describe how it will effect a point. Need to find a book with lots of info about that. Also info about detecting collision between two planes, and finding out if a point is inside a polygon etc. That's the sort of book I'm after.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 15th Aug 2003 20:06
In that case its definitely the Mathematics for 3d Game Programmers that you want. However, its not by any means a simple book.

An alternative is to buy anything on mechanics, being the mathematical study of movement.

Cheers

Phil

What do you mean, bears aren't supposed to wear hats and a tie?
EddieRay
23
Years of Service
User Offline
Joined: 28th Feb 2003
Location: USA
Posted: 2nd Sep 2003 19:56
Any luck getting the equations to do what you want?
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 2nd Sep 2003 22:03
Yes mate. Did the job fine.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 3rd Sep 2003 16:46
Noooo! Not the LaMothe "Tricks of the 3D Game Programming Gurus" book IanM?!

Mucked about with Raiders yet?

Phil

What do you mean, bears aren't supposed to wear hats and a tie?
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Sep 2003 21:14
I had a play around with it, but I couldn't honestly say I understood all the maths, so I'll go back to it in a few weeks.

Login to post a reply

Server time is: 2026-07-23 18:19:19
Your offset time is: 2026-07-23 18:19:19