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

Author Topic: Using View instead of translating all Box2d coordinates  (Read 2279 times)

0 Members and 1 Guest are viewing this topic.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Using View instead of translating all Box2d coordinates
« on: January 12, 2012, 05:31:12 pm »
Hi

I'm implenting a client-server platformer I decided to use box2d for physics.
So the server calculates the physics then sends the positions to the client which are interpolated to reduce lag, blah blah,.. But my problem is that I decided that having something like 50pixels or 64pixels = 1m i box2d. The problem then is that the client have to convert all the positions..

Soo then I tought of the sf::View. Can I use this in some way to "magically convert" all the positions getting drawn.

Code: [Select]

sf::View view(MagicGoesHere);
App.SetView(view);
while(true)
{
//rendering by just drawing the position recived from the server which
//is box2d body positions
}

Haikarainen

  • Guest
Using View instead of translating all Box2d coordinates
« Reply #1 on: January 13, 2012, 06:38:38 am »
I honestly think something else than views would work better. How is the code looking so far, from the point where you receive data in your client to the point where you position your objects?

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Using View instead of translating all Box2d coordinates
« Reply #2 on: January 13, 2012, 04:28:07 pm »
Quote from: "Haikarainen"
I honestly think something else than views would work better. How is the code looking so far, from the point where you receive data in your client to the point where you position your objects?


Right now the server just sends the position of a b2Body(a box2d body) to the clients. Now rendering yet. But I don't want to always have in mind that the position needs to be converted.

[1] So I either want to use some kinda view that automagically does the converting for me, [2] or I need to implent a "interceptor" for the packets recvied from the server which converts the positions before they are passed on.

[1] Would have a little delay on the drawing, but not much. And would be much "nicer" that option 2.

[2] Not sure if I even wanna go down this road.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Using View instead of translating all Box2d coordinates
« Reply #3 on: January 13, 2012, 07:42:57 pm »
hayer, why not make the server convert the data before sending to the clients? That way the clients would receive the correct data and the problem would be solved.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Using View instead of translating all Box2d coordinates
« Reply #4 on: January 13, 2012, 09:45:40 pm »
Yea, changed to that method now. First I did it so the server would get less load but then I tought about the sending int vs. float to the client.

But I still got a problem.. I don't want to replicate static objects.

So on the server I got this
Code: [Select]

b2BodyDef bodydef;
bodydef.type = b2_dynamicBody;
//bodydef.type = b2_staticBody;
bodydef.position.Set(0,0);
b2Body* body = world.CreateBody(&bodydef);
b2PolygonShape box;
box.SetAsBox(184.0f * METERS_PER_PIXEL, 230.0f * METERS_PER_PIXEL);
b2FixtureDef fixdef;
fixdef.shape = &box;
fixdef.density = 1.0f;
fixdef.friction = 0.3f;
body->CreateFixture(&fixdef);

// this will not be sent to the client
b2BodyDef gdef;
gdef.type = b2_staticBody;
gdef.position.Set(0,(230.f * 2.f) * METERS_PER_PIXEL);
b2Body* ground = world.CreateBody(&gdef);
b2PolygonShape gbox;
gbox.SetAsBox(3.f, 0.1f);
ground->CreateFixture(&gbox, 0.0f);


and this is how I set the position on the client for the static body

Code: [Select]

// static body
sf::RectangleShape rs;
rs.SetSize(sf::Vector2f(3.f*64.f, 1.f * 64.f));
rs.SetOrigin((3.f * 64.f) / 2.f, (1.f * 64.f) / 2.f);
rs.SetPosition(0.f, 230.f * 2.f);
rs.SetFillColor(sf::Color::Blue);

// dynamic body - gets updated behind the scenes
sf::Texture img;
img.LoadFromFile("C:\\MyGameEngine\\Assets\\test.jpg");

sf::Sprite spr(img);
spr.SetOrigin(92, 115);


But when they collide serverside and the client paints them there is still something like 30-40 pixels between them.. Soo there have to be something wrong in my converting of the positions. Anyone care to help me out here?  :)

edit:
forgot to mention that I'm going with 64pixels = 1meter in box2d

Edit: Found out why.. Find my typo and see how retarded I am. Then range my retardness 1 - 10.