Note that there are some explanatory texts on larger screens.

plurals
  1. POcollision of a platformer (like mario bros) not working
    text
    copied!<p>i've been sitting here for hours now trying to figure out how collision could work for a platformer.. after doing a lot of research i found something useful in the internet.. but it doesn't work yet :/ my character seems to jump one time.. and then gets stuck a short distance above the ground</p> <p>This is the function where i load my platforms from txt (by calling setupworld) in my txt i define xstart (where the platform starts) xend (end of platform) ystart (bottom of platform) 2 unnused variables and the texture filter (0-4 atm)</p> <p>each platform is then created by repeating 2x2 tiles in x direction numblocks= number of platforms (sry for the bad variable name ^^) number of blocks is calculated by taking the end coordinate of the platform - start coordinate and dividing by 2.0 (my platforms always have coordinates dividable by 2.. like.. 0 - 16.. or.. 8 - 16.. )</p> <p>as u see.. the structure block is where all the data is saved to in setupworld() and it has nothing to do with the number of tiles displayed.. sry again for my weird names</p> <hr> <pre><code>GLvoid BuildLists() </code></pre> <p>{</p> <pre><code>texture[0]=LoadPNG("data/grass.png"); texture[1]=LoadPNG("data/gnd.png"); texture[2]=LoadPNG("data/pilz_test.png"); texture[3]=LoadPNG("data/rockwall.png"); texture[4]=LoadPNG("data/crate.png"); setupworld(); quad[0]=glGenLists(numblocks); for(int loop=0;loop&lt;numblocks;loop++) { GLfloat xstart,xend,ystart,u,v,u2,v2; xstart=block.data[loop].xstart; xend=block.data[loop].xend; ystart=block.data[loop].ystart; //u=block.data[loop].u; //v=block.data[loop].v; GLuint filter=block.data[loop].filter; GLfloat blocks=(xend-xstart)/2.0f; u=0.0f; v=0.0f; u2=1.0f*blocks; v2=1.0f; glNewList(quad[loop],GL_COMPILE); glBindTexture(GL_TEXTURE_2D, texture[filter]); // Start Drawing Quads for(int y=0;y&lt;blocks;y++) { glBegin(GL_QUADS); glTexCoord2f(u,v); glVertex3f(xstart,ystart,-1.0f); glTexCoord2f(u2,v); glVertex3f(xstart+((y+1)*2.0f),ystart,-1.0f); glTexCoord2f(u2,v2); glVertex3f(xstart+((y+1)*2.0f),ystart+2.0f,-1.0f); glTexCoord2f(u,v2); glVertex3f(xstart,ystart+2.0f,-1.0f); glEnd(); } glEndList(); quad[loop+1]=quad[loop]+1; } </code></pre> <p>}</p> <hr> <p>This is where the key actions are processed.. DetectCollision() calls my function where i (try to) check for collisions</p> <p>ymovement ypos2=ypos; is just to remind the last position for repositioning i jump until i reach 5.0f or a collision in y-direction is detected.. then locky turns TRUE; the else is to let my character get back to ground (cause no gravity) even when the player keeps w pressed the same thing happens when w is not pressed.. and locky is being reset</p> <p>xmovement i add to (or subtract from) xpos for movement</p> <h2>as long as the character doesn't reach the border or a collision appears it should do the normal movement</h2> <pre><code>if (active) // Program Active? { if (keys[VK_ESCAPE]) { done=TRUE; glDeleteTextures(1,&amp;texture[0]); glDeleteTextures(1,&amp;texture[2]); glDeleteTextures(1,&amp;texture[1]); } if (keys['W']) { if(!locky) { DetectCollision(); ypos2=ypos; ypos=ypos+0.2f; if(ypos&gt;=5.0f) { locky=!locky; } if(collisiony) { ypos=ypos2; locky=!locky; } } else { if(ypos&gt;0.0f &amp;&amp; !collisiony) { ypos=ypos-0.2f; } } } if (!keys['W']) { locky=!locky; if(ypos&gt;0.0f &amp;&amp; !collisiony) { ypos=ypos-0.2f; } } if (keys['A']) { if(xpos&gt;0.0f &amp;&amp; !collisionx) { xpos=xpos-0.2f; } } if (keys['D']) { if(xpos&lt; 50.0f &amp;&amp; !collisionx) { xpos=xpos+0.2f; xcam=xcam-0.1f; } } glLoadIdentity(); glTranslatef(0,-7.0f,-25.0f); </code></pre> <p>DrawWorld(); //draws my platforms by calling the display lists compiled in build lists<br> DrawChar(); //draws the character</p> <pre><code> SwapBuffers(hDC); } </code></pre> <hr> <p>Finally the code where i check for collisions</p> <p>inPlatformx for checking x</p> <p>is my character between left and right side of the platform being checked -> function returns TRUE and is written into collisionx</p> <p>inPlatformy for checking y</p> <p>same for inPlatformy</p> <hr> <pre><code>bool inPlatformx(float xpos, BLOCK block, int i){ return xpos &gt; block.data[i].xstart &amp;&amp; xpos &lt; block.data[i].xend;} bool inPlatformy(float ypos, BLOCK block, int i){ return ypos &gt; block.data[i].ystart &amp;&amp; ypos &lt; (block.data[i].ystart+0.2); } GLvoid DetectCollision(){ for(int i=0; i&lt;numblocks;i++) { collisionx=inPlatformx(xpos,block,i); collisiony=inPlatformy(ypos,block,i); } </code></pre> <p>}</p> <hr> <p>finally a screenshot <a href="http://www.grenzlandzocker.de/test.png" rel="nofollow">http://www.grenzlandzocker.de/test.png</a></p> <p>I hope u can help me.. either fix my code or give me some tips on collisions.. since it's my first game with opengl :s if u need any more detail or infos please ask ^^ and thanks in advance !</p> <hr> <pre><code> if (keys['W']) { //DetectCollision(); if(!locky) { ypos2=ypos; ypos=ypos+0.2f; if(ypos&gt;=5.0f) { locky=!locky; } if(collisiony) { ypos=ypos2; locky=!locky; } } else { if(ypos&gt;0.0f) { ypos=ypos-0.2f; } } } if (!keys['W']) { locky=!locky; if(ypos&gt;0.0f &amp;&amp; !collisiony) { ypos=ypos-0.2f; } } if (keys['A']) { //DetectCollision(); if(xpos&gt;0.0f &amp;&amp; !collisionx) { xpos2=xpos; xpos=xpos-0.2f; } if(collisionx) { xpos=xpos2; } } if (keys['D']) { //DetectCollision(); if(xpos&lt; 50.0f &amp;&amp; !collisionx) { xpos2=xpos; xpos=xpos+0.2f; xcam=xcam-0.1f; } if(collisionx) { xpos=xpos2; } } </code></pre> <p>THANKS :) well i just edited the code for x movement and collision.. so it works properly.. (despite the flickering (but idc atm :) ).. i'm wondering why jumping doesn't work at all anymore.. it's just increasing till i'm stuck.. can't even get up once anymore :/ i put my detectCollision() in front of they key section.. which works for x AND y now (hopefully).. and i also edited something in DetectCollision() i had ystart+0.2 instead of ystart+2.0 which is the correct coordinate for the platforms top</p> <p>but looks like it just got worse at y i'm still totally confused..</p> <p>oh and talking about those predefined api's and stuff.. since somebody first showed me some things with glut i tried to make the next step and initialize everything myself.. i guess detection is much easier using predefined stuff.. but i want to learn it and not just get one game to work :) if u have any more suggestions for my code i would be really grateful.. and if u know any good books for starters till advanced lvl i would really appreciate</p> <p>and yes.. i know.. display lists are deprecated since opengl 3.0 ^^</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload