|
-
Rotating projectiles
Hello,
I'm having an issue with my game that I can't fix, I'm clueless.
I'm trying to rotate my bullet (class-bassed MovieClip) so that it's always facing the direction it's traveling to, and I have no idea how to make that happen. The picture here at the bottom should illustrate how I want it to be and how it's right now.
The game is your average shooter at the moment. You click, the game calculates the rotation from that click and then sends the bullet to that direction. Gravity and friction affects the bullets, simple. Fixing the rotation homever is not simple, for me.
I would really, really appreciate any help. Thank you!
Illustration picture; http://i44.tinypic.com/fayk36.png
-
You know which direction it's moving in, so you just need to get the angle from the direction. Use Math.arctan2 to get an angle from the difference between two adjacent points in the bullet path.
-
What's the movement code for your projectiles right now? Rotating will be heavily based upon that and probably Math.atan2.
-
Thank you for answering, I think this will answer how the movements works.
gravity = 5
friction = -3
vx = 0
vy = 0
SpeedF = 15
this.x += vx;
this.y += vy;
xSpeed = Math.cos(((currentRotation + 90) * degreesToRadians));
ySpeed = Math.sin(((currentRotation + 90) * degreesToRadians));
x += xSpeed * SpeedF;
y += ySpeed * SpeedF;
-
That's still not everything, where are vx/vy set?
-
Oh yeah sorry I forgot;
vy += gravity;
vx += friction;
-
What's the deal with the two differen x/y assignments? It seems like you're assigning the x/y position twice. What is happening with your code?
-
 Originally Posted by swak
What's the deal with the two differen x/y assignments? It seems like you're assigning the x/y position twice. What is happening with your code?
As you probably can tell, I'm very new at this. So it's possible that the code is bad. I guess it would be better if y/xSpeed affected vx/vy directly instead of the y/x position.
SpeedF is not always 15, it's also affected by a meter that can sink, which will also sink SpeedF.
CurrentRotation is the rotation of the shot.
-
What I mean is if you're assigning xy twice than the second one should override the first one thus rendering the first one pointless. It might be better if you could post your code. On the other hand looking at how I think your code should work than the second x/y assignment should actually be vx and vy. Is this right?
-
 Originally Posted by swak
What I mean is if you're assigning xy twice than the second one should override the first one thus rendering the first one pointless. It might be better if you could post your code. On the other hand looking at how I think your code should work than the second x/y assignment should actually be vx and vy. Is this right?
Really? I'm pretty sure that += only affects, not overriding the results. So right now, the Y/X position should be affected both by vx/vy, and xSpeed/ySpeed, I believe.
I tried now to change the second Y/X assignment to vy/vx, but then the code just didn't work. I tried every combination and played around with everything else in the code but the bullets were just acting weird.
Last edited by Carl Blom; 02-10-2012 at 08:40 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|