Jump to content
  • Advertisement
Sign in to follow this  

help rotating turret using mouse

This topic is 4251 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

Im trying to get my turret to roate as according to where the mouse is.In other words i want my turrer to face my mouse pointer, where ever it is on the screen, whilst still in the same poistion. and i also have a problem getting my bulle to fire at the mouse pointer void init() { allegro_init(); install_timer(); install_mouse(); install_keyboard(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT,ResX,ResY,0,0); screen_buffer=create_bitmap(ResX,ResY); select_mouse_cursor(MOUSE_CURSOR_ALLEGRO); } void spawn_bullet(double x,double y) { for(int i=0;i<Max_objs;i++) if(obj.status == 0) { obj.status = 1; obj.AItype=2; obj.size = 8; obj.angle=10; obj.ptrSprite=3; obj.positionX = x+16; obj.positionY= y+16; //obj.desX; //obj.desY; i=Max_objs; } } void spawn_turret() { for(int i=0;i<Max_objs;i++) if(obj.status == 0) { obj.status=1; obj.AItype=3; obj.ptrSprite=4; obj.size = 64; obj.angle=0; obj.positionX=50; obj.positionY=59; obj.sides=2;// 1=foe; 2=friend i=Max_objs; } } void AICalcXY(int i) { double dx,dy; dx=sin(obj.angle); dy=cos(obj.angle); dx*=obj.speed; dy*=obj.speed; obj.positionX+=dx; obj.positionY+=dy; } void AICalcAngle(int i) { double dx=obj.desX-obj.positionX;//+obj.size/2 -obj.positionX; double dy=obj.desY-obj.positionY; if(dx==0)dx=1; if(dy==0)dy=1; double newAngle=atan2(dy,dx); newAngle=(newAngle/M_PI)*180; while(newAngle<0) { newAngle+=360; } obj.angle=newAngle; } void AIbullet(int i) { AICalcAngle(i); AICalcXY(i); } void AIturret(int i) { AICalcAngle(i); } void ai() { for(int i=0;i<Max_objs;i++) { if(obj.AItype==1) //AIexplosion(i); if(obj.AItype==3) AIturret(i); if(obj.AItype==4) AIbullet(i); } } void drawsprite(int arrPos) { double topLeftX=obj[arrPos].positionX; double topLeftY=obj[arrPos].positionY; double BtmRX=topLeftX+obj[arrPos].size; double BtmRY=topLeftY+obj[arrPos].size; int sX,sY;//source X int cX,cY; if(topLeftX<0) { sX=-topLeftX; topLeftX=0; } else { sX=0; } if(topLeftY<0) { sY=-topLeftY; topLeftY=0; } else { sY=0; } if(BtmRX>ResX) { BtmRX=ResX; } if(BtmRY>ResY) { BtmRY=ResY; } cX=BtmRX-topLeftX; cY=BtmRY-topLeftY; int pX = (int)(obj[arrPos].angle/10)*obj[arrPos].size; if(cX>0 && cY>0) { for(int y=0;y<cY;++y) for(int x=0;x<cX;++x) { int col=getpixel(sprite[obj[arrPos].ptrSprite],x+sX+pX,y+sY); putpixel(screen_buffer,topLeftX+x,topLeftY+y,col); } } } void rotate() { BITMAP* temp1, *temp2; temp1=load_bitmap("shooter.bmp",NULL); sprite[4]=create_bitmap(64*36,64); temp2=load_bitmap("bullet.bmp",NULL); sprite[3]=create_bitmap(8*36,8); for (int rotNr=0; rotNr<36;rotNr++) { rotate_sprite(sprite[3],temp2,rotNr*8,0,itofix(7.1*rotNr)); rotate_sprite(sprite[4],temp1,rotNr*64,0,itofix(7.1*rotNr)); } } void window() { for(int i=0;i<Max_objs;i++) drawsprite(i); } void mouse() { if (mouse_b & 1) { for(int i=0;i<Max_objs;i++) { if(obj.sides==2) spawn_bullet(obj.positionX,obj.positionY); i=Max_objs; } for(int x=0;x<Max_objs;x++) { if(obj[x].AItype==2) obj[x].desX=mouse_x; obj[x].desY=mouse_y; x=Max_objs; } } } void passive_to_active() { show_mouse(screen_buffer); blit(screen_buffer,screen,0,0,0,0,ResX,ResY); clear_to_color(screen_buffer,makecol(0,0,0)); show_mouse(NULL); } void fse() { ai(); mouse(); window(); passive_to_active(); } int main() { init(); rotate(); spawn_turret(); while(!key[KEY_ESC]) { fse(); } } END_OF_MAIN()

Share this post

Link to post
Share on other sites
Put your code into [source] tags so that it's rather easier to read (source colouring, the formatting's kept and only so much on screen at one time).

Here's some C++-style pseudocode for you, though:

MousePosition = GetMousePosition(); // Get screen-space co-ords of mouse
ScreenToWorldCoordinates(MousePosition); // Convert to world space
int xToMouse = MousePosition.x - TurretPosition.x;
int yToMouse = MousePosition.y - TurretPosition.y;
// Normalize direction
int length = sqrt((x * x) + (y * y));
xToMouse /= length;
yToMouse /= length;

FireDirection(xToMouse, yToMouse);

EDIT: Or if you want an angle (in radians): FireDirection(atan2(yToMouse, xToMouse));.

EDIT2: Fixed odd expression (*= 1 / length rather than simply /= length). Don't know quite what I was thinking.

[Edited by - TheUnbeliever on April 23, 2007 11:23:13 AM]

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!