User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,029 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,883 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 654 | Replies: 6
Reply
Join Date: Mar 2007
Posts: 8
Reputation: h_howee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
h_howee h_howee is offline Offline
Newbie Poster

negative coordinates?

  #1  
May 4th, 2007
I'm working on a breakout game and i want the paddle to follow the mouse
It works from the left side of the client area to the far right of the screen but when i go over the left side of the client area, i expected it to return a negative value and the paddle keeps moving left but instead, i get some coordinate like (32070, 32389) and it goes right

i tried adding m.x < 5000 but for some reason, that doesn't work

Heres the function that moves the paddle and the function i use to get the mouse inputs
void C_Paddle::Paddle_Movement()
{
    POINT m = Input.Get_Mouse();
    RECT ClientRect;
    GetClientRect(g_hwnd, &ClientRect);

    if ((Paddle_Rect.left + Paddle_Size/2) < m.x)
    {
        Paddle_Rect.left++;
        Paddle_Rect.right = Paddle_Rect.left + Paddle_Size;
    }
    if ((Paddle_Rect.left + Paddle_Size/2) > m.x)
    {
        Paddle_Rect.left--;
        Paddle_Rect.right = Paddle_Rect.left + Paddle_Size;
    }
}

void C_Input::Set_Mouse()
{
    GetCursorPos(&Mouse);
    ScreenToClient(g_hwnd, &Mouse);
}

POINT Get_Mouse() { return Mouse; }

Set_Mouse is constantly called in the main loop, would it be better practice to only call it from the wndproc?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Troy
Posts: 1,275
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: negative coordinates?

  #2  
May 4th, 2007
It seems that you're using an unsigned integer to represent your coordinates. When you decrement an unsigned integer, such as 0, instead of getting -1, you get the highest possible value the integer can represent.

In fact, if you're getting 32 thousand, you're probably getting errors related to the comparison of integers -- you're probably representing your paddle coordinates with unsigned integers while your mouse coordinates are signed, or maybe the other way around. I'm guessing your paddle coordinates are rolling over from 0 to 65535 and then getting decremented 32 thousand times ... or something like that.
Last edited by Rashakil Fol : May 4th, 2007 at 5:50 pm.
Reply With Quote  
Join Date: Mar 2007
Posts: 8
Reputation: h_howee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
h_howee h_howee is offline Offline
Newbie Poster

Re: negative coordinates?

  #3  
May 4th, 2007
well, accorind to msdn, it's a LONG

typedef struct tagPOINT
{
    LONG   x;
    LONG   y;
} POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT; 
Reply With Quote  
Join Date: Jun 2005
Location: Troy
Posts: 1,275
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: negative coordinates?

  #4  
May 4th, 2007
Then that's not the case, since RECT and POINT both use LONGs. Maybe you should look closely at the behavior of Get_Mouse() and see what it outputs.
Reply With Quote  
Join Date: Mar 2007
Posts: 8
Reputation: h_howee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
h_howee h_howee is offline Offline
Newbie Poster

Re: negative coordinates?

  #5  
May 4th, 2007
i didn't see the second half of ur post...

the paddle's coordinates are stored in the RECT structure so it's also signed

i made some changes to the code. The if statements are giving me errors now telling me that i'm comparing signed integers to unsigned. could that be part of the problem?

void C_Paddle::Paddle_Movement()
{
    POINT m = Input.Get_Mouse();
    RECT ClientRect;
    GetClientRect(g_hwnd, &ClientRect);

    if ((unsigned int)(Paddle_Rect.left + Paddle_Size/2) < m.x && 
        Paddle_Rect.left > ClientRect.left) //move right
    {
        Paddle_Rect.left++;
        Paddle_Rect.right = Paddle_Rect.left + Paddle_Size;
    }
    if ((unsigned int)(Paddle_Rect.left + Paddle_Size/2) > m.x && 
        Paddle_Rect.right < ClientRect.right) //move left
    {
        Paddle_Rect.left--;
        Paddle_Rect.right = Paddle_Rect.left + Paddle_Size;
    }
}
Reply With Quote  
Join Date: Mar 2007
Posts: 8
Reputation: h_howee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
h_howee h_howee is offline Offline
Newbie Poster

Re: negative coordinates?

  #6  
May 4th, 2007
nvm, took the (unsigned int) out
idk why it gave me errors in the first place


[edit]aaaaah, and im comparing the wrong... variables
Last edited by h_howee : May 4th, 2007 at 6:04 pm.
Reply With Quote  
Join Date: Mar 2007
Posts: 8
Reputation: h_howee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
h_howee h_howee is offline Offline
Newbie Poster

Re: negative coordinates?

  #7  
May 4th, 2007
alright, i found out why this was happening
Paddle_Size is an UINT
i ghanged it to an int and its working properly now

thanks rashakil
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:37 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC