Jumping, a python solution
I'm trying to work out how to make a character jump a specified height and within a specified number of steps and be effected by a gravitational force. For instance, jump 400px within 50 steps.
So far I can simulate a rocket effect so that starting velocity is slow but by the time 50 steps is up it will have reached a height of 400px like this:
HEIGHT = 400
STEPS = 50
g = 2.0 * HEIGHT / (STEPS * (STEPS - 1))
y = 0
velocity = 0
for index in range(STEPS):
y += velocity
velocity += g
print y, velocity # prints 400.0 16.3265306122
I've tried starting the velocity at -16.3265306122 and adding g each step but that doesn't result in y = -400, velocity = 0 as you'd except. The result of doing it like that is y = -416.32653061 and velocity = 4.49112969036e-11.
What do you think?
python
add a comment |
I'm trying to work out how to make a character jump a specified height and within a specified number of steps and be effected by a gravitational force. For instance, jump 400px within 50 steps.
So far I can simulate a rocket effect so that starting velocity is slow but by the time 50 steps is up it will have reached a height of 400px like this:
HEIGHT = 400
STEPS = 50
g = 2.0 * HEIGHT / (STEPS * (STEPS - 1))
y = 0
velocity = 0
for index in range(STEPS):
y += velocity
velocity += g
print y, velocity # prints 400.0 16.3265306122
I've tried starting the velocity at -16.3265306122 and adding g each step but that doesn't result in y = -400, velocity = 0 as you'd except. The result of doing it like that is y = -416.32653061 and velocity = 4.49112969036e-11.
What do you think?
python
add a comment |
I'm trying to work out how to make a character jump a specified height and within a specified number of steps and be effected by a gravitational force. For instance, jump 400px within 50 steps.
So far I can simulate a rocket effect so that starting velocity is slow but by the time 50 steps is up it will have reached a height of 400px like this:
HEIGHT = 400
STEPS = 50
g = 2.0 * HEIGHT / (STEPS * (STEPS - 1))
y = 0
velocity = 0
for index in range(STEPS):
y += velocity
velocity += g
print y, velocity # prints 400.0 16.3265306122
I've tried starting the velocity at -16.3265306122 and adding g each step but that doesn't result in y = -400, velocity = 0 as you'd except. The result of doing it like that is y = -416.32653061 and velocity = 4.49112969036e-11.
What do you think?
python
I'm trying to work out how to make a character jump a specified height and within a specified number of steps and be effected by a gravitational force. For instance, jump 400px within 50 steps.
So far I can simulate a rocket effect so that starting velocity is slow but by the time 50 steps is up it will have reached a height of 400px like this:
HEIGHT = 400
STEPS = 50
g = 2.0 * HEIGHT / (STEPS * (STEPS - 1))
y = 0
velocity = 0
for index in range(STEPS):
y += velocity
velocity += g
print y, velocity # prints 400.0 16.3265306122
I've tried starting the velocity at -16.3265306122 and adding g each step but that doesn't result in y = -400, velocity = 0 as you'd except. The result of doing it like that is y = -416.32653061 and velocity = 4.49112969036e-11.
What do you think?
python
python
edited Nov 23 '18 at 11:58
jonrsharpe
77.5k11104211
77.5k11104211
asked Nov 23 '18 at 11:56
cookertroncookertron
677
677
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In the first run you're estimating the integral of velocity over time using the lowest value that velocity actually has during each step. Then the last value of the velocity that you get from velocity += g
is printed out but was never used in the integration. So in going the other way you could start with a velocity of -16, which is the value before the last, the last one that was actually used. That will get you -400 0.326530612245
and the second number, the new last velocity, is again not used in the integration and is the value of g
. So you could simply revise the code to not print out the unused last velocity:
for index in range(STEPS):
y += velocity
used_velocity = velocity
velocity += g
print y, used_velocity
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
add a comment |
when you are increasing the y (height) you are staring from velocity as zero, so in first step value of y is zero.
When you are starting at y=0 and velocity as -16.3265306122, in first step you are travelling 16.32653061, which is the difference.
Instead of negative velocity, try with negative value of g. (while coming down you should feel negative of what you felt going up.)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446283%2fjumping-a-python-solution%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the first run you're estimating the integral of velocity over time using the lowest value that velocity actually has during each step. Then the last value of the velocity that you get from velocity += g
is printed out but was never used in the integration. So in going the other way you could start with a velocity of -16, which is the value before the last, the last one that was actually used. That will get you -400 0.326530612245
and the second number, the new last velocity, is again not used in the integration and is the value of g
. So you could simply revise the code to not print out the unused last velocity:
for index in range(STEPS):
y += velocity
used_velocity = velocity
velocity += g
print y, used_velocity
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
add a comment |
In the first run you're estimating the integral of velocity over time using the lowest value that velocity actually has during each step. Then the last value of the velocity that you get from velocity += g
is printed out but was never used in the integration. So in going the other way you could start with a velocity of -16, which is the value before the last, the last one that was actually used. That will get you -400 0.326530612245
and the second number, the new last velocity, is again not used in the integration and is the value of g
. So you could simply revise the code to not print out the unused last velocity:
for index in range(STEPS):
y += velocity
used_velocity = velocity
velocity += g
print y, used_velocity
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
add a comment |
In the first run you're estimating the integral of velocity over time using the lowest value that velocity actually has during each step. Then the last value of the velocity that you get from velocity += g
is printed out but was never used in the integration. So in going the other way you could start with a velocity of -16, which is the value before the last, the last one that was actually used. That will get you -400 0.326530612245
and the second number, the new last velocity, is again not used in the integration and is the value of g
. So you could simply revise the code to not print out the unused last velocity:
for index in range(STEPS):
y += velocity
used_velocity = velocity
velocity += g
print y, used_velocity
In the first run you're estimating the integral of velocity over time using the lowest value that velocity actually has during each step. Then the last value of the velocity that you get from velocity += g
is printed out but was never used in the integration. So in going the other way you could start with a velocity of -16, which is the value before the last, the last one that was actually used. That will get you -400 0.326530612245
and the second number, the new last velocity, is again not used in the integration and is the value of g
. So you could simply revise the code to not print out the unused last velocity:
for index in range(STEPS):
y += velocity
used_velocity = velocity
velocity += g
print y, used_velocity
answered Nov 23 '18 at 13:36
Mike O'ConnorMike O'Connor
999812
999812
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
add a comment |
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
This works perfectly thank you!
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
g = 2.0 * h / (s * (s - 1)), v = -(g * (s - 1))
– cookertron
Nov 23 '18 at 15:11
add a comment |
when you are increasing the y (height) you are staring from velocity as zero, so in first step value of y is zero.
When you are starting at y=0 and velocity as -16.3265306122, in first step you are travelling 16.32653061, which is the difference.
Instead of negative velocity, try with negative value of g. (while coming down you should feel negative of what you felt going up.)
add a comment |
when you are increasing the y (height) you are staring from velocity as zero, so in first step value of y is zero.
When you are starting at y=0 and velocity as -16.3265306122, in first step you are travelling 16.32653061, which is the difference.
Instead of negative velocity, try with negative value of g. (while coming down you should feel negative of what you felt going up.)
add a comment |
when you are increasing the y (height) you are staring from velocity as zero, so in first step value of y is zero.
When you are starting at y=0 and velocity as -16.3265306122, in first step you are travelling 16.32653061, which is the difference.
Instead of negative velocity, try with negative value of g. (while coming down you should feel negative of what you felt going up.)
when you are increasing the y (height) you are staring from velocity as zero, so in first step value of y is zero.
When you are starting at y=0 and velocity as -16.3265306122, in first step you are travelling 16.32653061, which is the difference.
Instead of negative velocity, try with negative value of g. (while coming down you should feel negative of what you felt going up.)
edited Nov 23 '18 at 14:01
answered Nov 23 '18 at 13:07
SachSach
625517
625517
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446283%2fjumping-a-python-solution%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown