Trying to shoot from the hand of my character. Need a formula

14 posts

Flag Post

Hi guys, like the topic said im trying to shoot from the hand of my character. This is what i got so far:

When i click left mouse a bullet is created and fired. This is the code for it

private function createBullet()
{
	for (var i = 0; i < playerWeapon.bulletNumber; i++)
	{
		var tempBullet:MovieClip = new Bullet();
		addChild(tempBullet);
		tempBullet.x = xChar.character.x + xChar.rightHand.x * Math.cos(xChar.character.rotation * radians);
		tempBullet.y = xChar.character.y + xChar.rightHand.y * Math.sin(xChar.character.rotation * radians);
		var randomNum:Number = randomize((playerWeapon.bulletOffset/2),playerWeapon.bulletOffset);
		var bulletAngle:Number = (xChar.character.rotation + randomNum) * radians;
		tempBullet.xSpeed = Math.cos(bulletAngle) * playerWeapon.bulletSpeed;
		tempBullet.ySpeed = Math.sin(bulletAngle) * playerWeapon.bulletSpeed;
		tempBullet.lifeTimer = setInterval(destroyBullet,playerWeapon.bulletMaxAge,tempBullet);
		bulletArray.push(tempBullet);
	}
	startReloading();
}

This is run from an enterframe that check if a bullet is fired and make its way trough the scene

private function updateBullets()
{
	for (var i = 0; i < bulletArray.length; i++)
	{
		var temporaryBullet:MovieClip = bulletArray[i];
		temporaryBullet.x +=  temporaryBullet.xSpeed;
		temporaryBullet.y +=  temporaryBullet.ySpeed;
	}
}

The code speak by itself i think… My bullet is currently shooting from the middle of my character and im kinda lost on math…

 
Flag Post

At first, is your arm a stationary object character-wise? Where is its attachment point? It looks like the arm can be rotated – around what object does it rotate? What is your character consist of?

 
Flag Post

yes its stationary to my character

public class wl_Character extends MovieClip
{
	public var character:Character = new Character();
	public var rightHand:MovieClip = new weap_Gun();
	public var leftHand:MovieClip = new weap_Gun();
	public function wl_Character(px,py,gun:String)
	{
		if (gun == "Double Pistol")
		{
			leftHand.x = 17;
			leftHand.y = -23;
			character.addChild(leftHand);
			rightHand.x = 17;
			rightHand.y = 23;
			character.addChild(rightHand);
		}
		else if (gun == "Single Pistol")
		{
			rightHand.x = 17;
			rightHand.y = 23;
			character.addChild(rightHand);
		}
		character.x = px;
		character.y = py;
		addChild(character);
		trace("Character added at " + character.x + " " + character.y);
	}
}

my gun is set in hands with those coord

 
Flag Post

and if you ask yes im trying only with single pistol hand right now :P

 
Flag Post

hmmm try double checking registration points, and see how is code related to them

 
Flag Post

This might be useful if you are a bit lost with the coordinates stuff.

 
Flag Post

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#localToGlobal%28%29

 
Flag Post

hmm guess its something to do with my rotation… when im looking right i shoot correctly when moved left i shoot with the other hand… which is the same coordinate but with – before the number. So i guess ill have to add a if rotation = less than 180 then do this etc… Oh well ill try to work something out from there

 
Flag Post

dunnoh if this can help… this is the mouvement of my character with my mouse

private function mouseMovements(event:MouseEvent):void
{
	var cy:Number = mouseY - xChar.character.y;
	var cx:Number = mouseX - xChar.character.x;
	var Radians:Number = Math.atan2(cy,cx);
	var Degrees:Number = Radians * 180 / Math.PI;
	xChar.character.rotation = Degrees;
}
 
Flag Post

To be honest, it would be a lot easier to go with Draco’s solution.

 
Flag Post

yeah.. I’m not sure how to use the localtoglobal function in my thing.
My rightHand child is added from the character movieclip so from 0,0 (middle character) it goes to + x17y23 and doesnt move from there.

yeah.. I’m not sure how to use the localtoglobal function in my thing.
My rightHand child is added from the character movieclip so from 0,0 (middle character) it goes to + x17y23 and doesnt move from there.If i had a point going on it would go like this i think.

yeah.. I’m not sure how to use the localtoglobal function in my thing.
My rightHand child is added from the character movieclip so from 0,0 (middle character) it goes to + x17y23 and doesnt move from there.If i had a point going on it would go like this i think.


var testA:Point = new Point(xChar.rightHand.x, xChar.rightHand.y);
trace("Point: " + tempBullet.localToGlobal(testA));

yeah.. I’m not sure how to use the localtoglobal function in my thing.
My rightHand child is added from the character movieclip so from 0,0 (middle character) it goes to + x17y23 and doesnt move from there.If i had a point going on it would go like this i think.


var testA:Point = new Point(xChar.rightHand.x, xChar.rightHand.y);
trace("Point: " + tempBullet.localToGlobal(testA));
But this return me the attached x and y clip from the 0,0 of the character which is 17,23.. I need the current position of the hand on the stage any idea?

 
Flag Post

ok i dunnoh why it reply like this i just pasted text once edited it 2-3 time to see if i didnt duplicate text and no… cant fix

 
Flag Post
var testA:Point = new Point(0, 0);
testA = xChar.rightHand.localToGlobal(testA);
trace("Point: " + testA);
 
Flag Post

player_03 thanks a bunch man. That worked well, just had to set my tempBullet.x = testA.x etc..

Oh well at least ive learned something in this 2 day of hell, cos sin and tan. :P Still didnt know how to calculate it correctly but i have a big idea of whats its doing now.

And thanks again man. I will remember this function forever. (Thanks draco too for pointing it!)