Jump to content
  • Advertisement
Sign in to follow this  
mind in a box

Rounding issue

This topic is 2905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again!

I have a problem with rounding numbers. Firstly here is a screenshot of my game:



As you see, the blocks have a fixed space between each other (50 pixels I think).
The problems lays at my editor. When I have a block attached to my mouse and I am near the top left corner of the window (0,0), everything works fine. But when I get away from this, the attached block isn't in the middle of the courser anymore but more left/top from it. The gap between them grows when I come farther away from the top/left corner.

This only happens when I have my Rounding code put in. Without it it works fine:


Pos.x=((int)((Pos.x/BlockGridSize.x))+0.5)*BlockGridSize.x;
Pos.y=((int)((Pos.y/BlockGridSize.y))+0.5)*BlockGridSize.y;



Where Pos is the Position of the block and BlockGridSize the space betweeb them (50!)

Am I doing this right? ( Guess not, because it fails :P)

Share this post


Link to post
Share on other sites
Advertisement
It looks like you've grouped your parentheses incorrectly.

Pos.x=((int)((Pos.x/BlockGridSize.x)+0.5))*BlockGridSize.x;

Also, if Pos.x and BlockGridSize.x are integers, you may need to cast to a floating point type before doing the division.

Share this post


Link to post
Share on other sites
if both Pos.x and BlockGridSize.x are integers, than your doing it wrong.
Pos.x/BlockGridSize.x will give you an integer instead of a float.

Share this post


Link to post
Share on other sites
The issue (I think):
You use the same Pos variable for the mouse and the display too?

You should decouple the two.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
The issue (I think):
You use the same Pos variable for the mouse and the display too?

You should decouple the two.


Sorry, that's stupid.
Post more code: input handling etc.

Share this post


Link to post
Share on other sites
Thats all the code:


POINT p;
POINT p;
GetCursorPos(&p);
ScreenToClient(D2DVars.RenderTarget->GetHwnd(),&p);

D3DXVECTOR3 Pos = D3DXVECTOR3(p.x,p.y,0);
CurrentCamera->TransformVector(&Pos);

Pos.x-=BlockGridSize.x;
Pos.y-=BlockGridSize.y;

Pos.x=((int)((Pos.x/BlockGridSize.x)+0.5))*BlockGridSize.x;
Pos.y=((int)((Pos.y/BlockGridSize.y)+0.5))*BlockGridSize.y;
//Pos.z=(int)(Pos.z+0.5);

//Don't place two blocks on top of each other
vector<BasicBlock *>::iterator it;
for(it=Scene->Blocks.begin();it!=Scene->Blocks.end();it++)
{
if((*it)!=CurrentBlock && memcmp((*it)->GetPosition(),&Pos,sizeof(D3DXVECTOR3))==0)
{
return false;
}
}

if(CurrentBlock)
{
CurrentBlock->SetPosition(&D3DXVECTOR3(Pos.x,Pos.y,0));
}






What do you mean?

[edit]: I forgot this one:


void PlayerCamera::TransformVector(D3DXVECTOR3* InOutPosition)
{
(*InOutPosition).x-=Position.x;
(*InOutPosition).y-=Position.y;
}




Share this post


Link to post
Share on other sites
I uploaded a build of my game here: http://up.servut.us/24304
[Edit: Uses Direct2D, so it only works on Vista + 7, sorry for that)

No source code, I know the rules and I don't want you to debug it. It's just to make you familiar with my problem.

At first, click once, anywhere you want to attach a new block to the mouse. Move it around to see what I mean.

Additional controls (Not needed for solving my problem, but might be fun):

Mouse1: Place block
Mouse2: Place physics block
Mousewheel: Change textures

W: Start simulation
Q: Stop Simulation
F7: Reset simulation (A bit buggy with physical blocks)

F5: Save world
F6: Load world

(Started simulation)
Left/Right arrow-keys: Move player
Space: Jump

(When you get stuck, press F7)

I know about every physics bug you find! I anyone knows how to fix them, just let me know, I use the BulletPhysics library btw. But that is just a side note.

The Block positioning is a bigger problem for now.


I hope someone can help me now [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!