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 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
bool veBoundingBox::CheckCollision(const veBox& Box1, const veBox& Box2) { // Prüfen, ob eine Kollision statt fand if((Box1.PosX + Box1.SizeX) <= Box2.PosX) return false; if((Box1.PosY + Box1.SizeY) <= Box2.PosY) return false; if((Box2.PosX + Box2.SizeX) <= Box1.PosX) return false; if((Box2.PosY + Box2.SizeY) <= Box1.PosY) return false; return true; } veSprite::veSprite(string TextureFilename) { // ------------------------------------------------------------------------- // Konstruktor der Sprite Klasse // ------------------------------------------------------------------------- m_pTexture = NULL; m_pBuffer = NULL; m_Width = 0; m_Height = 0; m_Frames = 0; m_Position = veVector3(0.0f, 0.0f, 0.0f); m_XAnimation = 0.0f; m_X2Animation = 0.0f; m_IsWithAnimation = false; m_IsAnimationPlaying = false; m_AnimationInterval = 0; m_IntervalCounter = 0; HRESULT hResult; D3DXIMAGE_INFO ImgInfo; D3DXGetImageInfoFromFile(TextureFilename.c_str(), &ImgInfo); // Das Sprite ist ohne Animation, also auf false setzen m_IsWithAnimation = false; if(FAILED(hResult = D3DXCreateTextureFromFileEx(veSceneManager::SceneManager().m_pD3DDevice, TextureFilename.c_str(), ImgInfo.Width, ImgInfo.Height, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &m_pTexture))) { MessageBox(NULL, "Fehler beim Erzeugen der Spieler Sprite Textur", "Fehler aufgetreten", MB_OK | MB_ICONEXCLAMATION); } m_Width = ImgInfo.Width; m_Height = ImgInfo.Height; ........ // Die Bewegungsmatrix durchführen m_mTranslation = veMatrixTranslation(m_Position); // Die Bewegungsmatrix setzen veSceneManager::SceneManager()->SetTransform(D3DTS_WORLD, (D3DXMATRIX*)&m_mTranslation); // Die Box mit Werten füllen m_Box.PosX = static_cast<int>(m_Position.x); m_Box.PosY = static_cast<int>(m_Position.y); m_Box.SizeX = static_cast<int>(m_Position.x + m_Width); m_Box.SizeY = static_cast<int>(m_Position.y + m_Height); ........ } veResult veSprite::SetPosition(veVector3 Position) { // ------------------------------------------------------------------------- // Das Sprite bewegen // ------------------------------------------------------------------------- // Die Position übergeben m_Position = Position; // Die Box mit den aktuellen Werten füllen m_Box.PosX = static_cast<int>(m_Position.x); m_Box.PosY = static_cast<int>(m_Position.y); m_Box.SizeX = static_cast<int>(m_Position.x + m_Width); m_Box.SizeY = static_cast<int>(m_Position.y + m_Height); return VE_OK; } veBox& veSprite::GetBoundingBox() { // Die Box mit den aktuellen Werten füllen m_Box.PosX = static_cast<int>(m_Position.x); m_Box.PosY = static_cast<int>(m_Position.y); m_Box.SizeX = static_cast<int>(m_Position.x + m_Width); m_Box.SizeY = static_cast<int>(m_Position.y + m_Height); return m_Box; } |
Diese Funktion greift dann einfach, über einen Zeiger auf die Klasse veBoundingBox, auf die Funktion CheckCollision zu.
C-/C++-Quelltext |
|
1 |
m_Box.SizeX = static_cast<int>(m_Position.x + m_Width); |
Aha, also du meinst die Funktion ohne Klasse schreiben. Kann ich auch machen.
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 |
class BoundingBox { public: bool CheckCollision(const BoundingBox& box); void SetPosition(const float newx, const float newy); void SetSize(const float newwidth, const float newheight); // usw... private: float x; float y; float width; float height; // usw... } bool BoundingBox::CheckCollision(const BoundingBox& box) { // Hier dein Quellcode (die if-Bedingung etc.) von oben } // In der Anwendung: BoundingBox box1(); box1.SetPosition(123.f, 456.f); box1.SetWidth(100.f, 100.f); BoundingBox box2(); box2.SetPosition(132.f, 456.f); box2.SetSize(100.f, 100.f); if(box1.CheckCollision(box2)) { // Kollision! } if(box2.CheckCollision(box1)) // Entspricht der oberen if-Bedingung, es ist also egal, von welchem Objekt die Funktion aufgerufen wird. { // Kollision! } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »SaRu« (03.03.2011, 14:09)
Werbeanzeige