|
|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
bibuti.
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()); 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. |
|
|
|
|
|
#2 |
|
Senior Member
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. |
|
|
|
|
|
#3 | |
|
bibuti.
Join Date: Sep 2002
Location: az.
Posts: 183
|
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Senior Member
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;
}
Hope that makes some kind of sense... |
|
|
|
|
|
#5 |
|
bibuti.
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. |
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|