I admit it. I am a mathematical idiot. Should I try to do this at home? Probably not without serious injury. Anyway. Anyone who has a little experience in games and physics who can view this code and make it clear why there is a built-in “loss” of speed in the prototype will be greatly admired.
concept: an actor falls from the sky, at some point crosses the border (trampoline.y), after which gravity (in this example, a value of 1.6) stops working and the restitution coefficient (in this case, the value is 6).
right now I'm trying to create a situation with a zero amount in which you get the same speed as exiting the trampoline "bounce" (hero.y> = trampoline.y), as you did. as the tracks show.
I tried five different approaches, and this is the last, given the understanding that with any given tick of my game timer (33 ms) we could go over the trampoline boundary, and thus, the acceleration rules will be changed. I really thought I had it, but the results show a decrease in speed after each bounce, which I did not expect.
I pass the following code to your reviewed review:
var newV:Number; if (hero.y < trampoline.y) { newV = hero.velocityY + gravityAccelerationInPixels } else { newV = hero.velocityY + TrampolineActor.SPRING_ACCELERATION } var newY:Number = hero.y + newV; trace("Coming in: y=" + hero.y + " oldVelocity=" + hero.velocityY + " newVelocity=" + newV + " Change in V: " + (newV - hero.velocityY) + ". Testing for newY=" + newY); if ((hero.y < trampoline.y && newY < trampoline.y) || (hero.y >= trampoline.y && newY >= trampoline.y)) { hero.y = newY; hero.velocityY = newV; } else { trace("SPLIT"); var percent:Number = (trampoline.y - hero.y) / newV; trace("Percent: " + percent); var newVV:Number; if (hero.y < trampoline.y) { // going down! newVV = hero.velocityY + percent * gravityAccelerationInPixels; trace("New velocity before split: " + hero.velocityY + " Change in V: " + (newVV - hero.velocityY)); newVV += (1 - percent) * TrampolineActor.SPRING_ACCELERATION; trace("Percent after split: " + (1 - percent) + " Change in V: " + (newVV - hero.velocityY)); } else { // movin on up! newVV = hero.velocityY + percent * TrampolineActor.SPRING_ACCELERATION; trace("New velocity before split: " + hero.velocityY + " Change in V: " + (newVV - hero.velocityY)); newVV += (1 - percent) * gravityAccelerationInPixels; trace("Percent after split: " + (1 - percent) + " Change in V: " + (newVV - hero.velocityY)); } trace("New velocity: " + newVV + " Change in V: " + (newVV - hero.velocityY)); hero.velocityY = newVV; hero.y += hero.velocityY; }
Additional info: The origin of the screen is top left, so y-- = higher up Currently my tick is set to 33 ms gravityAccelerationInPixels currently 1.62 (fairly random) SPRING_ACCELERATION is arbitrary 6, but I hope to set this number to control. how far below the trampoline the character can go during the deceleration / absorption cycle.
Currently, I hope to get a solution in which the speed at tic after the character contacts the trampoline will be the same (but negative) from the speed immediately before the entrance. I even tried to keep both getting the position and entry speed, but it looks like a hiccup.