Be a Supporter!

Lasersight and triangles

  • 323 Views
  • 7 Replies
New Topic Respond to this Topic
Arkuni
Arkuni
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Lasersight and triangles Nov. 24th, 2009 @ 03:44 PM Reply

Hey guys

I'm trying to figure out a way to calculate the position of my laser sight.

It is a 2D sidescrolling shooter. You got a red line ( the lasersight ), which goes from the tip of your gun to the mouse cursor. When you shoot, the gun bounces a little due to the recoil. Right now, the lasersight will still be drawn to the cursor, but I want it to jump parallel with the gun.

I got:
position a = guntip = x20,y300
position b = cursor = x200,y30
angle = 30 degrees
How do I finde position c? ( which is where the lasersight should be after firing ).


Okay, okay... something funny... something funny.... hmmm.... which quote am I most likely to get away with as my own?... I KNOW!!! "I eat my own shit!" - The local homeless

evan210
evan210
  • Member since: May. 14, 2007
  • Offline.
Forum Stats
Member
Level 26
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 04:13 PM Reply

probably just add the angle by a few degrees?
sorry if this doesn't answer your question; it's a little unclear...

Zealotti
Zealotti
  • Member since: Sep. 15, 2009
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 04:21 PM Reply

Hold the laser sight and gun within the same movieclip, apply movement and rotation to this.

Arkuni
Arkuni
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 04:32 PM Reply

@Evan

The 30 degrees is the increase of angle when firing the gun.
It's like this http://www.math.washington.edu/~greenber /tri.gif
Where V3 is the guntip, V1 is the mouse cursor and V2 ( my missing point ) is where the laser sight is after the gun have been shot and been raised by 36 degrees ( 30 in my case ).

@Zealotti

Haha. Pretty simple. Then I got a question only for you: How do I calculate the length of my lasersight line? I want a line straight from the tip to the mouse cursor. Not just tagged on as a grahpic.


Okay, okay... something funny... something funny.... hmmm.... which quote am I most likely to get away with as my own?... I KNOW!!! "I eat my own shit!" - The local homeless

Zealotti
Zealotti
  • Member since: Sep. 15, 2009
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 04:54 PM Reply

Just some trigonometry, lets call the movieclip that holds the gun and laser sight $parent and the starting point of the laser (within this movieclip) $offset. $length is the distance from the start of the laser to the end of the laser and $offset2 is the point within $parent that you need to make the laser extend to (i.e. draw a line from $offset to $offset2).

In pseudocode he have :

$x = $parent.x  - MouseX;
$y = $parent.y  - MouseY;

$length = sqrt($x^2 + $y^2);

$offset2.x = $offset.x + $x;
$offset2.y = $offset.y + $y

May have messed that up , am very very tired but I'm fairly sure that's right.

Zealotti
Zealotti
  • Member since: Sep. 15, 2009
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 04:55 PM Reply

At 11/24/09 04:54 PM, Zealotti wrote: $x = $parent.x - MouseX;
$y = $parent.y - MouseY;

$length = sqrt($x^2 + $y^2);

$offset2.x = $offset.x + $x;
$offset2.y = $offset.y + $y

Meant to put $parent.MouseX and $parent.MouseY rather than MouseX and MouseY.

Arkuni
Arkuni
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Lasersight and triangles Nov. 24th, 2009 @ 05:51 PM Reply

Thanks a bunch Zealotti. It's working now.

I would stil like a solution that didn't include stuffing the gun and lasersight into the same movieclip. Mostly out of curiosity.


Okay, okay... something funny... something funny.... hmmm.... which quote am I most likely to get away with as my own?... I KNOW!!! "I eat my own shit!" - The local homeless

Havegum
Havegum
  • Member since: Oct. 20, 2008
  • Offline.
Forum Stats
Member
Level 24
Melancholy
Response to Lasersight and triangles Nov. 24th, 2009 @ 06:06 PM Reply

Sinus and cosinus is your answer!

it should say something like this:

(YourAngle*radians)*sinus*speedOfLaser //Xposition
(YourAngle*radians)*cosinus*speedOfLaser //Yposition.

now, i could be mixing these up, but (angle*radians)*sinus should return a value between zero and one.
You have to look that up as i don't have any examples and don't remember it right now. I could be mixing sinus and cosinus, and i could have misspelled them.

Google trigonometry, and you will find the information you need :D


BBS Signature