Advertisement Jump to content
Sign in to follow this  

2D movement

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

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 mWeightHead = Math.sqrt(Math.pow(m_head_x, 2) + Math.pow(m_head_y, 2));
        double m_norm_x = m_x / mWeightXY;
        double m_norm_y = m_y / mWeightXY;
        double m_normHead_x = m_head_x / mWeightHead;
        double m_normHead_y = m_head_y / mWeightHead;
        double headCos = m_norm_x*m_normHead_x + m_norm_y*m_normHead_y;

        heading = Math.acos(headCos);
        m_x += Math.round(agentSpeed * Math.cos(headCos));
        m_y += Math.round(agentSpeed * Math.sin(headCos));

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 this post

Link to post
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


Share this post

Link to post
Share on other sites
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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!