SFML only draws some sprites - c++

I'm having a strange problem where one sprite is loading however another isn't
Here is my main.cpp
window.draw(universe.getPlayer()->draw()); //Draw Player
std::list<AbstractBlock*>::const_iterator i;
std::list<AbstractBlock*>* values = universe.getLoadedBlocks();
for (i = values->begin(); i != values->end(); ++i){
window.draw((*i)->draw()); //Draw Blocks
}
window.display();
Here you can see the player drawing and all the blocks in the universe drawing. However, only the player draws and the blocks don't draw at all. I have made sure that the loop is actually working. However because draw() returns void I can't actually see if its working or not.
Here is the DirtBlock.cpp (I'm inheriting from AbstractBlock)
DirtBlock::DirtBlock(int x, int y, float rotation, b2World *world){
bodyDef.position.Set(x, y);
bodyDef.linearDamping = .03f;
bodyDef.type = b2_dynamicBody;
fixDef.density = .1f;
b2PolygonShape shape;
shape.SetAsBox(16, 16);
fixDef.shape = &shape;
body = world->CreateBody(&bodyDef);
body->CreateFixture(&fixDef);
texture.loadFromFile("Dirt.bmp");
sprite.setTexture(texture);
sprite.setOrigin(16, 16);
}
sf::Sprite DirtBlock::draw(){
sprite.setPosition(body->GetPosition().x, body->GetPosition().y);
return sprite;
}
Not everything is included, only the stuff that is involved with the drawing.
My Player class is very similar:
Player::Player(b2World *world){
texture.loadFromFile("player.bmp");
bodyDef.position.Set(10, 10);
bodyDef.type = b2_dynamicBody;
fixDef.density = .1f;
b2PolygonShape shape;
shape.SetAsBox(16, 16);
fixDef.shape = &shape;
body = world->CreateBody(&bodyDef);
body->CreateFixture(&fixDef);
body->SetLinearDamping(.03f);
sprite.setTexture(texture);
sprite.setOrigin(16, 16);
force = 10.f;
}
sf::Sprite Player::draw(){
sprite.setPosition(body->GetPosition().x, body->GetPosition().y);
sprite.setRotation(body->GetAngle() * (180 / b2_pi));
return sprite;
}
Since they are so similar why is one drawing and the other not? I have a feeling it might be because of my inheritance. I'm typically a Java programmer and I'm not 100% sure I did the inheritance correctly in C++. Should it be like this? (My DirtBlock.h)
class DirtBlock: public AbstractBlock
{
public:
DirtBlock();
DirtBlock(int x, int y, float rotation, b2World *world);
~DirtBlock();
virtual sf::Sprite draw();
virtual void destroy(b2World *world);
private:
sf::Sprite sprite;
};

I actually fixed it myself. It turned out to be a stupid mistake on my part but I might as well write and answer so if anyone else makes this mistake they may find this and it may fix their problem.
I didn't make the function draw in the AbstractBlock class virtual. Because of this, when I was calling draw for the DirtBlock, it was looking for the draw method in AbstractBlock that didn't have a virtual flag.

Related

The collision in SFML is not that good, how to improve it?

I've been lately working on a simple game using C++ and SFML latest version, but I had a problem which is that the collision detection is not that good, for example the player dies even if the enemy didn't touch him yet, but just near him. Here is the code of the player class with the move function and collision detection code AND the moves of the enemy class:
`class PlayerA : public CircleShape
{
public:
//Constructor:
PlayerA(float xposition, float yposition, float radius, float s)
{
setRadius(radius);
setFillColor(Color::Yellow);
setOutlineColor(Color(00,80,00));
setOutlineThickness(-2);
setPointCount(3);
setSpeed(s);
setPosition(xposition,yposition);
}
//Movements of the player:
void up()
{
move(0,-10*speed);
}
void down()
{
move(0,10*speed);
}
void right()
{
move(10*speed,0);
}
void left()
{
move(-10*speed,0);
}
void checkA(ObsA *obs1=NULL,ObsA *obs2=NULL, ObsA *obs3=NULL, ObsA *obs4=NULL, ObsA *obs5=NULL)
{
if(obs2==NULL)
{
if(getGlobalBounds().intersects(obs1->getGlobalBounds()))
{
relevel();
}
}
private:
float speed=0.00;
void obs()
{
if(speed > 0)
{
rotate(0.5*speed);
}
else
{
rotate(0.5*speed);
}
}
private:
float speed = 0.00;
void obs()
{
if(speed > 0)
{
rotate(0.5*speed);
}
else
{
rotate(0.5*speed);
}
}
private:
float speed = 0.00;
Is there something wrong with the code, how to fix the problem, thank you!
The intersects function just check if two rectangles intersect. If you want pixel perfect collision detection in SFML you have to write that yourself.
Basically, start with intersects, if it is true, then get the intersecting rectangle and check if any pixels therein from both original rectangles contains overlaping relevant pixels.
You can use this function to perform better collision detection.Its a basic one but works well
bool circleTest(const sf::Sprite &first, const sf::Sprite &second)
{
sf::Vector2f firstRect(first.getTextureRect().width, first.getTextureRect().height);
firstRect.x *= first.getScale().x;
firstRect.y *= first.getScale().y;
sf::Vector2f secondRect(second.getTextureRect().width, second.getTextureRect().height);
secondRect.x *= second.getScale().x;
secondRect.y *= second.getScale().y;
float r1 = (firstRect.x + firstRect.y) / 4;
float r2 = (secondRect.x + secondRect.y) / 4;
float xd = first.getPosition().x - second.getPosition().x;
float yd = first.getPosition().y - second.getPosition().y;
return std::sqrt(xd * xd + yd * yd) <= r1 + r2;
}
Are you using a circle? If I remember correctly, the circle will have a rectangle hitbox. If that is the case, then you may have collision between the invisible rectangle corners.
If you're using a circle, Perhaps change class to a square rectangle and see if collision works correctly. Or try testing collision directly on an x or y axis with your circles; i.e. having them moving in a straight line towards each other only changing 1 axis. (the edge of the circle will be the same as the edge of the rectangle at the left, right, top, and bottom sections).
If you're needing a better collision for circles, there may be one already built in SFML. But I don't think it would be too much to write your own logic using the radius of your two circles, the center of your two objects, and the angle hypotenuse between the centers.
edit based on Merlyn Morgan-Graham's comment.

White Square in SFML, Sprite and texture stored in different objects

I'm experiencing the white square problem in SFML. I'm working on a game that uses a tiled map. A Game class will load the tile set as a sf::Texture from a file and then a getTileById function in Game will "Cut" out the appropriate tile from the tileset texture. Eg.
sf::Sprite GameScreen::getSpriteByPos(int pos, sf::Texture texture, int tileSize) {
sf::IntRect subRect;
subRect.left = (pos-1)*tileSize;
subRect.top = (pos-1)*tileSize;
subRect.width = tileSize;
subRect.height = tileSize;
sf::Sprite tileSprite(texture, subRect);
return tileSprite;
}
The sprite will then be passed into a Tile object, which will then set its sprite_ attribute to it. Eg.
void Tile::setSprite(sf::Sprite sprite) {
sprite_ = sprite;
}
The Game object will load all tiles in this way and store them all in a vector. To draw them, it will loop through the vector and call the Tile::draw(sf::RenderWindow&) method on each one of them (Passing in the RenderWindow to be drawn to). This method takes an sf::RenderWindow and the tile simply calls the sf::RenderWindow::draw(sf::Sprite) on the RenderWindow with its sprite_ attribute. Eg.
void Tile::draw(sf::RenderWindow &window){
window.draw(sprite_);
return;
}
I'd like to do drawing this way because the Tile::draw method is inherited from a Drawable class, so that all objects that are drawable can inherit from Drawable and implement their drawing method in a way that suits them, something that will be necessary in my project. The sprite is being drawn as a white square though, which strikes me as strange as the tileSet_ attribute has not been destroyed, it is an attribute of the Game class and still exists.
Can anybody tell me what is going on here? Any help would be appreciated.
You are passing your texture "by value". That means you get a copy of your texture inside this function:
sf::Sprite GameScreen::getSpriteByPos(int pos, sf::Texture texture, int tileSize) {
sf::IntRect subRect;
subRect.left = (pos-1)*tileSize;
subRect.top = (pos-1)*tileSize;
subRect.width = tileSize;
subRect.height = tileSize;
sf::Sprite tileSprite(texture, subRect);
return tileSprite;
}
But that copy is destroyed when the function ends.
You don't need that copy, so don't make a copy:
sf::Sprite GameScreen::getSpriteByPos(int pos, const sf::Texture& texture, int tileSize) {
sf::IntRect subRect;
subRect.left = (pos-1)*tileSize;
subRect.top = (pos-1)*tileSize;
subRect.width = tileSize;
subRect.height = tileSize;
sf::Sprite tileSprite(texture, subRect);
return tileSprite;
}

How to add sprite in box2d body (c++, cocos2d)?

setTouchEnabled(true);
ball=CCSprite::create("soccer_ball.png");
ball->setPosition(ccp(100,100));
addChild(ball,1);
//CREATE WORLD
b2Vec2 gravity(0, -9.8); //normal earth gravity, 9.8 m/s/s straight down!
bool doSleep = true;
myWorld = new b2World(gravity);
myWorld->SetAllowSleeping(doSleep);
//BODY DEFINITION
myBodyDef.type = b2_dynamicBody; //this will be a dynamic body
myBodyDef.position.Set(0, 20); //set the starting position
myBodyDef.angle = 0; //set the starting angle return true;
myBodyDef.userData=ball;
//CREATE SHAPE
b2CircleShape ballShape;
ballShape.m_p.Set(2.0f,3.0f);
ballShape.m_radius=50.0/PTM_RATIO;
//CREATE BODY
dynamicBody = myWorld->CreateBody(&myBodyDef);
//FIXTURE DEFINITION
b2FixtureDef ballFixtureDef;
ballFixtureDef.shape = &ballShape;
ballFixtureDef.density = 1;
dynamicBody->CreateFixture(&ballFixtureDef);
}
void HelloWorld::update()
{
float32 timeStep = 1/20.0; //the length of time passed to simulate (seconds)
int32 velocityIterations = 8; //how strongly to correct velocity
int32 positionIterations = 3; //how strongly to correct position
myWorld->Step( timeStep, velocityIterations, positionIterations);
}
I am learning box2d basic concepts and i have put my code here. I created a box2d circle shape. now i want to add sprite of ball in that circle shape. I have used myBodyDef.userData=ball; but it is not working .. i used gles-render code to debug draw but in that circle body is different and ball sprite is different. when i applyforce or impluse body works perfectly but dont attached to ball sprite..is any mistake in my code. I want to apply force to ball and ball should bounce according to physics but i can not attach to body plz help me.
Body and sprite looks like this
Here's my version working for V-3.6:
void GamePlayScreen::update(float dt) {
phyWorld->Step(dt, 8, 1);
// Iterate over the bodies in the physics phyWorld
for (b2Body* b = phyWorld->GetBodyList(); b; b = b->GetNext()) {
if (b->GetUserData() != NULL) {
// Synchronize the AtlasSprites position and rotation with the corresponding body
Sprite* sprActor = (Sprite*) b->GetUserData();
sprActor->setPosition(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
sprActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
}

My 'body' class in SFML: How does it rotate member shapes correctly on its own?

Hey so i've made a 'body' class, which inherits from sf::drawable in SFML that holds shapes together so i can form more complicated shapes and figures. I found i had to do a few updates on each individual member shape at render time, based on what's happened to the body over-all since last render.
This included:
Rotation
Translations
Scaling
I thought i would have to manually write the code to factor in these changes that could have occured to the overall body, so the shape positions turned out right. However when coding the Rotation part, i found after writing the manual code that even though i made Bodies Render() function only iterate through and render each shape without changing them, they somehow came out with the right orientation anyway. HOW CAN THIS BE?
I commented out the code i wrote and apparently didn't need, and i use the Draw() function to render. Please explain to me my own code! (God i feel retarded).
here's the class:
class Body : public sf::Drawable{
public:
Body(const sf::Vector2f& Position = sf::Vector2f(0, 0), const sf::Vector2f& Scale = sf::Vector2f(1, 1), float Rotation = 0.f, const sf::Color& Col = sf::Color(255, 255, 255, 255)){
SetPosition(Position);
SetScale(Scale);
SetRotation(Rotation);
SetColor(Col);
RotVal=0;};
////////////////// Drawable Functions ////////////////////
void SetX(float X){
MoveVal.x += X - GetPosition().x;
Drawable::SetX(X);};
void SetY(float Y){
MoveVal.y += Y - GetPosition().y;
Drawable::SetY(Y);};
void SetRotation(float Rotation){
RotVal+= Rotation-GetRotation();
Drawable::SetRotation(Rotation);};
////////////////////////////////////////////////////////////
bool AddShape(sf::Shape& S){
Shapes.push_back(S); return true;};
bool AddSprite(sf::Sprite& S){
Sprites.push_back(S); return true;};
void Draw(sf::RenderTarget& target){
for(unsigned short I=0; I<Shapes.size(); I++){
//Body offset
Shapes[I].SetPosition(
Shapes[I].GetPosition().x + MoveVal.x,
Shapes[I].GetPosition().y + MoveVal.y);
// Aparrently Body shapes rotate on their own...
//WWTFFFF>>>>??????????
//Body Rotation
//float px= GetPosition().x,
// py= GetPosition().y,
// x= Shapes[I].GetPosition().x,
// y= Shapes[I].GetPosition().y,
// rot= ConvToRad(RotVal);
/*Shapes[I].SetPosition(
px + ((x-px)*cos(rot)) - ((y-py)*sin(rot)),
py - ((x-px)*sin(rot)) + ((y-py)*cos(rot)));*/ //TODO: put this in a math header
//Shapes[I].Rotate(RotVal);
}
target.Draw(*this); // draws each individual shape
//Reset all the Change Values
RotVal=0;
MoveVal.x=0;
MoveVal.y=0;
};
private:
sf::Vector2f MoveVal;
float RotVal;
std::vector<sf::Shape> Shapes;
std::vector<sf::Sprite> Sprites;
virtual void Render(sf::RenderTarget& target) const{
for(unsigned short I=0; I<Shapes.size(); I++){
target.Draw(Shapes[I]);}
for(unsigned short I=0; I<Sprites.size(); I++){
target.Draw(Sprites[I]);}
};
};
My guess was confirmed by the SFML source code.
When you call target.Draw(*this) it eventually calls Drawable::Draw(RenderTarget& Target) const, which sets up a render matrix which has the rotation that you gave it with the Drawable::SetRotation call. Your virtual Render function is then called, with that rotation environment set up. This means that your body-parts get rotated.
Have a look at the source yourself to get a better understanding. (;

Shape manipulation with openFrameworks

I'm a openFrameworks newbie. I am learning basic 2d drawing which is all great so far. I have drawn a circle using:
ofSetColor(0x333333);
ofFill;
ofCircle(100,650,50);
My question is how do I give the circle a variable name so that I can manipulate in the mousepressed method? I tried adding a name before the ofCircle
theball.ofSetColor(0x333333);
theball.ofFill;
theball.ofCircle(100,650,50);
but get I 'theball' was not declared in this scope error.
As razong pointed out that's not how OF works. OF (to the best of my knowledge) provides a handy wrapper to a lot of OpenGL stuff. So you should use OF calls to effect the current drawing context (as opposed to thinking of a canvas with sprite objects or whatever). I usually integrate that kind of thing into my objects. So lets say you have a class like this...
class TheBall {
protected:
ofColor col;
ofPoint pos;
public:
// Pass a color and position when we create ball
TheBall(ofColor ballColor, ofPoint ballPosition) {
col = ballColor;
pos = ballPosition;
}
// Destructor
~TheBall();
// Make our ball move across the screen a little when we call update
void update() {
pos.x++;
pos.y++;
}
// Draw stuff
void draw(float alpha) {
ofEnableAlphaBlending(); // We activate the OpenGL blending with the OF call
ofFill(); //
ofSetColor(col, alpha); // Set color to the balls color field
ofCircle(pos.x, pos.y, 5); // Draw command
ofDisableAlphaBlending(); // Disable the blending again
}
};
Ok cool, I hope that makes sense. Now with this structure you can do something like the following
testApp::setup() {
ofColor color;
ofPoint pos;
color.set(255, 0, 255); // A bright gross purple
pos.x, pos.y = 50;
aBall = new TheBall(color, pos);
}
testApp::update() {
aBall->update()
}
testApp::draw() {
float alpha = sin(ofGetElapsedTime())*255; // This will be a fun flashing effect
aBall->draw(alpha)
}
Happy programming.
Happy designing.
You can't do it that way. ofCircle is a global drawing method and draws just a circle.
You can declare a variable (or better three int for rgb - since you can't use ofColor as an argument for ofSetColor) that store the color for the circle and modify it in the mousepressed method.
Inside the draw method use your variables for ofSetColor before rendering the circle.