Jump to content
  • Advertisement
Sign in to follow this  
lynob

How to get object current position?

This topic is 591 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

I have objects moving on the screen, they're in 2 arrays of objects; obj and obj2
 
    public void Test() {
        obj = new Array < Rectangle > ();
        for (int i = 0; i < numberOfobj; i++) {
            Rectangle ob = new Rectangle();
            ob.x = xpos;
            ob.y = ypos;
            ob.width = width;
            ob.height = height;
            xpos += 28;
            if (score <= -1) {
                obj2.add(ob);
                obj.add(ob); 
            }
        }
    }
When the user clicks on an object, I want to store it in the first array along with its position. and compare its position with the position of the object stored in the second array, not the starting position. I want the position of the object when the user clicked on it.
 
I considered creating a third array or a 2d array to store
 
obj.get(index).x * Gdx.graphics.getDeltaTime()
 
but I don't think this would work, and even if it does, it doesn't seem like the best solution. I feel like Gdx offers some function for that, I just haven't been able to find it.
 
I have see this question, I have tried getX and getY without creating a vector, doesn't work. Do I have to create a vector? Further more, I'm not interested in 

if(sprite.getBoundingRectange().contains(input.x, input.y))
 
i'm more interested in something like

if (obj.get(index).x > obj2.get(index).x)
 
 
 
 
 
 

Share this post


Link to post
Share on other sites
Advertisement

You mentioned the objects are moving across the screen. Show the code where you do this (or does GDX do this for you?). This should be fairly trivial to solve if you are the one responsible for moving the objects. It will also be helpful to see the section of code that runs when the user clicks on an object.

 

Could you give more details of why you are doing this? What is supposed to happen when the user clicks on the object (from a gameplay point of view rather than a logical view).

Share this post


Link to post
Share on other sites

The code for moving is this

 

 
   public void moveObj(){
        Iterator<Rectangle> iter = obj.iterator();
        while (iter.hasNext()) {
            Rectangle obj = iter.next();
            for (int objIndex=0; objIndex<numberOfobj; objIndex++)
                obj.get(objIndex).y += 200 * Gdx.graphics.getDeltaTime();
        }
    }

Think of it like tetris, an object moves, as soon as the user touches the screen, the object stops moving,  gets added to another array for display, and then a new array gets created holding another object and moves on y axis. And as soon as the user touch the screen I want to see if the y axis of the object is the same as the y axis of the other object, if it is the same exact y, then he wins or whatever, if not then he loses, or whatever.

 

Keep in mind that the for loop is generating multiple objects so I have to test the position against the position of the previously generated objects from the second array.

Share this post


Link to post
Share on other sites

There isn't much detail I can give based off what I saw of your code, but this is what I can say.

 

You have an Array of objects in your code, you are trying to get the X and Y value of a Rectangle from that Array. Do you know which Rectangle it would be that you need the X and Y of? If you do, it would be

 

float X = arrayName.get(indexOfRectangle).getX();

float Y = arrayName.get(indexOfRectangle).getY();

 

If you give a more specific definition of what you have and what you're trying to do, I can try to help more.

 

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Array.html

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Rectangle.html

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!