Du bist nicht angemeldet.

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

1

17.06.2012, 18:38

[Closed] Box fällt falsch [Box2D / SFML]

//Closed

Fehler gefunden !

Sry, habe wohl x und y bei der Plattform vertauscht. Arg und nach dem dummen Fehler such ich dann die ganze Zeit -.-


//Closed





Hey,

ich wollte heute etwas mit Box2D rumprobieren um die Engine besser kennenzulernen.

Naja aber jetzt habe ich schon bei dem ersten kleinen Programm Probleme und ich kann den Fehler einfach nicht finde. Gut, ich bin jetzt noch recht neu in Box2D, aber ich habe mir mittels der Anleitung mein Programm mehrmal durchgeschaut und finde den Fehler leider trotzdem nicht. Auch Google spuckt zu dem Thema nicht gerade viel aus.

Hier mal eine kleine Beschreibung zu meinem Problem:

Also ich hab eine Plattform und eine Box (im Code wird die Box als Ball bezeichnet), die Plattform ist ein statisches Objekt und die Box ein Dynamisches. Ich positioniere die Box über der Plattform, damit die Box dann auf diese fallen kann.
Das erste Problem ist jetzt schon, dass die Box ohne lineare Geschwindigkeit gar nicht fällt, obwohl ich eine Schwerkraft mit (0.0f , 9.8f) gesetzt habe.
Das zweite Problem ist, dass wenn ich eine lineare Geschwindigkeit gesetzt habe, die Box immer nur bis zu y = 175 Pixel fällt und dann einfach stoppt. Das Komische ist, dass wenn ich die Box auf eine Position mit, zum Beispiel, y = 200 setze, dann "fällt" die Box plötzlich nach oben zu den y = 175 Pixel und stoppt dort wieder.

Ich kann mir beim besten Willen nicht vorstellen, wieso die Box immer zu y=175 will.

Ich hoffe, dass einer von euch eine Idee oder sogar eine Lösung hat, denn ich komme einfach nicht mehr weiter.




Hier mal der Code:

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <SFML\Graphics.hpp>
#include <SFML\window.hpp>
#include <SFML\System.hpp>
#include <Box2D\Box2D.h>
#include <iostream>

// 1 meter = 50 pixel

int main()
{

    //Create SFML Window
    sf::RenderWindow Game(sf::VideoMode(800,600,32),"Jump 'n' run - 0.1");


    
    //Include BackgroundImage
    sf::Image backgroundImg;
    backgroundImg.LoadFromFile("Background.png");
    sf::Sprite backgroundSprite;
    backgroundSprite.SetImage(backgroundImg);
    backgroundSprite.SetPosition(0,0);

//Create Box2D stuff
    //World
    b2Vec2 gravity(0.0f,9.8f); 
    b2World world(gravity);
    
    //Static Body
    b2BodyDef platformDef;
    b2Body* platformBody;
    platformDef.type = b2_staticBody;
    platformDef.position.Set(1.0f,12.0f);
    platformBody = world.CreateBody(&platformDef);

    //Creating a Shape for the static body
    b2PolygonShape platformBox;
    b2FixtureDef platformBoxDef;
    platformBoxDef.shape = &platformBox;
    platformBoxDef.density = 2.0f;
    platformBoxDef.restitution = 0.5f;
    platformBox.SetAsBox(4.0f,8.0f);

    platformBody->CreateFixture(&platformBox,0);




//Include sprite for the platform 
    sf::Image platformImg;
    platformImg.LoadFromFile("Platform.png");
    sf::Sprite platformSprite;
    platformSprite.SetImage(platformImg);
    platformSprite.SetPosition(50,400);


//Create a dynamic body

    b2BodyDef ballDef;
    ballDef.type = b2_dynamicBody;
    ballDef.position.Set(1.0f,1.0f);
    b2Body* ballbody = world.CreateBody(&ballDef);

    b2PolygonShape ballShape;
    ballShape.SetAsBox(0.5f,0.5f);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &ballShape;
    fixtureDef.density = 5.0f;
    fixtureDef.friction = 0.3f;
    ballbody->SetLinearVelocity(gravity);
    ballbody->CreateFixture(&fixtureDef);
//Create SFML Shape for the dynamic body

    sf::Shape ballShapeSFML = sf::Shape::Rectangle(50,50,50,50,sf::Color(0,0,0));


    float timeStep = 1.0f / 60.0f;
    float velIter = 8.0;
    float posIter = 3.0;


    while (Game.IsOpened())
    {
        //
        int StepInt;
        std::cin >> StepInt;
        //


        sf::Event event;
        while (Game.GetEvent(event))
        {
            if(event.Type == sf::Event::Closed)
                Game.Close();
        }
        world.Step(timeStep,velIter,posIter);
    

        //get ball position
        b2Vec2 pos = ballbody->GetPosition();
        pos.x *=50.0f;
        pos.y *=50.0f;

        ballShapeSFML.SetPointPosition(0,pos.x, pos.y);
        ballShapeSFML.SetPointPosition(1,pos.x+50,pos.y);
        ballShapeSFML.SetPointPosition(2,pos.x+50,pos.y+50);
        ballShapeSFML.SetPointPosition(3,pos.x,pos.y+50);

        ballShapeSFML.SetRotation(ballbody->GetAngle());


        Game.Clear(sf::Color(255,255,255));

        printf("Ball:\t\t%f --- %f\n",ballbody->GetPosition().x*50.0f, ballbody->GetPosition().y*50.0f);
        printf("Platform:\t%f --- %f\n",platformBody->GetPosition().x*50.0f, platformBody->GetPosition().y*50.0f);

        Game.Draw(backgroundSprite);
        Game.Draw(platformSprite);
        Game.Draw(ballShapeSFML);




        Game.Display();
    }


    world.~b2World();
    return EXIT_SUCCESS;
}


Hier nochmal ein Bild wie das Ganze aussieht. In der Konsole wird erst der X-Wert und dann der Y-Wert der beiden Objekte ausgegeben.

(Link)
Why so serious ?

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Noxum« (17.06.2012, 19:16)