• Advertisement
Sign in to follow this  

[solved] Problems making an object track another object

This topic is 3457 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, I've run across a problem, and cant seem to understand where im going wrong. I want my enemy object to home in on the player object in a 2D world (think geometry wars). At the moment in the enemys update loop i run this code:
[source language="cpp"]
	float angle = atan2(
		(float)(m_target->getYPos() + (m_target->getHeight() / 2) - (m_yPos + (m_height / 2)))
		,(float)(m_target->getXPos() + (m_target->getWidth() / 2) - (m_xPos + (m_width / 2)))
		);

	m_xVelocity = cos(angle) * m_trackVelocity;
	m_yVelocity = sin(angle) * m_trackVelocity;


Which gets the angle between the enemy and its target and modifys its velocitys accordingly. This all works mostly fine and dandy unless the player is to the bottom right of the enemy at which point the enemy will slow down to a crawl / jitter towards the enemy / stop completly. Can anyone spot where ive gone wrong? Or which schoolboy error ive made? Thanks, Scott [Edited by - gameXcore on August 3, 2008 12:39:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
What are the types of m_width, m_height, m_xPos, and m_yPos (and the corresponding variables for m_target)?

Share this post


Link to post
Share on other sites
try this instead


float track_celocity = whatever;
Vector2 my_position = whatever;
Vector2 target_position = whatever;
Vector2 direction = target_position - my_position;
direction.Normalize();
my_position += direction * track_velocity;


this is for C# but easily converted to whatever language you are using

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
What are the types of m_width, m_height, m_xPos, and m_yPos (and the corresponding variables for m_target)?


They are all ints, ill see what happens if i change them to floats, as i think thats what your going to get at^^

Iterestingly ive observed that when the m_trackSpeed variable is higher i dont seem to have this problem. In the example posted m_trackSpeed is between 0.1 and 0.2, above this in the range of 0.2 - 0.5 it works.

As for ncsu121978's suggestion, it seems that to use this approach i would have to use 2D vectors instead of seperate variables and have to write the normalize() function and operator overloads etc, seems a bit too much unless i cant find another solution.


(Edit)

Changing the types to floats has indeed solved the problem, i guess this was just a silly over sight at the beginning why i was using int int he first place, probably due to sdl blitting positions working in full ints.

Thanks for the help =]

Share this post


Link to post
Share on other sites
Quote:
As for ncsu121978's suggestion, it seems that to use this approach i would have to use 2D vectors instead of seperate variables and have to write the normalize() function and operator overloads etc, seems a bit too much unless i cant find another solution.
Using a vector class of some sort will make your code easier to write, read, maintain, and debug. Any time spent up front (either writing the code yourself or finding a suitable existing library) will more than pay for itself in the long run.

Share this post


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

  • Advertisement