Skip to main content
GameDev.net gamedev.net
🔒 Locked

moving a sprite towards a target (SDL)

Started by shuwo Feb 3, 2010 at 10:57 AM 8 replies 3.1k views
Original Post
shuwo
shuwo
hello! i want my tower to shoot bullets towards a moving sprite. how can i do this? i think it has something to do with sin or cos? please point me in the right direction! :D ty
HolyGrail
HolyGrail
Hi,
there is a simple way to do that : given the origin point and the target point , you can calculate the line equation between origin and target.
Otherwise yes if you want to consider a shooting angle and a particular range you can use trigonometric transformations.
Check in Wikipedia for formulas.
shuwo
shuwo
Quote:
Original post by HolyGrail
Hi,
there is a simple way to do that : given the origin point and the target point , you can calculate the line equation between origin and target.
Otherwise yes if you want to consider a shooting angle and a particular range you can use trigonometric transformations.
Check in Wikipedia for formulas.


thanks for your answer! if i understand right i can use this? y = kx + m
but how do i implement that equation?

i know you can get 'k' by (y2-y1)/(x2-x1) and such, but how do i use it to move my sprite?

HolyGrail
HolyGrail
Hello Shuwo,
once you've got the k ( slope ) and the m of your equation , simply make a loop where you can store the coordinates of your sprite for example in a container like std::vector or std::list

You can also use a polar-cartesian conversion if you want to use angles and range for the bullet:

http://en.wikipedia.org/wiki/Polar_coordinate_system

Once you have a stack of sprite coordinates you can move your sprite with a Sprite::Update() method during every timer event
Zakwayda
Zakwayda
Quote:
thanks for your answer! if i understand right i can use this? y = kx + m
but how do i implement that equation?

i know you can get 'k' by (y2-y1)/(x2-x1) and such, but how do i use it to move my sprite?
No, don't use the slope-intercept form. It can't be used to represent vertical lines and may cause problems with lines that are nearly vertical, and generally isn't very useful in the context of game development.

The most straightforward way to do what you're asking is as follows (and actually doesn't involve any trig):
vector2 direction = target - current_position;direction.normalize();// Then, for each update:current_position += direction * speed * time_step;
HolyGrail
HolyGrail
Quote:
The most straightforward way to do what you're asking is as follows (and actually doesn't involve any trig):
vector2 direction = target - current_position;direction.normalize();// Then, for each update:current_position += direction * speed * time_step;

ok but with your solution we don't know when to start and when to finish to update the position
Gammenon
Gammenon
Quote:
Original post by HolyGrail
Quote:
The most straightforward way to do what you're asking is as follows (and actually doesn't involve any trig):
vector2 direction = target - current_position;direction.normalize();// Then, for each update:current_position += direction * speed * time_step;

ok but with your solution we don't know when to start and when to finish to update the position


Each shot will have current_position, which will start where the tower is (current_position = tower_shot_spawn_position) and in each iteration will get closer to target, thanks to current_position += direction * speed * time_step
DariusBoone
DariusBoone
jyk's solution is the correct one. You want to be using vectors in your game. You can create a vector by subtracting the sprites position from the towers position. Once the vector is made unit length by normalizing it, you can scale it by multiplying the vector with a scalar value to set the speed. Each frame multiply by the delta time value to modify the position of the bullet.
shuwo
shuwo
Quote:
Original post by jyk
Quote:
thanks for your answer! if i understand right i can use this? y = kx + m
but how do i implement that equation?

i know you can get 'k' by (y2-y1)/(x2-x1) and such, but how do i use it to move my sprite?
No, don't use the slope-intercept form. It can't be used to represent vertical lines and may cause problems with lines that are nearly vertical, and generally isn't very useful in the context of game development.

The most straightforward way to do what you're asking is as follows (and actually doesn't involve any trig):
vector2 direction = target - current_position;direction.normalize();// Then, for each update:current_position += direction * speed * time_step;


is vector2 equals to SDL_Rect? is there any way to do .normalize() in sdl?
Zakwayda
Zakwayda
Quote:
is vector2 equals to SDL_Rect?
No, it should be a simple 2-d vector type, either from an existing library or of your own devising. A simple example:
struct vector2{    float x;    float y;};
Quote:
is there any way to do .normalize() in sdl?
No, that will also need to come from an existing library or your own code.

For more info, look around online for tutorials on vector math.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.