Posted: 30th Nov 2011 15:46
hello, I am developing an agk app that has a turret in the middle of the map and you are firing at enemies. The thing I am having trouble with is when I rotate the turret how can I make the bullet shoot out in the direction of the turret? I guess I could do a ton of if statement that checks for the angle of the turret, but anyone else have a better way? thank you!
Posted: 30th Nov 2011 15:57
I guess I could do a ton of if statement that checks for the angle of the turret, but anyone else have a better way? thank you!


Look up the angle of the turrent and then align the new object in the same direction, and then thrust it forward.

Check out the following commands:
GetSpriteAngle
SetSpriteAngle
Posted: 30th Nov 2011 16:23
Hi
I am working on the same type of thing. I have the x and y and the angle what is the line of code that would be to thrust it forward on a stright line.

Dennis
Posted: 30th Nov 2011 16:55
i use these functions in my boinkadroid game for my turrets.
The point to command can you simply adjust with -90 or something if it points wrongly.
+ Code Snippet
function Point_TO(id,x#,y#,x2#,y2#)
    sprite_new_angle# = atanfull(x2#-x#,y2#-y#)
    SetSpriteAngle(id,sprite_new_angle#)
endfunction


function Move_To( id , originalX# , originalY# , destinationX# , destinationY# , move# )

    ontarget=0
    distanceX# = destinationX# - originalX#
    distanceY# = destinationY# - originalY#
    distanceFromAtoB# = sqrt ( ( distanceX# * distanceX# ) + ( distanceY# * distanceY# ) )

    if ( distanceFromAtoB# <> 0.0 )
       directionX# = distanceX# / distanceFromAtoB#
       directionY# = distanceY# / distanceFromAtoB#
    endif
    if ( move# > distanceFromAtoB# ) or (distanceFromAtoB#<move#)
        ontarget=1
        //ExitFunction ontarget
    endif
    if ontarget=0
       // work out new location
       newX# = originalX# + directionX# * move#
       newY# = originalY# + directionY# * move#

       // update position of our sprite
       SetSpritePosition ( id , newX#, newY# )
    endif

endfunction ontarget
Posted: 30th Nov 2011 17:42
This command would apply a force.

SetSpritePhysicsForce

To calculate the correct x and y force, you need to use sin and cos on the angle of your sprite.

Xforce = speed * Cos(angle)
Yforce = speed * Sin(angle)
Posted: 30th Nov 2011 18:27
Well you need to know several things about the turret because it rotates.

First you need to fire from the center point.

This is actually taken out of my game.
+ Code Snippet
void Resetendata(int xxw,int xxp,bool torf)
{

    float xdistfromcenter;
    float ydistfromcenter;
    float len;
    float lendirx;
    float lendiry;
	
	playanden[xxw].g_Bullets[g_bulletplace2].rateoffire=3.0;

	if(!playanden[xxw].g_Bullets[g_bulletplace2].bActive&&playanden[xxw].life>=3.0)
    {
	
		//float rotangle2=agk::ATanFull(agk::GetSpriteX(playanden[xxw].id)-agk::GetSpriteX(player.playerid),agk::GetSpriteY(playanden[xxw].id)-agk::GetSpriteY(player.playerid));
		if(torf)
		{
		playanden[xxw].life=0;
		}
		if(playanden[xxw].xxc<19)
		{
			xdistfromcenter=24;
			ydistfromcenter=18;
		}
	
		else
		{
			xdistfromcenter=49;
			ydistfromcenter=48;
		}
		
		
        len=sqrt((xdistfromcenter)*(xdistfromcenter)+(ydistfromcenter)*(ydistfromcenter));
		
        lendirx=agk::Sin(rotangle2+180+xxp)*len;
        lendiry=agk::Cos(rotangle2+180+xxp)*len; 
		
	
		playanden[xxw].g_Bullets[g_bulletplace2].accx=-agk::Sin(rotangle2)*playanden[xxw].g_Bullets[g_bulletplace2].rateoffire;
		playanden[xxw].g_Bullets[g_bulletplace2].accy=agk::Cos(rotangle2)*playanden[xxw].g_Bullets[g_bulletplace2].rateoffire;

		if(playanden[xxw].xxc==15)
		{
			playanden[xxw].g_Bullets[g_bulletplace2].xcord=lendirx+agk::GetSpriteX(playanden[xxw].id)+xdistfromcenter;
            
       playanden[xxw].g_Bullets[g_bulletplace2].ycord=-lendiry+agk::GetSpriteY(playanden[xxw].id)+ydistfromcenter+45;
		}
		else
		{
		playanden[xxw].g_Bullets[g_bulletplace2].xcord=lendirx+agk::GetSpriteX(playanden[xxw].id)+xdistfromcenter;
            
       playanden[xxw].g_Bullets[g_bulletplace2].ycord=-lendiry+agk::GetSpriteY(playanden[xxw].id)+ydistfromcenter;
		}
        playanden[xxw].g_Bullets[g_bulletplace2].angle=rotangle2;
		playanden[xxw].g_Bullets[g_bulletplace2].fLife=0.0f;
        playanden[xxw].g_Bullets[g_bulletplace2].bActive=true;
		g_bulletplace2++;
	
            //	agk::Print(g_bulletplace);
         if(g_bulletplace2==79)
         {
            g_bulletplace2=0;
         }
	}
	
}
Posted: 30th Nov 2011 20:47
@ Jerico2day
Thanks starting to make sense. Going to need a pot of coffee

Thanks to all

Dennis
Posted: 30th Nov 2011 21:23
Just to add to J's help, if you are using an aspect rather than a set resolution size you'll need to multiply the Y value by an aspect ratio before using any of the sine rules IE

+ Code Snippet
aspect# = w#/h#
Yforce = speed * aspect# * Sin(angle)