Search Results

Search found 2562 results on 103 pages for 'vector'.

Page 15/103 | < Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >

  • C# 2D Vector Graphics Game using DirectX or OpenGL?

    - by Brian
    Hey Guys, So it has been a while since I have done any game programming in C#, but I have had a recent bug to get back into it, and I wanted some opinions on what configuration I should use. I wanted to use C# as that is what I use for work, and have become vary familiar with. I have worked with both DirectX and OpenGL in the past, but mostly in 3D, but now I am interested in writing a 2D game with all vector graphics, something that resembles the look of Geometry Wars or the old Star Wars arcade game. Key points I am interested in: • Ease of use/implementation. • Easy on memory. (I plan on having a lot going on at once) • Looks good, I don't want curve to look pixelated. • Maybe some nice effects like glow or particle. I am open to any and all suggestions, maybe even something I have not thought of... Thanks in advance!

    Read the article

  • C++ a map to a 2 dimensional vector

    - by user1701545
    I want to create a C++ map where key is, say, int and value is a 2-D vector of double: map myMap; suppose I filled it and now I would like to update the second vector mapped by each key (for example divide each element by 2). How would I access that vector iteratively? The "itr-second[0]" syntax in the statement below is obviously wrong. What would be the right syntax for that action? for(std::map<in, vector<vector<double> > > itr = myMap.begin(); itr != myMap.end();++itr) { for(int i = 0;i < itr->second[0].size();++i) { itr->second[0][i] /= 2; } } thanks, rubi

    Read the article

  • Applications: The mathematics of movement, Part 1

    - by TechTwaddle
    Before you continue reading this post, a suggestion; if you haven’t read “Programming Windows Phone 7 Series” by Charles Petzold, go read it. Now. If you find 150+ pages a little too long, at least go through Chapter 5, Principles of Movement, especially the section “A Brief Review of Vectors”. This post is largely inspired from this chapter. At this point I assume you know what vectors are, how they are represented using the pair (x, y), what a unit vector is, and given a vector how you would normalize the vector to get a unit vector. Our task in this post is simple, a marble is drawn at a point on the screen, the user clicks at a random point on the device, say (destX, destY), and our program makes the marble move towards that point and stop when it is reached. The tricky part of this task is the word “towards”, it adds a direction to our problem. Making a marble bounce around the screen is simple, all you have to do is keep incrementing the X and Y co-ordinates by a certain amount and handle the boundary conditions. Here, however, we need to find out exactly how to increment the X and Y values, so that the marble appears to move towards the point where the user clicked. And this is where vectors can be so helpful. The code I’ll show you here is not ideal, we’ll be working with C# on Windows Mobile 6.x, so there is no built-in vector class that I can use, though I could have written one and done all the math inside the class. I think it is trivial to the actual problem that we are trying to solve and can be done pretty easily once you know what’s going on behind the scenes. In other words, this is an excuse for me being lazy. The first approach, uses the function Atan2() to solve the “towards” part of the problem. Atan2() takes a point (x, y) as input, Atan2(y, x), note that y goes first, and then it returns an angle in radians. What angle you ask. Imagine a line from the origin (0, 0), to the point (x, y). The angle which Atan2 returns is the angle the positive X-axis makes with that line, measured clockwise. The figure below makes it clear, wiki has good details about Atan2(), give it a read. The pair (x, y) also denotes a vector. A vector whose magnitude is the length of that line, which is Sqrt(x*x + y*y), and a direction ?, as measured from positive X axis clockwise. If you’ve read that chapter from Charles Petzold’s book, this much should be clear. Now Sine and Cosine of the angle ? are special. Cosine(?) divides x by the vectors length (adjacent by hypotenuse), thus giving us a unit vector along the X direction. And Sine(?) divides y by the vectors length (opposite by hypotenuse), thus giving us a unit vector along the Y direction. Therefore the vector represented by the pair (cos(?), sin(?)), is the unit vector (or normalization) of the vector (x, y). This unit vector has a length of 1 (remember sin2(?) + cos2(?) = 1 ?), and a direction which is the same as vector (x, y). Now if I multiply this unit vector by some amount, then I will always get a point which is a certain distance away from the origin, but, more importantly, the point will always be on that line. For example, if I multiply the unit vector with the length of the line, I get the point (x, y). Thus, all we have to do to move the marble towards our destination point, is to multiply the unit vector by a certain amount each time and draw the marble, and the marble will magically move towards the click point. Now time for some code. The application, uses a timer based frame draw method to draw the marble on the screen. The timer is disabled initially and whenever the user clicks on the screen, the timer is enabled. The callback function for the timer follows the standard Update and Draw cycle. private double totLenToTravelSqrd = 0; private double startPosX = 0, startPosY = 0; private double destX = 0, destY = 0; private void Form1_MouseUp(object sender, MouseEventArgs e) {     destX = e.X;     destY = e.Y;     double x = marble1.x - destX;     double y = marble1.y - destY;     //calculate the total length to be travelled     totLenToTravelSqrd = x * x + y * y;     //store the start position of the marble     startPosX = marble1.x;     startPosY = marble1.y;     timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) {     UpdatePosition();     DrawMarble(); } Form1_MouseUp() method is called when ever the user touches and releases the screen. In this function we save the click point in destX and destY, this is the destination point for the marble and we also enable the timer. We store a few more values which we will use in the UpdatePosition() method to detect when the marble has reached the destination and stop the timer. So we store the start position of the marble and the square of the total length to be travelled. I’ll leave out the term ‘sqrd’ when speaking of lengths from now on. The time out interval of the timer is set to 40ms, thus giving us a frame rate of about ~25fps. In the timer callback, we update the marble position and draw the marble. We know what DrawMarble() does, so here, we’ll only look at how UpdatePosition() is implemented; private void UpdatePosition() {     //the vector (x, y)     double x = destX - marble1.x;     double y = destY - marble1.y;     double incrX=0, incrY=0;     double distanceSqrd=0;     double speed = 6;     //distance between destination and current position, before updating marble position     distanceSqrd = x * x + y * y;     double angle = Math.Atan2(y, x);     //Cos and Sin give us the unit vector, 6 is the value we use to magnify the unit vector along the same direction     incrX = speed * Math.Cos(angle);     incrY = speed * Math.Sin(angle);     marble1.x += incrX;     marble1.y += incrY;     //check for bounds     if ((int)marble1.x < MinX + marbleWidth / 2)     {         marble1.x = MinX + marbleWidth / 2;     }     else if ((int)marble1.x > (MaxX - marbleWidth / 2))     {         marble1.x = MaxX - marbleWidth / 2;     }     if ((int)marble1.y < MinY + marbleHeight / 2)     {         marble1.y = MinY + marbleHeight / 2;     }     else if ((int)marble1.y > (MaxY - marbleHeight / 2))     {         marble1.y = MaxY - marbleHeight / 2;     }     //distance between destination and current point, after updating marble position     x = destX - marble1.x;     y = destY - marble1.y;     double newDistanceSqrd = x * x + y * y;     //length from start point to current marble position     x = startPosX - (marble1.x);     y = startPosY - (marble1.y);     double lenTraveledSqrd = x * x + y * y;     //check for end conditions     if ((int)lenTraveledSqrd >= (int)totLenToTravelSqrd)     {         System.Console.WriteLine("Stopping because destination reached");         timer1.Enabled = false;     }     else if (Math.Abs((int)distanceSqrd - (int)newDistanceSqrd) < 4)     {         System.Console.WriteLine("Stopping because no change in Old and New position");         timer1.Enabled = false;     } } Ok, so in this function, first we subtract the current marble position from the destination point to give us a vector. The first three lines of the function construct this vector (x, y). The vector (x, y) has the same length as the line from (marble1.x, marble1.y) to (destX, destY) and is in the direction pointing from (marble1.x, marble1.y) to (destX, destY). Note that marble1.x and marble1.y denote the center point of the marble. Then we use Atan2() to get the angle which this vector makes with the positive X axis and use Cosine() and Sine() of that angle to get the unit vector along that same direction. We multiply this unit vector with 6, to get the values which the position of the marble should be incremented by. This variable, speed, can be experimented with and determines how fast the marble moves towards the destination. After this, we check for bounds to make sure that the marble stays within the screen limits and finally we check for the end condition and stop the timer. The end condition has two parts to it. The first case is the normal case, where the user clicks well inside the screen. Here, we stop when the total length travelled by the marble is greater than or equal to the total length to be travelled. Simple enough. The second case is when the user clicks on the very corners of the screen. Like I said before, the values marble1.x and marble1.y denote the center point of the marble. When the user clicks on the corner, the marble moves towards the point, and after some time tries to go outside of the screen, this is when the bounds checking comes into play and corrects the marble position so that the marble stays inside the screen. In this case the marble will never travel a distance of totLenToTravelSqrd, because of the correction is its position. So here we detect the end condition when there is not much change in marbles position. I use the value 4 in the second condition above. After experimenting with a few values, 4 seemed to work okay. There is a small thing missing in the code above. In the normal case, case 1, when the update method runs for the last time, marble position over shoots the destination point. This happens because the position is incremented in steps (which are not small enough), so in this case too, we should have corrected the marble position, so that the center point of the marble sits exactly on top of the destination point. I’ll add this later and update the post. This has been a pretty long post already, so I’ll leave you with a video of how this program looks while running. Notice in the video that the marble moves like a bot, without any grace what so ever. And that is because the speed of the marble is fixed at 6. In the next post we will see how to make the marble move a little more elegantly. And also, if Atan2(), Sine() and Cosine() are a little too much to digest, we’ll see how to achieve the same effect without using them, in the next to next post maybe. Ciao!

    Read the article

  • Problem with passing vector of pointers to objects to member function of another object

    - by Jamesz
    Hi, I have a vector of pointers to Mouse objects called 'mice'. I'm passing the mice to the cat by reference. vector <Mouse*> mice; Cat * c; c->lookForMouse(&mice); And here's my lookForMouse() member function void Cat::lookForMouse(vector <Mouse*> *mice) { ... } And now to the problem! Within the function above, I can't seem to access my mice. This below will not work mice[i]->isActive(); The error message I receive suggests to use mice[i].isActive(), but this throws an error saying isActive() is not a member of std::vector<_Ty ... This works though... vector <Mouse*> miceCopy = *mice; miceCopy[i]->isActive(); I understand that I shouldn't be creating another vector of mice here, it defeats the whole point of passing it by reference (let me know if I'm wrong)... Why can't I do mice[i]-isActive() What should I be doing? Thanks for your time and help :D James.

    Read the article

  • returning a pointed to an object within a std::vector

    - by memC
    I have a very basic question on returning a reference to an element of a vector . There is a vector vec that stores instances of class Foo. I want to access an element from this vector . ( don't want to use the vector index) . How should I code the method getFoo here? #include<vector> #include<stdio.h> #include<iostream> #include<math.h> using namespace std; class Foo { public: Foo(){}; ~Foo(){}; }; class B { public: vector<Foo> vec; Foo* getFoo(); B(){}; ~B(){}; }; Foo* B::getFoo(){ int i; vec.push_back(Foo()); i = vec.size() - 1; // how to return a pointer to vec[i] ?? return vec.at(i); }; int main(){ B b; b = B(); int i = 0; for (i = 0; i < 5; i ++){ b.getFoo(); } return 0; }

    Read the article

  • C++ Vector of vectors is messing with me

    - by xbonez
    If I put this code in a .cpp file and run it, it runs just fine: #include <iostream> #include <vector> #include <string> using namespace std; typedef vector<int> row; typedef vector<row> myMatrix; void main() { //cout << endl << "test" << endl; myMatrix mat(2,2); mat[0][1] = 2; cout << endl << mat[0][1] << endl; } But, if I make a .h and a .cpp file with the .h file like this, it gives me boatloads of errors. #ifndef _grid_ #define _grid_ #include<iostream> #include<vector> #include<string> using namespace std; typedef vector<int> row; typedef vector<row> myMatrix; class grid { public: grid(); ~grid(); int getElement(unsigned int ri, unsigned int ci); bool setElement(unsigned int ri, unsigned int ci, unsigned int value); private: myMatrix sudoku_(9,9); }; #endif These are some of the errors I get: warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    Read the article

  • Using NumPy arrays as 2D mathematical vectors?

    - by CorundumGames
    Right now I'm using lists as position, velocity, and acceleration vectors in my game. Is that a better option than using NumPy's arrays (not the standard library's) as vectors (with float data types)? I'm frequently adding vectors and changing their values directly, then placing the values in these vectors into a Pygame Rect. The vector is used for position (because Rects can't hold floats, so we can't go "between" pixels), and the Rect is used for rendering (because Pygame will only take in Rects for rendering positions).

    Read the article

  • C++ Directx 11 D3DXVECTOR3 doesn't allow me to devide it

    - by Miguel P
    If i have a simple vector3 like this: D3DXVECTOR3 inversevector = D3DXVECTOR3( (pos+lookat_pos)); It works perfect! But let's say i wanted to multiply it by: Speed*(float) timeHandler.GetDelta() So: D3DXVECTOR3 inversevector = D3DXVECTOR3( (pos+lookat_pos) * Speed*(float) timeHandler.GetDelta()); Now this fails completely, i've used this snippet before, but for some wierd reason it simply won't work( The vector somehow leads x,y,z to 0 or almost, no idea why). Do you have any idea why?

    Read the article

  • Strange problem with vectors.

    - by Catalin Dumitru
    I have a really strange problem with stl vectors in which the wrong destructor is called for the right object when I call the erase method if that makes any sense. My code looks something like this: for(vector<Category>::iterator iter = this->children.begin(); iter != this->children.end(); iter++) { if((*iter).item == item) { this->children.erase(iter); return; } ------------------------- } It's just a simple function that finds the element in the vector which has some item to be searched, and removes said element from the vector. My problem is than when the erase function is called, and thus the object which the iterator is pointing at is being destroyed, the wrong destructor is being called. More specific the destructor of the last element in the vector is being called, and not of the actual object being removed. Thus the memory is being removed from the wrong object, which will still be an element in the vector, and the actual object which is removed from the vector, still has all of it's memory intact. The costructor of the object looks like this: Category::Category(const Category &from) { this->name = from.name; for(vector<Category>::const_iterator iter = from.children.begin(); iter != from.children.end(); iter++) this->children.push_back((*iter)); this->item = new QTreeWidgetItem; } And the destructor Category::~Category() { this->children.clear(); if(this->item != NULL) { QTreeWidgetItem* parent = this->item->parent(); if(parent != NULL) parent->removeChild(this->item); delete this->item; } }

    Read the article

  • Routing protocols, distance vector vs link state

    - by Artem Barger
    I'm trying to figure out the differences(pros/cons) between two routing protocols approach and I would be great-full for any help, advice and explanation. As far I can say that it seems like distance vector is more static and more local based routing, since it doesn't know the network state whereas link state is more aware of current states therefore it seems more natural to use it over distance-vector, but I have a feeling like I'm missing something. And I would be glad to here about more aspects and different issues I have to consider while choosing one of them.

    Read the article

  • VG.net 8.5 Released

    - by Frank Hileman
    We have released version 8.5 of the VG.net vector graphics system. This release supports Visual Studio 2013. Companies who purchased a VG.net license after October 1, 2013, are eligible for a free upgrade. We will be sending you an email. There is one cosmetic problem which wasted our time, as we could not find a work around. It occurs when your display is set to a high DPI. You can see the problem in the image of the toolbox below, which uses a DPI of 125%, on Windows 7: The ToolboxItem class accepts only Bitmaps with a size of 16x16. We tried many sizes and many bitmap formats. As you can see, this tiny Bitmap is then scaled by the toolbox, and the scaling algorithm adds artifacts. This is an "improvement" Microsoft recently added to Visual Studio 2013.

    Read the article

  • Converting 3 axis vectors to a rotation matrix

    - by user38858
    I am trying to get a rotation matrix (in 3dsmax) from 3 vectors that form an axis (all 3 vectors are aligned by 90 degrees each other) Somewhere I read that I could build a rotation matrix just by inserting in every row one vector at a time (source: http://renderdan.blogspot.cz/2006/05/rotation-matrix-from-axis-vectors.html) So, I built a matrix with these example vectors x-axis : [-0.194624,-0.23715,-0.951778] y-axis : [-0.773012,0.634392,0] z-axis : [-0.6038,-0.735735,0.306788] But for some reason, if I try to convert this matrix to eulerangles, I receive this rotation: (eulerAngles 47.7284 6.12831 36.8263) ... which is totally wrong, and doesn't align to my 3 vectors at all. I know that rotation is quite difficult to understand, may someone shed some light? :)

    Read the article

  • For normal mapping, why can we not simply add the tangent normal to the surface normal?

    - by sebf
    I am looking at implementing bump mapping (which in all implementations I have seen is really normal mapping), and so far all I have read says that to do this, we create a matrix to convert from world-space to tangent-space, in order to transform the lights and eye direction vectors into tangent space, so that the vectors from the normal map may be used directly in place of those passed through from the vertex shader. What I do not understand though, is why we cannot just use the normalised sum of the sampled-normal vector, and the surface-normal? (assuming we already transform and pass through the surface normal for the existing lighting functions) Take the diagram below; the normal is simply the deviation from the 'reference normal' for any given coordinate system, correct? And transforming the surface normal of a mapped surface from world space to tangent space makes it equivalent to the tangent space 'reference normal', no? If so, why do we transform all lighting vectors into tangent space, instead of simply transforming the sampled tangent once in the pixel shader?

    Read the article

  • Component-wise GLSL vector branching

    - by Gustavo Maciel
    I'm aware that it usually is a BAD idea to operate separately on GLSL vec's components separately. For example: //use instrinsic functions, they do the calculation on 4 components at a time. float dot = v1.x*v2.x + v1.y * v2.y + v1.z * v2.z; //NEVER float dot = dot(v1, v2); //YES //Multiply one by one is not good too, since the ALU can do the 4 components at a time too. vec3 mul = vec3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); //NEVER vec3 mul = v1 * v2; I've been struggling thinking, are there equivalent operations for branching? For example: vec4 Overlay(vec4 v1, vec4 v2, vec4 opacity) { bvec4 less = lessThan(v1, vec4(0.5)); vec4 blend; for(int i = 0; i < 4; ++i) { if(less[i]) blend[i] = 2.0 * v1[i]*v2[i]; else blend[i] = 1.0 - 2.0 * (1.0 - v1[i])*(1.0 - v2[i]); } return v1 + (blend-v1)*opacity; } This is a Overlay operator that works component wise. I'm not sure if this is the best way to do it, since I'm afraid these for and if can be a bottleneck later. Tl;dr, Can I branch component wise? If yes, how can I optimize that Overlay function with it?

    Read the article

  • Translating an Object to a certain Vector 3 in OpenGL and Java LWJGL

    - by aliasmk
    So after almost two hours, I got the hang of using glTranslated() (with Java and LWJGL). If I am correct, applying glTranslated on an object moves that object with the x,y,z relative to the previously moved object. I believe the correct term for this is local vs global, global being the one I want. I was wondering if there was a way to translate an object to a specific XYZ position, or relative to the origin. Thanks! (Code or other details can be supplied if it helps, just let me know. Also sorry if this is a noob comment, Im very new to OpenGL.)

    Read the article

  • How do I calculate opposite of a vector, add some slack

    - by Jason94
    How can i calulate a valid range (RED) for my object's (BLACK) traveling direction (GREEN). The green is a Vector2 where x and y range is -1 to 1. What I'm trying to do here is to create rocket fuel burn effekt. So what i got is rocket speed (float) rocket direction (Vector2 x = [-1, 1], y = [-1, 1]) I may think that rocket speed does not matter as fuel burn effect (particle) is created on position with its own speed.

    Read the article

  • 3d trajectory - calculate initial velocity

    - by Skoder
    Hey, I've got a 2D projectile code sample working, but would like to extend it to 3D. How would I calculate the initial velocity of the Z-axis? At the moment, I've got: initVel.X = (float)Math.Cos(45.0); initVel.Y = (float)Math.Sin(45.0); How would I convert this to work in 3D (and add the initial velocity for the Z-axis)? In my example, X is across, Y is up down and Z is going into the screen. I also normalize the vector and multiply it by the speed. Thanks

    Read the article

  • How do I implement a selectable world map?

    - by Clay
    I want to have a selectable map of the world, preferably zoomable, in a cocos2d project. When I tap on a country, I want that country to be selected so that I can perform some other operations with it. It seems that the best approach would be to use a vector world map, but I'm unsure how to implement this with cocos2d. Other options include using map tiles, but it seems that still would require the implementation of country polygons for tap/click detection. Depending on user input, I want to add icons to various countries on the map. What is a good way to approach the implementation of this type of map?

    Read the article

  • Calculating angle between 2 vectors

    - by Error 454
    I am working on some movement AI where there are no obstacles and movement is restricted to the XY plane. I am calculating 2 vectors: v - the direction of ship 1 w - the vector pointing from the position of ship 1 to ship 2 I am then calculating the angle between these two vectors using the standard formula: arccos( v . w / ( |v| |w| ) ) The problem I'm having is the nature of arccos only returning values between 0 and 180. This makes it impossible to determine whether I should turn left or right to face the other ship. Is there a better way to do this?

    Read the article

  • Slide 2d Vector to destination over a period of time

    - by SchautDollar
    I am making a library of GUI controls for games I make with XNA. I am currently developing the library as I make a game so I can test the features and find errors/bugs and hopefully smash them right away. My current issue is on a slide feature I want to implement for my base class that all controls inherit. My goal is to get the control to slide to a specified point over a specified amount of time. Here is the #region containing the code #region Slide private bool sliding; private Vector2 endPoint; private float slideTimeLeft; private float speed; private bool wasEnabled; private Vector2 slideDirection; private float slideDistance; public void Slide(Vector2 startPoint, Vector2 endPoint, float slideTime) { this.location = startPoint; Slide(endPoint,slideTime); } public void Slide(Vector2 endPoint, float slideTime) { this.wasEnabled = this.enabled; this.enabled = false; this.sliding = true; Vector2 tempLength = endPoint - this.location; this.slideDistance = tempLength.Length(); //Was this.slideDistance = (float)Math.Sqrt(tempLength.LengthSquared()); this.speed = slideTime / this.slideDistance; this.endPoint = endPoint; this.slideTimeLeft = slideTime; } private void UpdateSlide(GameTime gameTime) { if (this.sliding) { this.slideTimeLeft -= gameTime.ElapsedGameTime.Milliseconds; if (this.slideTimeLeft >= 0 ) { if ((this.endPoint-this.location).Length() != 0){//Was if (this.endPoint.LengthSquared() > 0 || this.location.LengthSquared() > 0) { this.slideDirection = Vector2.Normalize(this.endPoint - this.location); } this.location += this.slideDirection * speed * gameTime.ElapsedGameTime.Milliseconds;//This is where I believe the issue is, but I'm not sure. It seems right to me... (Even though it doesn't work) } else { this.enabled = this.wasEnabled; this.location = this.endPoint;//After time, the controls position will get set to be the endpoint. this.sliding = false; } } } #endregion this.location is the location of the control elsewhere defined in the class. I have looked at this blog as a huge reference and have googled around quite and have looked on many forums but can't find anything that shows how to implement it. Please and Thanks for your time! EDIT: I have switched this line "this.location += this.slideDirection * speed * gameTime.ElapsedGameTime.Milliseconds;" several times to see what it does. My issue is getting the control to smoothly move to the end location. It moves after the time has expired, but It doesn't move other then that except flash in my face. EDIT2: I have used the first slide method with 3 parameters and it works except it doesn't do it in a period of time and once it gets to its destination, it starts moving randomly towards the previous location and the end location.

    Read the article

  • How can I calculate the angle between two 2D vectors?

    - by Error 454
    I am working on some movement AI where there are no obstacles and movement is restricted to the XY plane. I am calculating two vectors, v, the facing direction of ship 1, and w, the vector pointing from the position of ship 1 to ship 2. I am then calculating the angle between these two vectors using the formula arccos((v · w) / (|v| · |w|)) The problem I'm having is that arccos only returns values between 0° and 180°. This makes it impossible to determine whether I should turn left or right to face the other ship. Is there a better way to do this?

    Read the article

  • How to pronounce "std" as in "std::vector"

    - by Lex Fridman
    In C++, the STL (standard template library) includes a namespace std that contains the many data structures and algorithms that we all know and love. I've always pronounced this namespace just like sexually transmitted diseases: S T D. But then I listened to this excellent series of lectures by Stephan T. Lavavej and he pronounces it "stood". Which is the "correct" pronunciation or at least what is that most commonly used one?

    Read the article

  • iPhone OpenGL ES: How do I use gravity vector to correctly transform scene for augmented reality

    - by gpdawson
    I'm trying figure out how to get an OpenGL specified object to be displayed correctly according to the device orientation (ie. according to the gravity vector from the accelerometer, and heading from compass). The GLGravity sample project has an example which is almost like this (despite ignoring heading), but it has some glitches. For example, the teapot jumps 180deg as the device viewing angle crosses the horizon, and it also rotates spuriously if you tilt the device from portrait into landscape. This is fine for the context of this app, as it just shows off an object and it doesn't matter that it does these things. But it means that the code just doesn't work when you attempt to emulate real life viewing of an OpenGL object according to the device's orientation. What happens is that it almost works, but the heading rotation you apply from the compass gets "corrupted" by the spurious additional rotations seen in the GLGravity example project. Can anyone provide sample code that shows how to adjust correctly for the device orientation (ie. gravity vector), or to fix the GLGravity example so that it doesn't include spurious heading changes? //Clear matrix to be used to rotate from the current referential to one based on the gravity vector bzero(matrix, sizeof(matrix)); matrix[3][3] = 1.0; //Setup first matrix column as gravity vector matrix[0][0] = accel[0] / length; matrix[0][1] = accel[1] / length; matrix[0][2] = accel[2] / length; //Setup second matrix column as an arbitrary vector in the plane perpendicular to the gravity vector {Gx, Gy, Gz} defined by by the equation "Gx * x + Gy * y + Gz * z = 0" in which we arbitrarily set x=0 and y=1 matrix[1][0] = 0.0; matrix[1][1] = 1.0; matrix[1][2] = -accel[1] / accel[2]; length = sqrtf(matrix[1][0] * matrix[1][0] + matrix[1][1] * matrix[1][1] + matrix[1][2] * matrix[1][2]); matrix[1][0] /= length; matrix[1][1] /= length; matrix[1][2] /= length; //Setup third matrix column as the cross product of the first two matrix[2][0] = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]; matrix[2][1] = matrix[1][0] * matrix[0][2] - matrix[1][2] * matrix[0][0]; matrix[2][2] = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]; //Finally load matrix glMultMatrixf((GLfloat*)matrix);

    Read the article

  • Fastest way to write large STL vector to file using STL

    - by ljubak
    I have a large vector (10^9 elements) of chars, and I was wondering what is the fastest way to write such vector to a file. So far I've been using next code: vector<char> vs; // ... Fill vector with data ofstream outfile("nanocube.txt", ios::out | ios::binary); ostream_iterator<char> oi(outfile, '\0'); copy(vs.begin(), vs.end(), oi); For this code it takes approximately two minutes to write all data to file. The actual question is: "Can I make it faster using STL and how"?

    Read the article

< Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >