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 Movement with vectors

Author
Message
Derekioh
17
Years of Service
User Offline
Joined: 1st Feb 2009
Location:
Posted: 23rd Jun 2010 06:01
I hope a stated that correctly. Anyway, i was wondering if there is a tutorial or if someone could explain how to move an object in 3D space using vectors (again, not sure if i'm stating that correctly). i as this because i see so many people in games like FPS using vectors to move an object. I've been programming about a year and a half now and i like to do more advanced things then "move object left."
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 23rd Jun 2010 15:15
In the world of 3D game programming, "vector" is a position/direction in reference to 0,0,0 (origin). In DBP it is not necessary to use vectors unless you are doing the math yourself. You can use the object positioning commands to create movement in 3D space. If you are wanting to do the math, look up vector/matrix math on the internet or in the DBP help. Ask us specific questions about what you don't understand and you should get a specific answer. I've been doing vector/matrix math for a few years now and I still don't know everything, but I am happy to help.

The fastest code is the code never written.
Derekioh
17
Years of Service
User Offline
Joined: 1st Feb 2009
Location:
Posted: 23rd Jun 2010 16:24
Ok, but is it the best way to move an object in a game? Or does it not matter?
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 23rd Jun 2010 17:28
Vectors are a good way of abstracting how you control your objects. DBPro isn't the best example of this, but I'll try and explain as clearly as I can how they can be useful.


So a vector is essentially a 'line segment', in 3D space (in this case). So, lets say you wanted to move an object straight forward - you move it along the vector (0, 0, 1). Just imagine a line coming out from the centre of your object, pointing straight along the Z axis. You move along that one.

Now where do vectors become really useful with 3D objects? An object maintains at least 3 vectors - the local X, Y and Z vectors. When you make an object, the local X vector comes straight out the side of the object, the Y out the top, and the Z in front. If you rotate the object slightly round the Y axis, the local X and Z vectors will rotate too. If you imagine yourself in a car, your local X vector would always be what you get if you point your right arm out - not east - that's the global X vector - it never changes.

So why are these useful? Well, if you want to move the object forward, you just add the local Z vector onto the object's position vector. If you want to move left, you subtract the local X vector from the object's position vector, gettit?

Furthermore, they are useful for rotations. If you think about being in a spaceship, I'm sure you know there's no such thing as 'up' in space. But when you want to turn left or right, you're essentially turning around your local Y vector. Tilting up and down is rotation around the local X vector, and rolling (barrel rolls etc) are around the Z vector - the one that points in the direction you're moving. DBPro handles all this automatically, and you can do it using 'turn object left/right', 'pitch object up/down' and 'roll object left/right'.

That's how vectors tend to be used for object movement. You can think about just changing the X position of the object, or w/e, but that's not a very useful way of thinking about it. If you think of the vectors as actual distinct "things", thinking about movement of an object becomes a lot simpler - and this becomes important when you want to do more complex things, like physics.

"everyone forgets a semi-colon sometimes." - Phaelax
Derekioh
17
Years of Service
User Offline
Joined: 1st Feb 2009
Location:
Posted: 23rd Jun 2010 17:42
ok, i sort of get it. I guess the problem is i see so many people doing crazy stuff with vectors just to move an object, i figured that using "move object" is probably not the best tecnique for making a game.
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 23rd Jun 2010 18:10
It's perfectly fine as it is - it does exactly what I just told you, but keeps it under the surface to make it simple. However, if you want some smoother movement, it's not really that good. I made a vehicle physics tutorial in the Tutorials Thread (at the top of the Newcomers Corner). Check it out, it's old, but it'll still give you a good understanding.

"everyone forgets a semi-colon sometimes." - Phaelax
Derekioh
17
Years of Service
User Offline
Joined: 1st Feb 2009
Location:
Posted: 23rd Jun 2010 18:13
ok, thanks Zotoaster. I'll try using vectors one of these days and probably come back asking questions.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 23rd Jun 2010 18:31
To use move object you have to orient the object to face the direction you want to move, then once it's moved, re-orient it back to the way you want it facing. Doing the vector math yourself requires that you use the position object command. This method does nothing with the object's orientation, so you can use the point object command to orient it.

I prefer vector math when I'm doing something in 3D with collisions; but pointing an object then moving it works just fine for everything else.

If you have a specific question about vector math, let me know.

The fastest code is the code never written.
Derekioh
17
Years of Service
User Offline
Joined: 1st Feb 2009
Location:
Posted: 23rd Jun 2010 18:39
ok, thx Hawkblood
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 23rd Jun 2010 19:34 Edited at: 23rd Jun 2010 19:39
One issue (there's more than one, believe me) with the vector/matrix commands in DBP is there missing loads. I ended up having to make my own set of Vector2d/3d/4d, Matrix2x2/3x3/4x4 and Quaternion classes. I remember I converted a move camera snippet by darkcoder:



A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 23rd Jun 2010 19:54
I've not seen any problems with DBP's matrix/vector math functions except in one extreme case: it had to do with rotating an object around a sphere..... For most applications, they work fine.

The fastest code is the code never written.
Four Dimensional
18
Years of Service
User Offline
Joined: 11th Jun 2008
Location: lost in thought
Posted: 24th Jun 2010 05:12
To expand upon the statements made previous, a vector can represent an object's position, velocity and acceleration.

Usually, acceleration due to gravity is represented by the vector <0, -4.9, 0> I think... When added to the velocity vector every second, the velocity of x and z will remain the same, but y will decrease.
The velocity vector should also be added to the position vector every second (NOTE: vector addition is very straightforward, <a, b, c> + <d, e, f> = <a+d, b+e, c+f>. This will change the position every second, so they function like real-world velocity and acceleration.
Also, friction can be a vector operation as well: simply scale the velocity vector (s * <a, b, c>= <sa, sb, sc>, where s < 1).
Finally, vectors can even be reflected about other vectors to bounce an object off another or light from a shiny object.

I hope this helps, I could create an example if you want.

Imagine this: there may exist infinite parallel universes, all similar to ours, but we may never be able to encounter or even interact with them.
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 24th Jun 2010 05:18
4D, in game programming you wouldn't really add it on every second, you would add it on every frame. If you add it on every second, there would be ~60 frames of one velocity before going on to the next - but I'm guessing you're talking about real life, in which case it's (0, -9.81, 0). Also, just to be picky, for friction, you gotta make sure -1 < s < 1 - sorry for stating the obvious, lol

"everyone forgets a semi-colon sometimes." - Phaelax
Four Dimensional
18
Years of Service
User Offline
Joined: 11th Jun 2008
Location: lost in thought
Posted: 24th Jun 2010 06:14
Thank you, Zotoaster. I actually haven't applied many of these yet, so I apologize for my misstatements. I haven't figured a way around the second vs fps barrier yet, but I suppose if floats were used, one could scale the velocity and acceleration by (1/fps).
Also, you are indeed correct about the velocity vector, I checked and again apologize.

However, to clarify the range of friction, I meant 0 < s < 1. This is because a negative value is used to flip the direction of the velocity since multiplication is involved, so a negative s could lead to "jittering" as the object goes forward then backward again.

I figure I now owe all of you more information on vectors, which i hope is correct (crossing my fingers):

-To normalize a vector is to divide by its magnitude, which creates a unit vector, or one on a unit circle (or sphere).
-The dot product (<a,b,c>.<d,e,f>= ad+be+fc)of two normalized vectors returns the cosine of the angle between them (not a vector).
-The cross product (<a,b,c>x<d,e,f>= <bf−ce, cd−af, ae−bd> of two normalized vectors creates a unit vector perpendicular to both multiplied by the sine of the angle between them.

I haven't applied these either so i hope they work.
Thanks and sorry.

Imagine this: there may exist infinite parallel universes, all similar to ours, but we may never be able to encounter or even interact with them.

Login to post a reply

Server time is: 2026-07-25 06:57:13
Your offset time is: 2026-07-25 06:57:13