Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
BYTE* CSprite::GetReducedImage(int AnimationNr) { int nReductionFactor = 1; D3DSURFACE_DESC desc; LPDIRECT3DTEXTURE9 lpTexture = m_AnimationSequence[AnimationNr]; lpTexture->GetLevelDesc(0, &desc); // Breite und Hoehe speichern DWORD tempWidth = desc.Width; DWORD tempHeight = desc.Height; BYTE* lpReducedImage = new BYTE[m_Width / nReductionFactor * m_Height / nReductionFactor]; // Textur sperren D3DLOCKED_RECT LockedRect; lpTexture->LockRect(0, &LockedRect, NULL, D3DLOCK_READONLY); DWORD* pPixels = reinterpret_cast<DWORD*>(LockedRect.pBits); LockedRect.Pitch /= 4; int i, j; // reduzierten Puffer füllen for(i=0;i<tempHeight;i+=nReductionFactor) { for(j=0;j<tempWidth;j+=nReductionFactor) { if(pPixels[i*LockedRect.Pitch + j] != 0) { lpReducedImage[i/nReductionFactor * tempWidth/nReductionFactor + j/nReductionFactor] = 1; } else { lpReducedImage[i/nReductionFactor * tempWidth/nReductionFactor + j/nReductionFactor] = 0; } } } // Textur freigeben lpTexture->UnlockRect(0); return lpReducedImage; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
BOOL TestCollision2D::TestCollision(CSprite* sprite1, float rotation1, float x1, float y1,int imgNr1, CSprite* sprite2, float rotation2, float x2, float y2,int imgNr2) { int tempSize = 0; int tempSize2 = 0; //Radius des Sprites messen if (sprite1->GetHeight() > sprite1->GetWidth()) tempSize = (int)(sprite1->GetHeight() /2); else tempSize = (int)(sprite1->GetWidth() /2); //Radius des Sprites messen if (sprite2->GetHeight() > sprite2->GetWidth()) tempSize2 += (int)(sprite2->GetHeight() /2); else tempSize2 += (int)(sprite2->GetWidth() /2); //Grobes testen ob Sprites sich berüren könnten //Distanz berechnen float xDis = x1-x2; float yDis = y1-y2; if (xDis < 0) xDis *= -1; if (yDis < 0) yDis *= -1; float tempDisToSprite2 = sqrt(pow(xDis,2)+pow(yDis,2)); //wenn sich die Sprites berühren könnten genauer messen if (tempDisToSprite2 - (tempSize+tempSize2) <= 0) { int nReductionFactor = 1; BYTE* lpArea = new BYTE[(tempSize*2+tempSize2*2+x1+y1)*(tempSize*2+tempSize2*2+x1+y1)]; DWORD nAreaWidth = (tempSize*2+tempSize2*2+x1+y1)/nReductionFactor; memset(lpArea,0,nAreaWidth*nAreaWidth); DWORD Width = sprite1->GetWidth() / nReductionFactor; DWORD Height = sprite1->GetHeight() /nReductionFactor; DWORD x = nAreaWidth/2-(int)((sprite1->GetWidth() /2)/nReductionFactor);//(DWORD)(x1/nReductionFactor); DWORD y = nAreaWidth/2-(int)((sprite1->GetHeight() /2)/nReductionFactor);//(DWORD)(y1/nReductionFactor); DWORD i, j; BYTE* RedImg = sprite1->GetReducedImage(imgNr1); int index = 0; // reduzierten Puffer füllen for(i=0;i<Height;i+=nReductionFactor) { for(j=0;j<Width;j+=nReductionFactor) { lpArea[(i+y)*nAreaWidth+j+x] = RedImg[i*Width+j]; } } xDis = x2-x1; yDis = y2-y1; Width = sprite2->GetWidth() / nReductionFactor; Height = sprite2->GetHeight() /nReductionFactor; x = (nAreaWidth/2+xDis/nReductionFactor)-(int)((sprite2->GetWidth() /2)/nReductionFactor);//(DWORD)(x1/nReductionFactor); y = (nAreaWidth/2+yDis/nReductionFactor)-(int)((sprite2->GetHeight() /2)/nReductionFactor);//(DWORD)(y1/nReductionFactor); i, j; RedImg = sprite2->GetReducedImage(imgNr2); for(i=0;i<Height;i+=nReductionFactor) { for(j=0;j<Width;j+=nReductionFactor) { if (lpArea[(i+y)*nAreaWidth+j+x] && RedImg[i*Width+j]) return true; } } } return FALSE; }; |
Zitat
pVBuffer->x =cosf(fRotationZ)+sinf(fRotationZ);
pVBuffer->y =cosf(fRotationZ)-sinf(fRotationZ);
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
float fAngleShip; //Drehwinkel des Schiffes (im Uhrzeigersinn) Vector2 vPosShip; //aktuelle Position des Schiffes Vector2 vPosLaser; //Position des Lasers bzw der Rakete ... //Laser zum Schiff relativieren //Position Vector2 vPosLaserRelToShip=vPosLaser-vPosShip; //Rotation vPosLaserRelToShip.x=cosf(fAngleShip)*vPosLaserRelToShip.x -sinf(fAngleShip)*vPosLaserRelToShip.y; vPosLaserRelToShip.y=cosf(fAngleShip)*vPosLaserRelToShip.y +sinf(fAngleShip)*vPosLaserRelToShip.x; ... //Jetzt kannst du deine Kollisionsprüfung vornehmen indem du //für die Raumschiffposition (0,0) verwendest und für den Laser vPosLaserRelToShip |
Werbeanzeige