Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [HELP]SFML Sprite with Box2D  (Read 7798 times)

0 Members and 1 Guest are viewing this topic.

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
[HELP]SFML Sprite with Box2D
« on: August 28, 2011, 01:56:10 pm »
Can anyone show me how I can tie my sf::sprite with b2body

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
[HELP]SFML Sprite with Box2D
« Reply #1 on: August 28, 2011, 04:32:02 pm »
There some source code samples on http://box2dsfmlfun.blogspot.com/. (which you would have found if you had used the search function of the forum :wink: )

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
[HELP]SFML Sprite with Box2D
« Reply #2 on: August 28, 2011, 07:14:24 pm »
I already saw that tutorial, That tutorial mainly deals with shapes... I want to use sprites in Box2D.. can anyone help??

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[HELP]SFML Sprite with Box2D
« Reply #3 on: August 28, 2011, 10:43:33 pm »
You need to define the outline shape of your sf::Sprite so that it can work with b2Body. In general, you have to add the polygon points of your sprite manually.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
[HELP]SFML Sprite with Box2D
« Reply #4 on: August 28, 2011, 11:24:40 pm »
I usually use OpenGL directly, but this should work:

Code: [Select]
sf::Image img;
img.LoadFromFile("bla.png");
sf::Sprite s(img);

// Set origin to center
s.SetOrigin(img.GetWidth() / 2.0f, img.GetHeight());
b2BodyDef d;

// Adjust body def settings...

b2Body* b;
b = pWorld->CreateBody(&d);

// Apply a fixture...

b2Vec2 pos = b->GetWorldCenter();

s.SetPosition(pos.x * physicsDrawScale, pos.y * physicsDrawScale);
s.SetRotation(b->GetAngle() * (180.0f / PI));
appWindow.Draw(s);


Just make sure that your sprite's center corresponds to the center of the shape. If you do not have a symmetrical shape, you may have to get the center of mass instead of the plain center.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Haikarainen

  • Guest
[HELP]SFML Sprite with Box2D
« Reply #5 on: August 29, 2011, 12:08:03 am »
Quote from: "Nexus"
You need to define the outline shape of your sf::Sprite so that it can work with b2Body. In general, you have to add the polygon points of your sprite manually.


Dont wanna get too offtopic, but i've been thinking about an algorithm to generate box2d paths by tracing the pixels from an sf::Image.

Basically trace each side of the image, outwards to inwards and stop where transparency meets opaque pixels, so that you get the contures of the image and create a path out of that. And also give the programmer options to interlace the precision to save performance on big images. This could be achieved by checking the conturedifference on each pixel, and skip insignificant ones and always skip zeroes. Using a bool to switch when concave goes convex and vice versa could also be useful, especially in combination of the interlacing method.

Will probably get on this as soon as the project im on now is complete and im starting on Modern Arcade.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
[HELP]SFML Sprite with Box2D
« Reply #6 on: August 29, 2011, 12:13:15 am »
Isn't it more complicated now ? With sf::Texture ? (just asking 'cause I'm not sure I've get sf::Texture... :roll: )

Haikarainen

  • Guest
[HELP]SFML Sprite with Box2D
« Reply #7 on: August 29, 2011, 12:17:57 am »
Quote from: "Lo-X"
Isn't it more complicated now ? With sf::Texture ? (just asking 'cause I'm not sure I've get sf::Texture... :roll: )


Havent looked much in to it but from what i know now, sf::Image can still get pixels, and you can get an sf::Image from an sf::Texture. So its just a matter of creating a temporary sf::Image when creating the paths ;)

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
[HELP]SFML Sprite with Box2D
« Reply #8 on: August 29, 2011, 01:58:09 am »
Quote from: "Haikarainen"
Quote from: "Nexus"
You need to define the outline shape of your sf::Sprite so that it can work with b2Body. In general, you have to add the polygon points of your sprite manually.


Dont wanna get too offtopic, but i've been thinking about an algorithm to generate box2d paths by tracing the pixels from an sf::Image.

Basically trace each side of the image, outwards to inwards and stop where transparency meets opaque pixels, so that you get the contures of the image and create a path out of that. And also give the programmer options to interlace the precision to save performance on big images. This could be achieved by checking the conturedifference on each pixel, and skip insignificant ones and always skip zeroes. Using a bool to switch when concave goes convex and vice versa could also be useful, especially in combination of the interlacing method.

Will probably get on this as soon as the project im on now is complete and im starting on Modern Arcade.


I think it would be much more productive if this outputted a list of the points to a text file that the user could then use in their project instead of having to scan each sprite everytime it needs to define a box2d object

Haikarainen

  • Guest
[HELP]SFML Sprite with Box2D
« Reply #9 on: August 29, 2011, 04:20:56 am »
Quote from: "Richy19"

I think it would be much more productive if this outputted a list of the points to a text file that the user could then use in their project instead of having to scan each sprite everytime it needs to define a box2d object


Well yes.. nothing says the data cant be stored :P Even though, for the majority of 2dgames out there, it would be enough to just load all data and generate this on startup.

bensmiley45

  • Newbie
  • *
  • Posts: 1
    • AOL Instant Messenger - 87+Wyatt+park+Ro
    • View Profile
Tracing images to create Box2D objects
« Reply #10 on: November 17, 2011, 04:48:28 pm »
I wrote a Java program to create paths by tracing around sprites. The program outputs a series of coordinates for the path. I've also written an algorithm to simplify the path by selectively removing points. The path can then be directly used as an edge shape in Box2D or as a dynamic shape if you perform an triangulation. I've written a blog posts explaining the process in detail with full source code. The first tutorial covers how to trace around an image to create a path http://www.deluge.co/?q=box2d-tracing-shape-outlines the second explains how to simplify the path by selectively removing points http://www.deluge.co/?q=box2d-tracing-shape-outlines-part-2.