Hello Guest

Move object in it's center...

  • 8 Replies
  • 3652 Views
*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Move object in it's center...
« on: May 12, 2014, 08:40:46 »
So i'm trying to move a GameComponent on it's center (an interactible object in my game engine) around the screen with the mouse.

I already done a peace of code, but i'm not able to pick up the object in it's center, i can get it's x position, y position and width/height.

Here is my code that handles object movement (the render is not nessessary because the object is rendering entirely correctly) :
Code: [Select]
   public void updateComponent() {
        int x = Mouse.getX();
        int y = SLDTGame.getScreenHeight() - Mouse.getY();
        int xSlot = slotX + slotWidth;
        int ySlot = slotY + slotHeight;

        if (slotTaken){
            slotX = x;
            slotY = y;
        }

        if (xSlot >= SLDTGame.getScreenWidth()){
            slotX = SLDTGame.getScreenWidth() - (slotWidth + 5);
            Mouse.setCursorPosition(SLDTGame.getScreenWidth() - (slotWidth + 7), SLDTGame.getScreenHeight() - Mouse.getY());
            slotTaken = false;
            return;
        }
        if (ySlot >= SLDTGame.getScreenHeight()){
            slotY = SLDTGame.getScreenHeight() - (slotHeight + 5);
            Mouse.setCursorPosition(Mouse.getX(), SLDTGame.getScreenHeight() - (slotHeight + 7));
            slotTaken = false;
            return;
        }

        if (x <= xSlot && x >= slotX){
            if (y <= ySlot && y >= slotY){
                if (Mouse.isButtonDown(0)) {
                    slotTaken = true;
                }
                System.out.println("You are in the slot !");
            }
        }

        if (!Mouse.isButtonDown(0)) {
            slotTaken = false;
        }
    }



Yuri6037

*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Re: Move object in it's center...
« Reply #1 on: May 13, 2014, 07:58:40 »
Nobody ?
Nobody has ever tried to move object on the screen with the mouse ?

*

Offline abcdef

  • ****
  • 335
Re: Move object in it's center...
« Reply #2 on: May 13, 2014, 10:55:35 »
Yuri6037

It might be better next time to ask a specific question, you haven't actually asked a question yet. Also remember this is a forum to discuss LWJGL related issues so people will be mainly here for those types of questions.

I have no idea what your problem is but it looks like you want to do a ray-mesh intersection to find out what mesh your mouse has selected then after a mouse click and drag you need to work out where to move your object. It is your choice how you map the mouse actions to movement actions.

*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Re: Move object in it's center...
« Reply #3 on: May 13, 2014, 12:19:47 »
The question is very simple... It's already marked in my post ! I just need to find a way to move an object on it's center with the LWJGL mouse input.

*

Offline abcdef

  • ****
  • 335
Re: Move object in it's center...
« Reply #4 on: May 13, 2014, 13:04:56 »
I still can't see a question and I don't really understand what it is you want to do even with what you said in your last post. I will leave it for others to investigate further. I have already had a guess at what you wanted to do but I think its wrong.

*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Re: Move object in it's center...
« Reply #5 on: May 13, 2014, 19:14:38 »
You're not understanding me ?
So i will try to explain better : you know Minecraft ? You know item stack management in Minecraft GUIs ? That's it ! I just want to know how they do to take an object from a slot without the item to be moved in x = 0 and y = 0. I want my object to be taken like a Minecraft ItemStack in a GuiContainer's Slot.

Are you now understanding all i want ?


So that was the first point. The second poit is : Is there any way to prevent user from moving the taken object after the OpenGL window ? I want my object to be not moved to disapear from OpenGL window.




Do you now understanding what i need exactly ?
If that's still not clear, please explain what is exactly not clear.
Yuri6037

*

bogieman9876

Re: Move object in it's center...
« Reply #6 on: May 14, 2014, 19:37:17 »
Perhaps something like this?

Code: [Select]
int xCenter = objectWidth / 2;
int yCenter = objectHeight / 2;
x = Mouse.getX() + xCenter;
int y = SLDTGame.getScreenHeight() - Mouse.getY() + yCenter;

The center of a square/rectangle is simply width/2 and height/2.
Then adding the half-width and half-height to the current x and y position will give you the center. (Usually...)

Re: Move object in it's center...
« Reply #7 on: June 09, 2014, 03:28:57 »
Try using a bounding box type thing. Look http://math.stackexchange.com/questions/190111/how-to-check-if-a-point-is-inside-a-rectangle here for an example.

Re: Move object in it's center...
« Reply #8 on: June 25, 2014, 23:14:10 »
Code: [Select]
int xCenter = objectWidth / 2;
int yCenter = objectHeight / 2;
x = Mouse.getX() - xCenter;
int y = SLDTGame.getScreenHeight() - Mouse.getY() - yCenter;

You should actually subtract the center position if you want the mouse to be in the center of the object  :)