How to make an object move in the direction of another object?

I’m almost sure this is a BIG noob question, but pls answer me! I have 2 objects in my world, and i want an object to move toward the location of the another. How do i do that?

Seems like someone asked this exact question just the other day. Anyway, this might be helpful:

Thanks! I managed to make my object stare at the target. But how do I move it using his direction?
…and possibly:

And definitely without a doubt:

Seriously dude, i still can’t figure out. I’m having some bugs. Could you just give me the code?

@Ev1lbl0w said: Seriously dude, i still can't figure out. I'm having some bugs. Could you just give me the code?

I can quote you contract rates if you want to hire me.

Otherwise you may have to wait for someone else to help. This is like one of the easiest things so if you struggle here you will struggle with everything. And since you can’t really seem to be bothered to go through the tutorials or already existing docs, it is only going to get harder.

Or maybe you are already close and just have some simple problems. Since we can’t see your code there is no way of knowing.

Ok, i’ll put the important pieces of code here:

[java] @Override
public void simpleUpdate(float tpf) {
cube.lookAt(target.getLocalTranslation(), Vector3f.UNIT_Y);

The lookAt() method works well. My cube stares the target all the time. I then get the rotation of the cube and mult with the cube location, to make the cube go to the target. Then i mult with the tpf so it doesnt teleport right there, so i can see it chasing.

When i run it, the cube start to go up! notice the cube is going towards the location of the target, but i don’t understand why it goes up! Maybe is because of the Vector3f.UNIT_Y used at lookAt? I don’t know, but pls help me.

I think you need to go through the math tutorials again and then look closely at this line:

The cube.getlocalTranslation() in there makes no sense and is definitely not what you want. Reread the math tutorials until you see the problem.

It also occurs to me that splitting the line out into multiple steps and doing some simple debugging (System.out.printlns, stepping in a debugger, etc.) might have been tremendously helpful for you.

I have been thinking all the day in this, but i still didn’t find the error. But i won’t give up, i’ll still try to get the problem. I already read the Math for Dummies 3 times.

@Ev1lbl0w said: I have been thinking all the day in this, but i still didn't find the error. But i won't give up, i'll still try to get the problem. I already read the Math for Dummies 3 times.

Ask yourself this question: what is the different between a direction and a position?

I’ve been thinking on that this day. (I’m only 14, i don’t know high mathematics. Maybe that can make it harder for me to comprehend). Do you have any page of Vector3f documentation to read? Because i was searching for it and didn’t find a good one.

Page 16/17 of the math for dummies shows you how to get the vector that points from vectorB to vectorA. In your example, vectorB and vectorA are the positions and the resulting vectorC is, when normalized, the direction from vectorB to vectorA.

I’m dumb! I’m dumb! I’m dumb! I’m dumb! I’m dumb! Thanks you so much @normen. When i discovered it i felt so dumb. And thank you @pspeed, you indeed helped me a lot! Thank you so much for you two! I’m dumb! (Is this the way to say that i was so dumb?)

Hi Pspeed, do you know why all these links (math for dummies and scenegraph for dummies) are not available anymore?

Short answer is the stuff had to be moved. Top of the screen, you can see em all up there. ← math for dummies <-scenegraph for dummies ← wiki

1 Like