Jump to content
  • Advertisement
Sign in to follow this  

Leaping like a fish out of water?

This topic is 3684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm working on a little game where you play a Tremors style worm and the objective is to gobble up unsuspecting animals and people walking over the ground. I've got the underground movement of the worm working the way I want, but I have no idea (terrible at math) idea how to get the worm to shoot out of the ground in a nice, clean trajectory after that. You can play around with the worm using its underground movement here. Basically when the worm jumps out of the ground, I'd like it to follow a nice, clean arc pattern based on the angle as it comes up from underground. Here's the code I use for movement:
// Setting it up:
if (keyboard.isKeyDown(Keyboard.A) || keyboard.isKeyDown(Keyboard.LEFT))
	movieClip.rotation -= (speed + 0.1) * turnSpeed * deltaTime;
if (keyboard.isKeyDown(Keyboard.D) || keyboard.isKeyDown(Keyboard.RIGHT))
	movieClip.rotation += (speed + 0.1) * turnSpeed * deltaTime;
if (keyboard.isKeyDown(Keyboard.W) || keyboard.isKeyDown(Keyboard.UP))
	speed += acceleration * deltaTime;
	if (speed > maxSpeed)
		speed = maxSpeed;
if (keyboard.isKeyDown(Keyboard.S) || keyboard.isKeyDown(Keyboard.DOWN))
	speed -= acceleration * deltaTime;
	if (speed < 60)
		speed = 60;

// Moving it:
var angleRadians : Number = (movieClip.rotation / 360) * (2.0 * Math.PI);

position.x += Math.cos(angleRadians) * (speed * deltaTime);
position.y += Math.sin(angleRadians) * (speed * deltaTime);

My math skills are atrocious, so any guidance is welcome here. Thanks guys!

Share this post

Link to post
Share on other sites
A quick look in my copy of Physics for Game Devlopers[Bourg], page 103 seems to have the formulas you need(I'll quickly paraphrase them, trying not to make any errors;my math is a bit rusty-ish).

Keep in mind that these formulas presume that the ground is flat, more or less.

V0 = Initial Velocity
A = Launch Angle
R = Range of Projectile(distance between breaking the surface and reentry)
h = Apex(highest point?) of trajectory
t = any instant of time after launch(in seconds, I presume)
T = total time from launch to impact
g = wasn't defined on the page;I'm guessing gravity
with y and x being your Cartesian coordinates things.

x(t) = (V0*cos(A))*t
y(t) = (V0*sin(A))*t-(g*t^2)/2
Vx(t) = V0*cos(A)
Vy(t) = V0*sin(A)-g*t
v(t) = sqrt(V0^2-2*g*t*V0*sin(A)+g^2*t^2)
h = (V0)^2*sin_squared(A))/(2*g)
R = V0*T*cos(A)
T = (2*V0*sin(A))/g

Not having really messed with game physics, you may want another opinion, but I'd say the formulas x(t) and y(t) are probably what you'd be wanting. If so, you'll just have to detect when the worm is breaking through the surface and save some version of the time, whether the clock or tick counts(not sure how Flash handles it), so you can determine the number of seconds since whenever you need to draw it.

I'd presume that you'd be using the head as the driving force for the worm, and thus the anchor for determining the arc. If so, I'm not quite sure what changes, if any, you'd have to do for the worm moving above ground. I'd say either not allowing it to wriggle when in the arc or, if you can, maybe having the keyboard input rotate the head yet keeping it on the arc's path, and just having the body curl around it. :/

Hope I helped somehow, best of luck.

Share this post

Link to post
Share on other sites
Evidently rotating the head smoothly wasn't as easy as I thought it was going to be. I've tried calculating the distance from the ground and using that to determine how much to rotate per frame but that ends up with little to no rotation up high and then just before it hits the ground, a lot of rotation which looks really silly.

Anyone have any ideas?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!