Original Post
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
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.
Quote: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.
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?
vector2 direction = target - current_position;direction.normalize();// Then, for each update:current_position += direction * speed * time_step;
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;
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
Quote:
Original post by jyk Quote: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.
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?
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;
Quote:No, it should be a simple 2-d vector type, either from an existing library or of your own devising. A simple example:
is vector2 equals to SDL_Rect?
struct vector2{ float x; float y;};Quote:No, that will also need to come from an existing library or your own code.
is there any way to do .normalize() in sdl?
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more