Public Group

# 2D movement

This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all! I am doing a AI project for my Uni, but I come across some problems when doing 2D movement. All I want, is that I have an X, Y showing the current position of the entity and there is a Xh and Yh a position where the entity (should be) heading to. Here is what I try to do: - Normalize vectors (current position, and the "goal" position) - Calculate dot product of vectors, to get angle between the two vectors - Try to resolve the vector Here is the code (in JAVA, but should be the same as C++):
        double mWeightXY = Math.sqrt(Math.pow(m_x, 2) + Math.pow(m_y, 2));

double m_norm_x = m_x / mWeightXY;
double m_norm_y = m_y / mWeightXY;


The given code is executed every n-th frame (currently every frame). The problem is that the entity doesn't go the way it should go, but goes somewhere to the bottom right corner. I've tried many different things, but just cannot see where the error might be. Thanks for your time! Dan Fekete EDIT: Sorry, that I have posted it under OpenGL! I've maybe "misclicked" somewhere.

##### Share on other sites
i'm just going to ignore your code atm, b/c i am lazy, and at a first glance it just seems very wrong. to go from point A to point B, this is how it works:

A (0,0)
B (5,10)

vector of movement is B-A (head minus tail):
Vm (5,10)

now normalize it so you just get the directional component:

Vm ( ~0.4545, ~0.9090 )

to get the amount per unit time, just scale by the velocity and multiply by the time passed that frame:
Vm = Vm * velocity * time passed this frame

if you are doing frame based movement you just scale by the movement rate per frame:
Vm = Vm * movement amt per frame

then just add the resultant vector to the object's position each frame:
obj.position += Vm

-me

##### Share on other sites
LOL!
I've been using 3D math for years, and didn't thought of that. Kids, this is the case when someone overreacts a problem. Many many thanks for the reply!

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 100
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009659
• ### Who's Online (See full list)

There are no registered users currently online

×