## Lasersight and triangles

• 407 Views
• 7 Replies
Arkuni
Arkuni
• Member since: Mar. 14, 2005
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Lasersight and triangles 2009-11-24 15:44:02

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 2009-11-24 16:13:44

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 2009-11-24 16:21:59

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 2009-11-24 16:32:45

@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 2009-11-24 16:54:01

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 2009-11-24 16:55:50

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 2009-11-24 17:51:56

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 2009-11-24 18:06:32