0

I am working on creating a 2d platformer and want to implement moving platforms that move backward and forwards a set distance from their original position.

At the moment, the platform moves in one direction as expected, but does not move back when it reaches the specified range.

What needs to be changed or added to make this work as expected?

KinematicBlock.cpp

/*!
\file kinematicBlock.cpp
*/

#include "kinematicBlock.h"

KinematicBlock::KinematicBlock(b2World * world, sf::Vector2f position, sf::Vector2f size, float orientation, sf::Texture *texture)
{
    b2BodyDef l_bodyDef;
    b2PolygonShape l_shape;
    b2FixtureDef l_fixtureDef;

    l_bodyDef.type = b2_kinematicBody;

    l_bodyDef.position.Set(position.x, position.y);
    l_bodyDef.angle = orientation * DEG2RAD;

    m_body = world->CreateBody(&l_bodyDef);
    m_body->SetUserData(this); // used by our contact listener

    l_fixtureDef.density = mk_fDensity;
    l_fixtureDef.friction = mk_fFriction;
    l_fixtureDef.restitution = 0.f;
    l_fixtureDef.shape = &l_shape;

    l_shape.SetAsBox(size.x * 0.5f, size.y * 0.5f);
    b2Fixture* bodyFixture = m_body->CreateFixture(&l_fixtureDef);
    bodyFixture->SetUserData((void *)PhysicalThing::KINEMATICBLOCK);

    setPosition(position);
    setSize(size);
    setOrigin(size * 0.5f);
    setRotation(orientation);
    setOutlineThickness(0.f);
    setTexture(texture);
    setOutlineThickness(0.f);
    setTextureRect(sf::IntRect(0, 0, size.x * 24, size.y * 32));
    setScale(1, 1);

    originPos = position;

    m_body->SetLinearVelocity(b2Vec2(0.2f, 0.f));
}

void KinematicBlock::update()
{
    if (m_body->GetPosition().x == originPos.x + 0.5)
    {
        m_body->SetLinearVelocity(b2Vec2(-0.2f, 0.f));
    }

    if (m_body->GetPosition().x == originPos.x - 0.5)
    {
        m_body->SetLinearVelocity(b2Vec2(0.2f, 0.f));
    }

    setPosition(sf::Vector2f(m_body->GetPosition().x, m_body->GetPosition().y));
}

KinematicBlock.h

#pragma once

/*!
\file kinematicBlock.h
*/

#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>

#include "physicalThing.h"

/*! \class KinematicBlock
\brief A simple block which can move, rotate and collide with stuff ut is not affected by other dynamic bodies.
*/

class KinematicBlock : public sf::RectangleShape, public PhysicalThing
{
public:
    KinematicBlock() {}; //!< Default Contructor
    KinematicBlock(b2World * world, sf::Vector2f position, sf::Vector2f size, float orientation, sf::Texture *texture);
    void update();//!< Update rendering infomation 
    sf::Vector2f originPos;
};

Thanks in advance.

  • Don't compare `float`s for equality, they are prone to rounding erros. See also [is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) You should try with `>`, `<`, `>=` or `<=` – Lukas-T May 13 '20 at 16:33
  • What would be better to use instead of a float? – Jordan Lewis May 13 '20 at 16:36
  • @churill didn't recommend to not to use `float`. He recommended to not to use `float` **with** `operator==()`. Have a look at the [link](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) to see why. – Scheff's Cat May 13 '20 at 16:39
  • Thanks guys, this has resolved my issue. Appreciate it. – Jordan Lewis May 13 '20 at 16:47

0 Answers0