<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Create a dynamic jump</title>
		<link>http://www.allegro.cc/forums/view/618593</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 22 Jan 2022 14:46:05 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ya<br />i was thinking to make my character jump with variable height, similar to platform games (e.g. super mario bros).</p><p>the easiest way that i know is to create two jump buttons (pressed and down), and use a min (-y goes to up) function when the character is falling.</p><p>i tried to use the keypressed() (not worked) and the key[] array (looks like a down button).</p><p>So my question as a beginner in allegro: which function/flag i could use to detect these two types of events? (pressed and down)</p><p>PS: i&#39;m using allegro 4 and creating a platformer game
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MegaRubber)</author>
		<pubDate>Tue, 18 Jan 2022 23:42:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi:<br /> When I did my platformer I just used a series of images.<br /> It worked very well. If you want, I can send you what I did.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Scooter)</author>
		<pubDate>Wed, 19 Jan 2022 01:04:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Look into position, velocity, acceleration equations</p><p>With pressing any key direction, all you&#39;re going to do is change the acceleration</p><p>Velocity is based on acceleration<br />v(t) = v(0) + a(t)</p><p>You can split this up for x and y<br />vx(t) = vx(0) + ax(t)<br />vy(t) = vy(0) + ay(t)</p><p>Position is a function of velocity and acceleration<br />P(t) = P(0) + v(t) + 1/2a(t^2)</p><p>When you press the jump key. Add a negative acceleration (assuming negative is up). There is always a positive acceleration due to gravity. The negative acceleration must exceed the positive to make him jump.</p><p>Gravity is a change in velocity of (9.8m/s) every second. You would need to adjust for frame rate and pixel size. How many pixels is one meter? How many frames are processed is one second?</p><p>Of course any collision would drop the velocity to zero. You could base damage if velocity was too great the player hit the wall.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DanielH)</author>
		<pubDate>Wed, 19 Jan 2022 03:52:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://">Scooter</a> said:</div><div class="quote"><p>
Hi:<br />When I did my platformer I just used a series of images.<br />It worked very well. If you want, I can send you what I did.
</p></div></div><p>

If you could, I would be very grateful!</p><div class="quote_container"><div class="title"><a href="http://">DanielH</a> said:</div><div class="quote"><p>
Look into position, velocity, acceleration equations</p><p>With pressing any key direction, all you&#39;re going to do is change the acceleration</p><p>Velocity is based on acceleration<br />v(t) = v(0) + a(t)</p><p>You can split this up for x and y<br />vx(t) = vx(0) + ax(t)<br />vy(t) = vy(0) + ay(t)</p><p>Position is a function of velocity and acceleration<br />P(t) = P(0) + v(t) + 1/2a(t^2)</p><p>When you press the jump key. Add a negative acceleration (assuming negative is up). There is always a positive acceleration due to gravity. The negative acceleration must exceed the positive to make him jump.</p><p>Gravity is a change in velocity of (9.8m/s) every second. You would need to adjust for frame rate and pixel size. How many pixels is one meter? How many frames are processed is one second?</p><p>Of course any collision would drop the velocity to zero. You could base damage if velocity was too great the player hit the wall. 
</p></div></div><p>

I finished the logic of variable jump and I didn&#39;t use these equations, but the logic of them, gravity, and the info about properties of the game were very helpful.</p><p>thx <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MegaRubber)</author>
		<pubDate>Wed, 19 Jan 2022 09:42:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi Mega:</p><p>  Above is &#39;mario.zip&#39;. This is a very simple example of mario jumping. No physics!</p><p>Use Left and Right arrow keys to move mario. Press Space Bar to make mario jump up</p><p>and spin around and come back down.</p><p>This uses Allegro 5 and done on Linux. You should have no problem to make it run</p><p>on Allegro 4. If zip file does NOT work for you, let me know and I will send you</p><p>another one. Hope this helps. Have a great day!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Scooter)</author>
		<pubDate>Wed, 19 Jan 2022 18:59:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>All you need for jumping is a few simple equations.</p><p>If you know the duration you want your player to be in the air, and you know the force of gravity, it&#39;s easy to predict it&#39;s max jump height.</p><pre>
h(t) = h0 + v0*t + a*t^2 / 2
h`(t) = v0 + at
h`(t/2) = v0 + at/2 = 0
a = -2v0/t
h(t/2) = h0 + v0*t/2 + a*t^2/4 / 2
h(t/2) = v0*t/2 + -2v0*t^2/t/4 = v0*t/4
</pre><p>
t is the duration, h is the height, a is the acceleration of gravity, v0 is the initial jump velocity.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 19 Jan 2022 23:37:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In A4 you can use fixed numbers.</p><p>All you have to do is add each frame the value of gravity to the object that jumps to modify the force of fall.</p><p>grav = 0.2;</p><p>BY JUMP:<br />movy = -2;</p><p>BY FRAME:<br />movy += grav;<br />obj.y += movy;</p><p>You just have to adjust the values according to the game. And you may want to make it jump higher the longer the jump is held, it just adds or initializes again on &#39;movy&#39;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RmBeer2)</author>
		<pubDate>Thu, 20 Jan 2022 07:51:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Depending on the game, you don&#39;t even use proper physics.</p><p>Many games have &quot;fixed velocity up&quot; for X frames, then &quot;fixed velocity down&quot;, instead of acceleration. Depends on what you want.</p><p>In that case it&#39;s like</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">struct</span> player<span class="k2">{</span>
<span class="number">  2</span><span class="k1">float</span> x, y, height<span class="k2">;</span>
<span class="number">  3</span><span class="k2">}</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="c">//attempt to start jump here (because you pressed a jump key)</span>
<span class="number">  6</span><span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>is_jumping <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">!</span>is_falling<span class="k2">)</span> <span class="c">//if we're not falling or jumping up, we can start a jump</span>
<span class="number">  7</span>  <span class="k2">{</span>
<span class="number">  8</span>  is_jumping <span class="k3">=</span><span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number">  9</span>  is_falling <span class="k3">=</span><span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 10</span>  height <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="c">//reset our jump height counter.</span>
<span class="number"> 11</span>  <span class="k2">}</span>
<span class="number"> 12</span>
<span class="number"> 13</span><span class="k1">if</span><span class="k2">(</span>is_jumping <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">!</span>is_falling<span class="k2">)</span> <span class="c">//if we're jumping, add height until we reach max</span>
<span class="number"> 14</span>  <span class="k2">{</span>
<span class="number"> 15</span>  y <span class="k3">+</span><span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
<span class="number"> 16</span>  height <span class="k3">+</span><span class="k3">=</span> <span class="n">5</span><span class="k2">;</span> <span class="c">//track how far we've gone up since we started</span>
<span class="number"> 17</span>  <span class="k1">if</span><span class="k2">(</span>height <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">100</span><span class="k2">)</span><span class="k2">{</span>is_jumping <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span> is_falling <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span><span class="k2">}</span> <span class="c">//if we've gone far enough, stop going up</span>
<span class="number"> 18</span>  <span class="k2">}</span>
<span class="number"> 19</span><span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>is_jumping <span class="k3">&amp;</span><span class="k3">&amp;</span> is_falling<span class="k2">)</span> <span class="c">//if falling, fall until we hit a floor</span>
<span class="number"> 20</span>  <span class="k2">{</span>
<span class="number"> 21</span>  y <span class="k3">-</span><span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
<span class="number"> 22</span>  <span class="k1">if</span><span class="k2">(</span> we_touched_a_floor<span class="k2">(</span><span class="k2">)</span> <span class="k2">)</span> <span class="k2">{</span>is_jumping <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span> is_falling <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span><span class="k2">}</span>
<span class="number"> 23</span>  <span class="k2">}</span>
</div></div><p>

Many older/retro games used this style. It&#39;s &quot;one speed up/down&quot; jumping. Instead of accelerating and decellerating.</p><p>Basically what I&#39;ve done above is create a finite state machine. It sounds big/complex but it&#39;s not. There are three modes or &quot;states&quot; that a person is in. <br /> - Normal/Idle/Walking (not jumping), that&#39;s is_jumping==false, is_falling==false. We&#39;re walking around.<br /> - Jumping, that&#39;s is_jumping==true,is_falling=false. We&#39;re moving upward.<br /> - Falling, that&#39;s is_jumping==false,is_falling=true. We&#39;re falling.</p><p>Each state has its own set of code that runs. Whether moving you up, moving you down, or not moving you up or down. And in a real game, you&#39;d also have in those sections code for making your character &quot;look&quot; like he&#39;s jumping or falling like in Mario Bros.</p><p>And, mathematically, binary numbers can have only two numbers. Since we have three states, we&#39;d need a minimum of two binary numbers (2^2 = 4 states max) to fit them in. If we wanted five states, we&#39;d need (2^3 = 8 states max) three boolean numbers to store those possibilities.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Thu, 20 Jan 2022 22:00:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Lot&#39;s of games don&#39;t actually use proper physics. There is a lot of good analysis on youtube</p><p><a href="https://www.youtube.com/watch?v=yorTG9at90g">https://www.youtube.com/watch?v=yorTG9at90g</a><br /><a href="https://www.youtube.com/watch?v=hG9SzQxaCm8">https://www.youtube.com/watch?v=hG9SzQxaCm8</a></p><p>Or a search for game+design+jump will show up tons of useful stuff.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Fri, 21 Jan 2022 14:24:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think we&#39;re all missing the OT. Which was how to detect keypress and keydown events in Allegro 4.</p><p><a href="https://bitbucket.org/bugsquasher/unofficial-allegro-5-binaries/downloads/Allegro443.chm">https://bitbucket.org/bugsquasher/unofficial-allegro-5-binaries/downloads/Allegro443.chm</a></p><p>That&#39;s a CHM manual for Allegro 4.4. See keypressed() and readkey(). Keep an array of keystates and when they change state that is a press or a release. When their state stays the same, that is a hold / down or open / up.</p><p><span class="source-code"><span class="k1">while</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/keypressed"><span class="a">keypressed</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>command<span class="k2">(</span><a href="http://www.allegro.cc/manual/readkey"><span class="a">readkey</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span><span class="k2">}</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 22 Jan 2022 14:46:05 +0000</pubDate>
	</item>
</rss>
