Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mouse Aiming

This thread is locked; no one can reply to it. rss feed Print
Mouse Aiming
Kevlar Games
Member #7,757
September 2006

Hey,

I'm trying to get a sprite to always face the mouse. How to I do this? Is there a formula I need to calculate the angle based on the mouse position? If so, what is it?

Thanks,
KG

23yrold3yrold
Member #1,134
March 2001
avatar

Depends a lot on your game. If this is some top down angle where the camera is perfectly perpendicular to the playfield, easy as pie (your alliteration skill increases by 1). If not, it gets stickier ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Albin Engström
Member #8,110
December 2006
avatar

Here you go:
_______________________________________
include math.h

#include <math.h>

_______________________________________
define pi.

#ifndef PI
#define PI 3.1415927 //++
#endif

_______________________________________
you need a bitmap...

BITMAP *test;

_______________________________________
you need to store a value..

double player_angle;

_______________________________________
you need some logic.

player_angle = (atan2(mouse_y - pY, mouse_x - pX)*128/PI) + V;

mouse_y is the vertical screen position of the mouse pointer.
you can figure mouse_x out yourself.

pY is the second point of which is needed for the calculation of the degree, and which i suggest is you bitmaps rotation point is.

V is the additional degrees added to the calculated direction, you want to adjust this until it looks good :). NOTE: instead of a 360 degree, use 256.

_______________________________________
and you need to draw the sprite. :)

pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle));

pX and pY is the posision of the sprite.
and cX and xY is from which point the sprite should be rotated.

enjoy!

this should work, i think..

James Stanley
Member #7,275
May 2006
avatar

Albin said:

#ifndef PI
#define PI 3.1415927 //++
#endif

What is the //++ for?

Albin Engström
Member #8,110
December 2006
avatar

James Stanley said:

What is the //++ for?

It was my extremely bad way of saying: "you can use more decimals if you want". >< :)

Dustin Dettmer
Member #3,935
October 2003
avatar

Why not just use M_PI?

Quote:

and you need to draw the sprite. :)

pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle));

Slight correction

pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle * (256 / 360.0f));

But personally I would leave the number in radians, which would result in this (slightly simpler) code:

player_angle = atan2(mouse_y - pY, mouse_x - pX) + V;

pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle * (128 / M_PI));

Albin Engström
Member #8,110
December 2006
avatar

What is M_PI? -.-

Dustin Dettmer
Member #3,935
October 2003
avatar

Its a constant that represents Pi. Part of some standard and included with your compiler via cmath or math.h

Ceagon Xylas
Member #5,495
February 2005
avatar

Oops. Should have read. :-X

Kevlar Games
Member #7,757
September 2006

Thanks, everybody. I wasn't expecting this many replies! I'll try those methods and tell you how they worked.

EDIT: Well, they were a bit confusing at first, but I got them to work! Now, is there any way to rotate a sprite without distortion?

Go to: