SDL mouse

Started by
1 comment, last by incertia 11 years, 5 months ago
Hi all im kinda new with SDL and im making game for my self as a practise run really,
Iwe been having couple of problems i want it to be top down shooter...
Now i have no idea how to make my character follow and turn where the mouse is and bullet animition fire in direction of the mouse.
If i could get any tips from you guys i would be really glad i just dont know where to start with this one.
Tnx guys
Advertisement
Bullet animation would use line of sight, meaning as long as there is a straight line between origin and dest and no objects between them the bullet will move in a straight line. For character following the mouse you will need pathfinding, this way the character can navigate from pointA to pointB avoiding all obstacles.

http://www.policyalmanac.org/games/aStarTutorial.htm:this will explain pathfinding

I recomend python/pygame over c/c++/SDL for begginers.
Well, given that you know the location of two points (i.e. the character and the cursor), you can figure out the angle that you need to rotate the character to make him look at the cursor.

Suppose your cursor is currently at [eqn]\left(x_0, y_0\right)[/eqn] and your character sprite is located at the point [eqn]\left(x_1, y_1\right)[/eqn].

Then the vector from the character pointing towards the cursor is [eqn]\left(x_0 - x_1, y_0 - y_1\right) = \left(\Delta x, \Delta y\right)[/eqn]. This is the direction your character needs to walk in and it has length [eqn]L = \sqrt{\left(\Delta x, \Delta y\right) \cdot \left(\Delta x, \Delta y\right)} = \sqrt{\Delta x^2 + \Delta y^2}[/eqn]. If you want to scale it down to a unit vector so his walk speed is only impacted by direction and not distance, just divide by [eqn]L[/eqn] so your ideal direction vector is just
[eqn]\vec{d} = \dfrac{1}{L} \left(\Delta x, \Delta y\right)[/eqn]
what

This topic is closed to new replies.

Advertisement