# thecoast47

Member

91

## Community Reputation

255 Neutral

• Rank
Member

Recently i've been messing around with interpolation methods on scalar fields because im trying to make the scalar field of discrete points, continuous. I think I understand how bi-linear interpolation works. To my knowledge,In order to interpolate a value(using bilinear interpolation) you have to do three "lerps" using four discrete neighbor points on the field. But when it comes to higher order interpolation methods, I'm lost. I know how to use quadratic interpolation  to interpolate a value using 3 discrete points on a function. How would I use quadratic interpolation on a scalar field?
2. ## Ramer–Douglas–Peucker algorithm help

thanks for the input. Do you know any popular line/polygon simplification algorithms?    *EDIT*       I changed it so that I only iterate through p0 and p1 and now it works completely. Now I feel really,REALLY stupid.
3. ## Ramer–Douglas–Peucker algorithm help

I recently implemented a marching squares algorithm to generate a polygon for an arbitrary image.   Here is an example of my marching squares implementation:   As you can see it works,however, the marching squares algorithm gives me more data than I really need for a polygon. So a polygon simplification algorithm would be needed.   I'm currently trying to implement the Ramer–Douglas–Peucker algorithm. The problem is that I don't know what condition's to set to end the recursion. Currently, the either the algorithm runs in an infinite loop or terminates without correctly simplifying the line/polygon.   My interpretation of the algorithm is as followed: -for the current line segment compute the vertex with the farthest perpendicular distance. -if the farthest perpendicular distance is greater than the user defined "tolerance" do recursion, otherwise terminate recursion.   Can someone point out what i'm doing wrong here?   Here's my flawed code: void smoothCurve(Particle* p0, Particle* p1,float tolerance,std::vector<Particle*>& vertexList,std::vector<Particle*>& outPut , std::vector<bool>& isRejected){ Vector2 displacement; Vector2 projectionArm; Vector2 projectedPoint; Vector2 pointToLineDisp; Vector2& x0 = p0->curPos; Vector2& x1 = p1->curPos; float dot; float currentDist = 0; float maxDist = 0; unsigned int maxIndex = 0; displacement = x1- x0; //calculates maximum distance from the line segment for(unsigned int I = 1; I < vertexList.size()-1;I++){ Particle*& currentVertex = vertexList[I]; Vector2& curVertexPos= currentVertex->curPos; projectionArm = curVertexPos - x0; dot = (projectionArm*displacement)/(displacement*displacement); projectedPoint = x0 + (displacement * dot); pointToLineDisp = projectedPoint - curVertexPos; currentDist = pointToLineDisp.getMagnitude(); if(currentDist > maxDist){ maxDist = currentDist; maxIndex = I; } } if(maxDist >= tolerance && outPut[maxIndex] == NULL ){ Particle* criticalParticle = vertexList[maxIndex]; outPut[maxIndex] = criticalParticle; smoothCurve(p0,criticalParticle,tolerance,vertexList,outPut,isRejected); smoothCurve(criticalParticle,p1,tolerance,vertexList,outPut,isRejected); } } void simplifyPolygon(float tolerance,std::vector<Particle*>& vertexList){ std::vector<Particle*> outPut; std::vector<bool> isRejected; outPut.resize(vertexList.size()); isRejected.resize(vertexList.size()); for(unsigned int I = 0; I < vertexList.size();I++){ outPut[I] = NULL; isRejected[I] = false; } Particle* x0 = vertexList[0]; Particle* x1 = vertexList[vertexList.size()-1]; outPut[0] = x0; outPut[vertexList.size()-1] = x1; smoothCurve(x0,x1,10.0f,vertexList,outPut,isRejected); for(unsigned int I = 0; I < outPut.size();I++){ Particle * cp = outPut[I]; if(cp == NULL){ delete vertexList[I]; } } vertexList.clear(); for(unsigned int I = 0; I < outPut.size();I++){ Particle * cp = outPut[I]; if(cp != NULL){ vertexList.push_back(cp); } } }
4. ## reading 24 bit per pixel bitmaps issue

This is what I get when i try to render a 305x298 bitmap

6. ## Continuous collision detection

I think you should take a look at this: http://www.mediafire.com/?3d6tk67ppq3h4lb
7. ## MidPoint Method implementation issues

You're probably right about that. I'm very new to numerical methods in general.
8. ## MidPoint Method implementation issues

I'm trying to implement a function that calculates the integral of a function f'(x) using the midpoint method.  Here's what I came up with: float CMATH::midPointIntegrate(float steps,float x1, float(*F)(float x) ){ float stepSize = x1/steps; float integral = 0.0f; float xn = 0; float y0 = 0.0f; float ymid = 0.0f; float y1 = 0.0f; for(int I = 0; I < steps; I++){ y0 = F(xn); ymid = F(xn+stepSize*0.5f); y1 = F(xn+stepSize); integral+= y0*stepSize*0.5f + (ymid - y0)*stepSize*0.25f + ymid*stepSize*0.5f + (y1-ymid)*stepSize*0.25f; xn+=stepSize; } return integral; }     It appears to work, however, I was under the impression that the midpoint method requires less steps in order to calculate the integral.  Can anyone confirm that this is the correct implementation of the midpoint method because ,at the moment, i'm not really sure.
9. ## More question on Verlet

Take a look at hardwire's post here:[url="http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1952&start=15&sid=ac8f739422f0fc789578f8c84a6e5cab"] http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1952&start=15&sid=ac8f739422f0fc789578f8c84a6e5cab[/url] In his post he explicitly describes how to implement both static friction and dynamic friction. The dynamic friction method seems to solve the problem of bodies slowly separating away from each other while also adding some nice subtle effects.

At what line exactly does the application crash? *edit* I think you need to post ALL of the method/function/constructor definitions for both classes if you want anyone to solve your problem.
11. ## Numerical integration methods

Apparently this is verlet integration: [source lang="cpp"]void Particle::integrate(float timestep){ Vec2 Temp = P.Position; P.Position += P.Position - P.OldPosition + P.Acceleration*Timestep*Timestep; P.OldPosition = Temp; } [/source] I got it from this [url="http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-verlet-based-approach-for-2d-game-physics-r2714"]article,[/url]
12. ## SDL simple rotation?

[quote name='Canvas' timestamp='1339716779' post='4949357'] could you explain that abit more? if possible? [/quote] What specifically would you like me to explain? The math behind the rotation? Or the Code i posted?
13. ## SDL simple rotation?

I think the general idea is loop through each source pixel and multiply the pixel position by a rotation matrix. Rotation is relative to the origin, so you have to subtract each pixel by the center of the image before you multiply by the rotation matrix. Here some code i wrote to rotate images in visual basic. [source lang="vb"]Public Sub rotate(source As PictureBox, dest As PictureBox, ByVal deg As Single) 'i guess you could think of the PictureBox as SDL_surface Dim cpex As Long Dim cpey As Long Dim cpeColor As Long Dim tx As Double Dim ty As Double Dim tpx As Double Dim tpy As Double Dim Icos As Double Dim Isin As Double Dim sw As Double Dim sh As Double Dim rad As Double Dim cx As Double Dim cy As Double sw = source.Width sh = source.Height rad = deg * (PI / 180) 'convert degrees to radians Icos = Cos(rad) Isin = Sin(rad) cx = sw / 2 'compute center of the image cy = sh / 2 'compute center of the image dest.Cls 'this clears the destination "Surface" 'C.P.E ->current picture element For cpey = 0 To source.Height - 5 For cpex = 0 To source.Width - 5 cpeColor = GetPixel(source.hdc, cpex, cpey) tx = cpex - cx ty = cpey - cy tpx = ((Icos * tx) - (Isin * ty)) tpy = ((Isin * tx) + (Icos * ty)) dest.PSet (tpx + cx, tpy + cy), cpeColor Next cpex Next cpey End Sub[/source]
14. ## SDL - Creating surface with blits only renders one black square

It looks like your blitting parts of the spritesheet to locations that dont exist on the newly allocated surface. Right now im failing to understand the purpose of the dstrect variable. It looks like you should just set that parameter to NULL. Again i'm not sure why you're creating new surfaces of the same image just to render certain areas of that image when you can do the same thing with SDL_BlitSurface. It honestly just seems like a waste of memory. Anyway,who am i to judge? Here's a SDL app that loads the individual tiles from the sprite sheet into separate surfaces. [source lang="cpp"]#include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <stdlib.h> #include <Math.h> #include <vector> SDL_Surface * mainSurface = NULL; SDL_Event g_Event; SDL_Surface * spriteSheet = NULL; std::vector<SDL_Surface*> subSurfaceList; bool running = true; void handleEvents(SDL_Event & event){ if(event.type == SDL_QUIT){ running = false; } } void getSubSurfaces(){ SDL_Rect sheetArea; sheetArea.w = 32; sheetArea.h = 32; for(unsigned int y = 0; y < 4; y++){ for(unsigned int x = 0; x < 7; x++){ sheetArea.x = x*32; sheetArea.y = y*32; SDL_Surface * newSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,32,32,32,0,0,0,0); SDL_BlitSurface(spriteSheet,&sheetArea,newSurface,NULL); subSurfaceList.push_back(newSurface); } } } void clearSurface(SDL_Surface * surface,int r,int g, int b){ SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = surface->w; rect.h = surface->h; Uint32 color = SDL_MapRGB(surface->format,r,g,b); SDL_FillRect(surface,&rect,color); } int main ( int argc, char** argv ){ //initalizing SDL if(SDL_Init(SDL_INIT_VIDEO) == -1){ fprintf(stderr,"Failed to Initalize Video\n"); return 0; }else{ fprintf(stdout,"Suceeded to Initalize Video\n"); atexit(SDL_Quit); } //setup window mainSurface = SDL_SetVideoMode(640,480,32,SDL_RESIZABLE | SDL_HWACCEL | SDL_DOUBLEBUF); //load sprite spriteSheet = IMG_Load("spriteSheet.png"); getSubSurfaces(); //begin main loop int index = 0; int size = subSurfaceList.size(); SDL_Rect sheetArea; sheetArea.x = 0; sheetArea.y = 0; sheetArea.w = 32; sheetArea.h = 32; while(running){ SDL_PollEvent(&g_Event); SDL_Flip(mainSurface); handleEvents(g_Event); clearSurface(mainSurface,0,0,0); index = (index +1) % size; SDL_BlitSurface(subSurfaceList[index],&sheetArea,mainSurface,NULL); SDL_Delay(1000); } return 0; }[/source] Again i'm not sure why you're loading tiles into separate surfaces, I highly recommend that you just use the sprite sheet itself as the source and blit parts of the sprite sheet onto the main surface like this: [source lang="cpp"]void renderExplosion(int x,int y,int intensity,int orientation,SDL_Surface* explositonSheet,SDL_Surface * mainSurface){ SDL_Rect sheetArea; SDL_Rect sheetPosition; sheetPosition.x = x; sheetPosition.y = y; sheetPosition.w = mainSurface->w; sheetPosition.h = mainSurface->h; sheetArea.w = 32; sheetArea.h = 32; sheetArea.x = (orientation%7 )*32; sheetArea.y = (intensity%4)*32; SDL_BlitSurface(explositonSheet,&sheetArea,mainSurface,&sheetPosition); }[/source]