Register to reply

Tutorial For Vectors in C++?

by grantwilliams
Tags: tutorial, vectors
Share this thread:
grantwilliams
#1
Nov5-12, 10:48 PM
P: 65
Hi, I'm programming the orbit of planets using a verlet algorithm, and while i feel comfortable i could implement the program, i would like to get some more practice with vectors so I can really learn how they work in C++. If anyone knows of a good tutorial for vectors (applied to physics would ideal, but not at all necessary) I would be very grateful.

Thanks,
Grant
Phys.Org News Partner Science news on Phys.org
Experts defend operational earthquake forecasting, counter critiques
EU urged to convert TV frequencies to mobile broadband
Sierra Nevada freshwater runoff could drop 26 percent by 2100
Enthalpy
#2
Nov6-12, 09:39 AM
P: 661
Do you mean "arrays"?
Or a class that models physical vectors in 3D space?

If you mean the physical vectors, to my knowledge they're not a part of C++ (but possibly of some library you have access to) so you won't find a tutorial for them as a part of C++.
grantwilliams
#3
Nov6-12, 11:57 AM
P: 65
After doing some research C++'s vectors appear to be a kind of array?

I guess I can just write my own methods for 3D space vectors?

Mark44
#4
Nov6-12, 01:00 PM
Mentor
P: 21,311
Tutorial For Vectors in C++?

It's probably more accurate to refer to them as a container than an array, since an array implies a block of contiguous memory addresses. The vector template class is now a part of C++ - see http://en.cppreference.com/w/cpp/container/vector.

If you do a web search for "cpp vector" you'll probably find some tutorial material.
NattyBumppo
#5
Nov8-12, 08:45 PM
P: 19
It sounds like you're talking about the physics/engineering concept of vectors (a direction and a magnitude) rather than the C++ concept of vectors (a special type of container that's like an array with a dynamic size).

In that case, the standard C++ libraries don't include vector support. You could either write your own class, or use a library like Boost. The Boost library is very widely used for software development, and the Boost.Math library contains a library called uBLAS which is used for linear algebra operations and which has full support for vector and matrix math.
chiro
#6
Nov9-12, 02:52 AM
P: 4,573
Hey grantwilliams.

If you are doing 3D simulation stuff, I'd recommend doing a search for open source implementations and going through them unless you want to write them yourself.

In fact if you are trying to render something and you don't have code to do so (and haven't learned what it takes to get something up and running) then i strongly suggest you look at current implementations.

There are libraries that allow you to call a few functions to create windows, draw models and that kind of thing, but depending on what you want to do and what data you need access to, you will need to think about what functionality you need in what is out there (and thankfully there is a lot out there to pick from).

If you want to get a simple object spinning on a screen then you could get the OpenGL docs and SDK, SDL, and a few decent open source classes for matrices and vectors and put the whole thing together after a bit of pain.

However the minute you start thinking "I want it do this", "Now what about this" and so on, then you will need to sit down and think for a minute about how complex your needs are and more importantly (especially if they are complex) if someone has taken steps towards meeting them (partially).

This is why people use things like 3DSMAX, MATLAB (or Maple), or AutoCAD, or Oracle to do stuff: it's because they have managed to take an extremely complex problem and actually figure out how to do it well enough so that not only can people end up getting things done, but also so that it can be extended when people come along with their endless wish lists for plugins and features.

IMO having dealt with OpenGL and DirectX, OpenGL is a lot easier to deal with and I recommend it over DirectX even though I understand and appreciate the use for interface pointers and redirected structures through the whole COM approach of design (which I think is really good by the way).
grantwilliams
#7
Nov10-12, 07:35 PM
P: 65
Yes I was referring to 2D/3D Math/Physics/Engineering vectors. Is there a book I could read about how vectors can be implemented in C++?

I am starting research and using computation to solve problems and I sort of understand how I could implement vectors in C++, but I would love to be able to really learn them and establish a nice foundation for the computation I will be doing later.
chiro
#8
Nov10-12, 11:51 PM
P: 4,573
Quote Quote by grantwilliams View Post
Yes I was referring to 2D/3D Math/Physics/Engineering vectors. Is there a book I could read about how vectors can be implemented in C++?

I am starting research and using computation to solve problems and I sort of understand how I could implement vectors in C++, but I would love to be able to really learn them and establish a nice foundation for the computation I will be doing later.
There are tonnes of books talking about implementing vector libraries including a lot of game design books, computer graphics books, and computational science/engineering books.

As I said before though, a lot of free implementations can be found online already.

All the open-source game engines, visualization and rendering platforms, computational science and engineering packages, and others have classes for vectors and matrices with a wide range of features and optimizations relevant to that particular platform.

Game engines and high performance platforms will have for example, code with assembly routines for matrix multiplication because games have to do tonnes of these operations every frame so they need to have optimized code.

The OGRE library is a large open source game engine that will have these as well Irrlicht which is another open source game engine.

The Intel website has PDF's for optimized matrix multiplication in SSE and SSE2 for various common matrix sizes (3x3 up to 6x6).

I would personally figure out some of your requirements first with regards to features and performance characteristics and then search for a few implementations: I have given two suggestions and the code in OGRE should be very well developed for those libraries.

There are tonnes of open source repositories with matrix and vector routines but what I recommend is that you get one from a very large complex project and the reason is that they will have included the features in their classes that are required to do the breadth of things that the platform needs to do, and game engines need to do a lot of stuff.

If someone writes a stand-alone piece of code simply as an exercise and is used only in isolation, then it won't have this.

It's not to say that all individual submissions are in isolation and are purely theoretical exercises: a lot of submissions are just a few files from a massive project but if you have the entire project, then you can at least see how a particular class supports the rest of the platform.

Here is the link:

http://www.ogre3d.org/
grantwilliams
#9
Nov11-12, 02:17 AM
P: 65
Thank you very much Chiro, you seem very knowledgable and I really appreciate your help
D H
#11
Nov12-12, 02:34 PM
Mentor
P: 15,170
Quote Quote by chiro View Post
All of the above overload the numerical operators so you can do vector addition, vector subtraction, and scalar multiplication using good old +, -, and *. For example, x = a*(u+v) sets x to the sum of vectors u and v, scaled by some scalar a. This is very nice, but there is a hidden problem here. The problem is that the overloaded operators in C++ can spell the ruin of an application if done incorrectly. The above are examples of how *not* to do things. You certainly don't want to have to be forced to make your graphics and AI so simplified just so that the game can run on a typical computer that no one will buy your game. The naive implementations of the overloaded operators may force exactly that kind of tradeoff.

Doing things right so that there isn't a whole lot of allocation, copying, and deallocation going on takes a lot of work. One approach is to use expression templates to implement lazy evaluation. This is how the Eigen package (http://eigen.tuxfamily.org/index.php?title=Main_Page) and Boost uBLAS (http://www.boost.org/doc/libs/releas.../doc/index.htm) overcome some basic performance issues with C++ and operator overloading. Sometimes lazy evaluation will get you into trouble, and at least the Eigen package tries to be pretty smart about those issues.


Register to reply

Related Discussions
CFD tutorial Mechanical Engineering 4
C# Tutorial Programming & Computer Science 13
Position Vectors, Velocity Vectors, and Acceleration Vectors Introductory Physics Homework 3
GR Tutorial Special & General Relativity 2
Tutorial: Vectors Physics Learning Materials 3