• 13
• 15
• 19
• 27
• 9

# Leaping like a fish out of water?

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

## 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 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.
Letting:V0 = Initial VelocityA  = Launch AngleR  = Range of Projectile(distance between breaking the surface and reentry)h  = Apex(highest point?) of trajectoryt  = any instant of time after launch(in seconds, I presume)T  = total time from launch to impactg  = wasn't defined on the page;I'm guessing gravitywith y and x being your Cartesian coordinates things.Formulas:x(t)   = (V0*cos(A))*ty(t)   = (V0*sin(A))*t-(g*t^2)/2Vx(t)  = V0*cos(A)Vy(t)  = V0*sin(A)-g*tv(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 on other sites
Thanks nerd_boy!

y(t) = (V0*sin(A))*t-(g*t^2)/2 did the trick perfectly. Now I just need to figure out how to rotate smoothly on the downward fall to prevent the yo-yo effect and I'm all set. Hopefully that'll be as easy as I'm anticipating.

Thanks again!