Move object in it's center...

Started by Yuri6037, May 12, 2014, 08:40:46

Previous topic - Next topic

Yuri6037

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) :
   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

Yuri6037

Nobody ?
Nobody has ever tried to move object on the screen with the mouse ?

abcdef

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.

Yuri6037

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.

abcdef

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.

Yuri6037

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

Perhaps something like this?

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...)


Longarmx

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  :)