• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

114 Neutral

About JoakimNordstrandStien

  • Rank
  1. Individual vertex coloring is somehow disabled.

    That worked! Thank you so much! I just hate it when I run into these speed bumps, which quite frankly is caused by my own lack of attention. Best regards, Pimms.
  2. Edit: The fix is in: I had called glShadeModel(GL_FLAT) at some point in my code, because I was trying to derp while herping whilst writing it. The fix was to change GL_FLAT with GL_SMOOTH. Thanks to Sponji for pointing that out. --- Good day! I've been struggling to draw primitives correctly in my engine for quite a few hours, and I've managed to narrow it down to the fact that each vertex I'm passing along isn't colored individually. The following snippet and screenshot demonstrates my issue. [CODE] glDisable(GL_TEXTURE_2D); glBegin(GL_QUADS); glColor3f(0.f,1.f,0.f); // Green glVertex2f(-50.f, -50.f); glVertex2f(50.f, -50.f); glVertex2f(50.f, 50.f); glColor3f(1.f, 0.f, 0.f); // Red glVertex2f(-50.f, 50.f); glEnd(); glEnable(GL_TEXTURE_2D); [/CODE] This snippet [i]should[/i] render a square where the top left vertex is red and the other three are green. However, as can be seen in this screenshot, this is not the case. [img]http://i.imgur.com/7iB7p.png[/img] Disregard the purple sprites. Whenever I try to draw a primitive of any kind, [b]all[/b] vertices get the last color called. Apart from disabling/enabling GL_TEXTURE_2D, I'm not doing anything unusual. This issue occurs both when rendering to a texture, and when I'm not. Hope someone has experienced this issue before and can give me an aiding hand. Best regards, Pimms.
  3. Vector transformation

    Thanks again for your replies - As it turned out, a Normalized vector is in fact what I was looking for to begin with. Thank you for putting up with my ridiculously bad math knowledge. //Pimms
  4. Vector transformation

    I came up with something that works somewhat decent, thought I'd share it as it might be of help to someone out there It's messy as shit, and could use a rewrite, but I'm satisfied considering it's 2:45 AM. As mentioned, this function returns a vector scaled down to 1. [source] inline b2Vec2 scaleVecTo1( b2Vec2 vector ) { //flag for reversing them to negative before return bool invX=NO, invY=NO; float x=0.f, y=0.f; float ratio; invX = vector.x < 0.f ? YES : NO; invY = vector.y < 0.f ? YES : NO; vector.x *= vector.x < 0.f ? -1.f : 1.f; vector.y *= vector.y < 0.f ? -1.f : 1.f; if ( vector.x > vector.y ) { for (;;) { if ( vector.x < 1.0f ) { ratio = 1.f / vector.x; x = vector.x * ratio; y = vector.y * ratio; break; } if ( vector.x > 1.0f ) { ratio = vector.x / vector.x; x = vector.x / ratio; y = vector.y / ratio; break; } } } if ( vector.x <= vector.y ) { for (;;) { if ( vector.y < 1.0f ) { ratio = 1.f / vector.y; y = vector.y * ratio; x = vector.x * ratio; break; } if ( vector.y > 1.0f ) { ratio = vector.y / vector.y; y = vector.y / ratio; x = vector.x / ratio; break; } } } x *= invX ? -1.f : 1.f; y *= invY ? -1.f : 1.f; return b2Vec2(x,y); } [/source]
  5. Vector transformation

    Thanks for the quick replies, guys. Appreciate it a lot [b]@alvaro[/b] I'm not 100% sure of what you mean - what I've tried (without much success) is to first find the ratio between X and Y, based on the highest value. If the highest value is > 1.0, I divide X and Y by the highest value. This produces some funny results, though. If the highest value is < 1.0, I find the ratio by dividing 1 by the highest. Then X and Y is divided by ratio. The current (faulty) code: [source] if ( vector.x > vector.y ) { for (;;) { if ( high < 1.0f ) { ratio = 1.f / high; x = high * ratio; y = low * ratio; break; } if ( high > 1.0f ) { ratio = high / high; x = high / ratio; y = low / ratio; break; } } } [/source] [b]@Hassanbasil[/b] I've looked into normalizing the vector, but that would be quite the workaround for me, as I'm calculating the force to be applied as so; [source] body->ApplyForce( (hitVector*distance)*force, body->GetWorldCenter() ); [/source] Where hitVector is the vector I'm trying to calculate.
  6. Vector transformation

    Greetings, GameDev community! I'm currently working with Box2D, and I'm implementing an explosion-effect. Therefore, I'm in need of a direction vector to be multiplied by the force (which is based on distance). In order to make it appear as seamless as possile, I need to first transform the vector so that the highest value of the vector is 1. Let me show you; A vector of (15, 10) would be (1, 6.67). As would a vector (30,15) be (1, 0.5). Has anyone done this before and have a code snippet to share? The general theory / principle behind it would be sufficient as well. Thanks in advance //pimms
  7. Salutations! After browsing these forums for about a month, I decided to create an account, because I'm hopelessly stuck, and you guys know your stuff. I found a link a week ago (or so) to a web archive containing a "Jump n Shoot" tutorial written by jnrdev. I've been trying to build my own library based on his design on class interaction... Some parts are directly stolen, primarily because they work good, and I don't hold the skills to improve it much. The main problems I am experiencing is that it detecs a collision when starting in the middle of the screen. Only the outer edges are covered in solid tiles. If I start to move using the arrow keys, the player suddenly drops through the map, until the y value exceeds what an int can hold. I've attached a screenshot showing the console and the main game window. The Pmap class: Contains an array of type TILE which contains a pointer to an SDL_Surface, and a boolean containing whether it's solid or not. [code]class Pmap{ public: //The only one worth noticing in Pmap bool colmapxy( int x, int y ) { return Mtile[x][y].solid; } private: };[/code] Player class, main function: Interacts with the Pmap class using the "collision_ver" and "collision_hor" functions, which can be seen below. [code] void Player::think(SDL_Event &event, Pmap& map, SDL_Surface &screen) { cout<<"\n\nThinking..\n"; handleInput(event); int tilecoord; //X-axis first //Moving right if( xVel > 0) { printf("Moving right...xVel:%d, yVel:%d, x:%d, y:%d\n", xVel, yVel, x, y); if(collision_ver( x+xVel+w, y, tilecoord, map) ) x = tilecoord*40 -w-1; // else x += xVel; } //Moving left if( xVel < 0) { printf("Moving left...xVel:%d, yVel:%d, x:%d, y:%d\n", xVel, yVel, x, y); if( collision_ver( x+xVel, y, tilecoord, map)) x = (tilecoord+1) *40+1; // else x += xVel; } //Caps the xVel at SPEED if( xVel > SPEED ) xVel = SPEED; //Then Y-axis //Moving up if( yVel < 0 ) { printf("Moving up...xVel:%d, yVel:%d, x:%d, y:%d\n", xVel, yVel, x, y); //Moving up & block in the way if( collision_hor( x, y+yVel, tilecoord, map)) { y = (tilecoord+1) *40+1; yVel = 0; } //Moving up through blank spots else { y += yVel; yVel += GRAVITATION; } } //yVel > 0, hence falling or on the ground else { printf("Moving down / on ground...xVel:%d, yVel:%d, x:%d, y:%d\n", xVel, yVel, x, y); //Moving down, block in the way if( collision_hor( x, y+yVel+h, tilecoord, map)) { y = tilecoord*40-h -1; //On ground yVel = 1; } //Falling / in air else { printf("Falling...xVel:%d, yVel:%d, x:%d, y:%d\n", xVel, yVel, x, y); y += yVel; yVel += GRAVITATION; if( yVel >= TILESIZE ) yVel = TILESIZE; jumpLock = true; } } } [/code] Collision checker function: Note: there are two of these (_hor and _ver), but they are so similar I choose to only copy in one of them. Also, this one is identical to jnrdev's function, except for variable names. [code] bool Player::collision_hor(int x, int y, int &tilecoordy, Pmap& map){ int tilexpixels = x-(x%40); //calculate the x position (pixels!) of the tiles we check against int testend = x + w; //calculate the end of testing (just to save the x+w calculation each for loop) tilecoordy = y/40; //calculate the y position (map coordinates!) of the tiles we want to test int tilecoordx = tilexpixels/40; //calculate map x coordinate for first tile //loop while the start point (pixels!) of the test tile is inside the players bounding box while(tilexpixels <= testend){ if(map.colmapxy(tilecoordx, tilecoordy)) //is a solid tile is found at tilecoordx, tilecoordy? { printf("Collision(hor) at: X:%d, Y:%d\n", x, y); return true; } tilecoordx++; //increase tile x map coordinate tilexpixels+=40; //increase tile x pixel coordinate } cout<<"No hor coll\n"; return false; } [/code] Edit: For some reason, collision_ver() kept returning a wrong value, which I've corrected now, and it appears to be working. If possible, please delete this thread. The lesson I've learned is that printf / cout will save you tons of time when debugging.
  • Advertisement