To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Reply
 
Thread Tools Search this Thread Display Modes
Old 11-10-2009, 07:39 PM   #1
nolen
bibuti.
 
nolen's Avatar
 
Join Date: Sep 2002
Location: az.
Posts: 183
[Box2D] Apply force to body, dependant on mouse distance

I'm trying to create a setup where you have a player that is controlled by clicking where you want them to move.

What I'd like to implement is a scenario where you click the mouse, and the player jumps toward the location that was just clicked. This in itself isn't terribly difficult, however I would also like to ensure that if I click above another body (a wall, for example), that the player will perform a jump that has him land on said body.

The image below helps explain a bit better, I hope:




The arrows B and C can both be accomplished by doing something like this:

Code:
hero.ApplyImpulse(new b2Vec2((mx - hx)*hero.GetMass(), (my - hy)*hero.GetMass()), hero.GetPosition());
Where mx, my are the mouse x/y coordinates and hx, hy are the hero's coordinates. This moves the hero toward the point clicked, but does not ensure that it is his stopping place. I want to be able to click at point B and have the hero jump/move toward that position but stop once he arrives. If I click at point C, I want the hero to jump no higher than the clicked point.

And, my biggest hurdle, I want to click point A and have the hero apply a high enough vertical impulse to be able to land on the wall.


What types of calculations/tracking should I create to make this happen? I don't need code written out for me, but really just the method to this madness.
__________________
i'm obsessed with video games.
nolen is offline   Reply With Quote
Old 11-12-2009, 04:32 PM   #2
joshstrike
Senior Member
 
joshstrike's Avatar
 
Join Date: Jan 2001
Location: Saigon, Viet Nam
Posts: 979
It strikes me that a click at (C) and a click at (A) are doing two very different things in this example... when clicking (C), the player is clicking at the top of the arc that the character is supposed to leap to. Clicking (A) on the other hand is clicking the endpoint of an arc, and you're asking for the code to differentiate that type of click and come up with a higher midway point to be the top of the requested arc.

The way around this I guess is to think about (C) as an end-point, too. If that's true, than to get to either (C) or (A), the character will have to jump higher than those points. How much higher depends on how far the character is going to move horizontally, how quickly they're going to move horizontally, and how much vertical velocity they're going to lose.

Assuming you have a constant gravity acting on the character's y-velocity, and assuming the x-velocity is going to be constant throughout the jump, you should be able to take the horizontal distance divided by two and figure out how long in ticks the character will take to complete the second half of the arc in x-distance. If you know that, just create a scratch acceleration var and run it that many times in a loop, adding the value of itself+gravity each time, and the y-position of the end point minus that total gravity should give you the top of a parabola that's halfway between start and finish and at the right height above the endpoint to make the jump look realistic.
joshstrike is offline   Reply With Quote
Old 11-13-2009, 06:47 PM   #3
nolen
bibuti.
 
nolen's Avatar
 
Join Date: Sep 2002
Location: az.
Posts: 183
Quote:
Originally Posted by joshstrike View Post
Assuming you have a constant gravity acting on the character's y-velocity, and assuming the x-velocity is going to be constant throughout the jump, you should be able to take the horizontal distance divided by two and figure out how long in ticks the character will take to complete the second half of the arc in x-distance. If you know that, just create a scratch acceleration var and run it that many times in a loop, adding the value of itself+gravity each time, and the y-position of the end point minus that total gravity should give you the top of a parabola that's halfway between start and finish and at the right height above the endpoint to make the jump look realistic.
Thanks for your reply!

Your explanation almost makes sense to me, but I'm lost when you mention "a scratch acceleration and it run it many times in a loop". The thought of calculating a parabola of motion seems to be the most elegant solution though.

Could you elaborate a bit on this method? Again, thank you for your help.
__________________
i'm obsessed with video games.
nolen is offline   Reply With Quote
Old 11-13-2009, 07:24 PM   #4
joshstrike
Senior Member
 
joshstrike's Avatar
 
Join Date: Jan 2001
Location: Saigon, Viet Nam
Posts: 979
Basically at the top of the parabola, your upward velocity is wiped out by gravity, and the character should have a y-velocity of zero.
So the next tick, the character will fall 1 pixel, let's say, if gravity = 1. It will have a downward y velocity of 1. In the tick after that, it will have a downward velocity of 2, and will have traveled a total of 3 pixels. In the tick after that, it will have a downward velocity of 3 and will have traveled 6 pixels.

The idea of a scratch var is just a quick way to make that calculation to determine the distance it will fall off the top of its arc, given that you know how long it needs to fall to cover the 2nd half of the x-distance.

Code:
var testvel:Number = 0;
var ydist:Number = 0;
var ticks:Number = (xdistance / 2) / xvelocity;
for (var k:int=0;k<ticks;k++) {
	testvel += gravity;
	ydist += testvel;
}
At the end of that, ydist should equal the distance that the character will fall vertically from the top of a parabola, dependent on the strength of the gravity and the length of time it's falling. There are other, trig ways of calculating this but I'm not hip to them, being as I'm something of a dropout. But since you can get the length of time the character needs to fall based on half the x-distance between start and finish, the ydist represents the additional height over the finishing position that the character would be at when it reaches the top of its arc.

Hope that makes some kind of sense...
joshstrike is offline   Reply With Quote
Old 11-23-2009, 02:04 PM   #5
nolen
bibuti.
 
nolen's Avatar
 
Join Date: Sep 2002
Location: az.
Posts: 183
Hey I just wanted to thank you joshstrike for your help.

I've found my solution, which came about because of your suggestions regarding the calculation of a parabola.


So yeah, thanks!
__________________
i'm obsessed with video games.
nolen is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:46 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.