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 |
#ifndef EVILCOLLISION_HPP #define EVILCOLLISION_HPP //********************************************************************* //Headerdateien einbinden #include <SFML\Graphics.hpp> //********************************************************************* //Namespace ef (Evil Framework) namespace ef { //Richtung der Kollision enum E_COLLISION_DIRECTION { COLLISION_UPLEFT = 0x00000000, COLLISION_UPRIGHT = 0x00000001, COLLISION_DOWNLEFT = 0x00000010, COLLISION_DOWNRIGHT = 0x00000011, COLLISION_NONE = 0x11111111 }; //Berechnung der BoundingRect-Kollision bool getCollisionRect(sf::Sprite, sf::Sprite); //Berechnung der Richtung E_COLLISION_DIRECTION getCollisionDirection(sf::Sprite, sf::Sprite); //Berechnung der Pixelgenauen Kollision bool getCollisionPixel(sf::Sprite, sf::Sprite); } #endif |
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 |
#include "EvilCollision.hpp" //----------------------------------------------------------- //Kollisionsberechnungen //----------------------------------------------------------- bool ef::getCollisionRect(sf::Sprite target1, sf::Sprite target2) { return (target1.GetPosition().x < target2.GetPosition().x + target2.GetSize().x && target1.GetPosition().x + target1.GetSize().x > target2.GetPosition().x && target1.GetPosition().y < target2.GetPosition().y + target2.GetSize().y && target1.GetPosition().y + target1.GetSize().y > target2.GetPosition().y); } //*********************************************************** ef::E_COLLISION_DIRECTION ef::getCollisionDirection(sf::Sprite target1, sf::Sprite target2) { //Tritt keine Kollision auf, COLLISION_NONE zurückgeben if(!getCollisionRect(target1, target2)) return COLLISION_NONE; //Die Richtung berechnen int Direction = 0x00000000; if(target1.GetPosition().x < target2.GetPosition().x) Direction |= 0x00000001; if(target1.GetPosition().y < target2.GetPosition().y) Direction |= 0x00000010; return (ef::E_COLLISION_DIRECTION)Direction; } //*********************************************************** bool ef::getCollisionPixel(sf::Sprite target1, sf::Sprite target2) { E_COLLISION_DIRECTION CollisionDir = getCollisionDirection(target1, target2); if(CollisionDir != COLLISION_NONE) { //Positionsangaben für das zu testende Rechteck (Nur das Rechteck, in dem sich die Sprites überschneiden, wird getestet) float spx, spy, epx, epy; //Positionsangaben innerhalb der Sprites float t1x, t1y, t2x, t2y; //Je nach Richtung das zu testende Rechteck berechnen switch(CollisionDir) { case COLLISION_UPLEFT: spx = target1.GetPosition().x; spy = target1.GetPosition().y; epx = target2.GetPosition().x + target2.GetSize().x; epy = target2.GetPosition().y + target2.GetSize().y; break; case COLLISION_DOWNLEFT: spx = target1.GetPosition().x; spy = target2.GetPosition().y; epx = target2.GetPosition().x + target2.GetSize().x; epy = target1.GetPosition().y + target1.GetSize().y; break; case COLLISION_UPRIGHT: spx = target2.GetPosition().x; spy = target1.GetPosition().y; epx = target1.GetPosition().x + target1.GetSize().x; epy = target2.GetPosition().y + target2.GetSize().y; break; case COLLISION_DOWNRIGHT: spx = target2.GetPosition().x; spy = target2.GetPosition().y; epx = target1.GetPosition().x + target1.GetSize().x; epy = target1.GetPosition().y + target1.GetSize().y; break; } //Alle entsprechenden Pixel Testen for(float y = spy; y <= epy; y++) { for(float x = spx; x <= epx; x++) { //Spriteinterne Positionen berechnen t1x = target1.TransformToLocal(sf::Vector2f(x, y)).x; t1y = target1.TransformToLocal(sf::Vector2f(x, y)).y; t2x = target2.TransformToLocal(sf::Vector2f(x, y)).x; t2y = target2.TransformToLocal(sf::Vector2f(x, y)).y; //Kollision anhand des Alphawertes an den entsprechenden Stellen testen //Sind beide Alphawerte größer als 0, tritt eine Kollision auf float a1 = target1.GetPixel(t1x, t1y).a; float a2 = target2.GetPixel(t2x, t2y).a; if(a1 > 0 && a2 > 0) return true; } } } return false; } |
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 |
#ifndef EVILCOLLISION_HPP #define EVILCOLLISION_HPP //********************************************************************* //Headerdateien einbinden #include <SFML\Graphics.hpp> //********************************************************************* //Namespace ef (Evil Framework) namespace ef { //Richtung der Kollision enum E_COLLISION_DIRECTION { COLLISION_UPLEFT = 0x00000000, COLLISION_UPRIGHT = 0x00000001, COLLISION_DOWNLEFT = 0x00000010, COLLISION_DOWNRIGHT = 0x00000011, COLLISION_NONE = 0x11111111 }; //Berechnung der BoundingRect-Kollision bool getCollisionSphere(sf::Sprite, sf::Sprite); //Berechnung der Richtung E_COLLISION_DIRECTION getCollisionDirection(sf::Sprite, sf::Sprite); //Berechnung der Pixelgenauen Kollision bool getCollisionPixel(sf::Sprite, sf::Sprite); } #endif |
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 |
#include "EvilCollision.hpp" //----------------------------------------------------------- //Kollisionsberechnungen //----------------------------------------------------------- bool ef::getCollisionSphere(sf::Sprite target1, sf::Sprite target2) { //Position anpassen und die Mitte des Sprites an die Stelle 0 | 0 verschieben target1.SetPosition(target1.GetPosition().x - target1.GetCenter().x, target1.GetPosition().y - target1.GetCenter().y); target2.SetPosition(target2.GetPosition().x - target2.GetCenter().x, target2.GetPosition().y - target2.GetCenter().y); target1.SetCenter(0, 0); target2.SetCenter(0, 0); //Radius der Boundingsphere des ersten Sprites mit dem Satz des Pytagoras berechnen float a1 = target1.GetSize().x / 2; float b1 = target1.GetSize().y / 2; float radius1 = sqrt((a1*a1 + b1*b1)); //Radius der Boundingsphere des zweiten Sprites mit dem Satz des Pytagoras berechnen float a2 = target2.GetSize().x / 2; float b2 = target2.GetSize().y / 2; float radius2 = sqrt((a2*a2 + b2*b2)); //Distanz zwischen den Mittelpunkten der beiden Sprites berechnen float disX = abs((target2.GetPosition().x + a2) - (target1.GetPosition().x + a1)); float disY = abs((target2.GetPosition().y + b2) - (target1.GetPosition().y + b1)); float distance = sqrt(disX*disX + disY*disY); if(distance < (radius1 + radius2)) return true; return false; } //*********************************************************** ef::E_COLLISION_DIRECTION ef::getCollisionDirection(sf::Sprite target1, sf::Sprite target2) { //Tritt keine Kollision auf, COLLISION_NONE zurückgeben if(!getCollisionSphere(target1, target2)) return COLLISION_NONE; //Die Richtung berechnen int Direction = 0x00000000; if(target1.GetPosition().x < target2.GetPosition().x) Direction |= 0x00000001; if(target1.GetPosition().y < target2.GetPosition().y) Direction |= 0x00000010; return (ef::E_COLLISION_DIRECTION)Direction; } //*********************************************************** bool ef::getCollisionPixel(sf::Sprite target1, sf::Sprite target2) { if(getCollisionSphere(target1, target2)) { //Jedes Pixel in target1 überprüfen for(int x = 0; x < target1.GetSize().x; x++) { for(int y = 0; y < target1.GetSize().y; y++) { //Position des Pixels innerhalb von target2 berechnen int px1 = target1.TransformToGlobal(sf::Vector2<float>(x, y)).x; int py1 = target1.TransformToGlobal(sf::Vector2<float>(x, y)).y; int px2 = target2.TransformToLocal(sf::Vector2<float>(px1, py1)).x; int py2 = target2.TransformToLocal(sf::Vector2<float>(px1, py1)).y; //Befindet sich das Pixel innerhalb von target2? if(px2 > 0 && px2 < target2.GetSize().x && py2 > 0 && py2 < target2.GetSize().y) { //Alpha-Werte prüfen int a1 = target1.GetPixel(x, y).a; int a2 = target2.GetPixel(px2, py2).a; //Sind beide Alphawerte größer als 0, tritt eine kollision auf if(a1 != 0 && a2 != 0) return true; } } } } return false; } |
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »shadowstrike« (23.12.2010, 15:30)
Werbeanzeige