Author Topic: Physics Lessons  (Read 51821 times)

0 Members and 2 Guests are viewing this topic.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Physics Lessons
« Reply #75 on: February 12, 2011, 06:05:36 pm »
How do you use tile masking with 5 by 5 tiles rather than 8 by 8 tiles?
In-progress: Graviter (...)

Offline kindermoumoute

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 836
  • Rating: +54/-3
    • View Profile
Re: Physics Lessons
« Reply #76 on: February 13, 2011, 06:15:15 am »
Well first off, if you want realistic pysics, you might want to consider switching to x256 format.  This is where every time you add 256 to your position variables, your character moves 1 pixel.  This will allow you to have more precision and possibly get the gravity effects you are looking for.

However, If you do not want to use x256 format and stick with what you have, you can try this simple physics piece of code:

Code: [Select]
:Lbl G
:Θ*3+GDB0->r5
:If {r5+3}>>-7
:{r5+3}+1->{r5+3}
:End
:
:{r5+1}+{r5+3}->{r5+1}
:While sub(GN,{r5}+5,{r5+1}+5
:If {r5+3}>>0
:{r5+1}-1->{r5+1}
:Else
:{r5+1}+1->{r5+1}
:End
:End
:
:If sub(GN,{r5}+5,{r5+1}+5)=0 && getKey(4)
:-7->{r5+3}
:End
:Return

This piece of code *should* give you some realistic gravity and jumping in your game
Your code don't work, no gravity affect my worms, and I don't understand what does it mean. :/
Projects :

Worms armageddon z80 :
- smoothscrolling Pixelmapping : 100%
- Map editor : 80%
- Game System : 0%

Tutoriel français sur l'Axe Parser
- 1ère partie : en ligne.
- 2ème partie : en ligne.
- 3ème partie : en ligne.
- 4ème partie : 10%
- Annexe : 100%

Offline matthias1992

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 408
  • Rating: +33/-5
    • View Profile
Re: Physics Lessons
« Reply #77 on: February 13, 2011, 09:39:58 am »
@builderboy.

could you roughly explain what you mean with x256 format? I can't  see the use of moving a pixel one unit by adding 256 to it instead of 1 (alas, that is what I am getting from what you said about it some posts up)

edit:

Awesome tuts by the way, imma digging it!! :D
« Last Edit: February 13, 2011, 09:40:25 am by matthias1992 »
MASM xxxxxxxxxx aborted | SADce ====:::::: 40% -Halted until further notice| XAOS =====::::: 50% -Units done| SKYBOX2D engine ========== 100% -Pre-alpha done. Need to  document it and extend |

~Those who dream by day are cognizant of much more than those who dream by night only. -Sir Edgar Allen Poe-

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Physics Lessons
« Reply #78 on: February 13, 2011, 09:44:02 am »
It's basically where your X and Y values are inflated (multiplied) by 256 for greater accuracy. In other words, when you actually display it, you'd use

Code: (Axe) [Select]
Pt-Change(X/256,Y/256,Pic0)

To move one pixel, then, you'd add 256 to either X or Y.

The point here is to simulate "half-pixels" and other fractions for the motion to seem smoother. For example, you could have the character move 1.5 pixels every frame by adding 384 to X instead of 256.




Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Physics Lessons
« Reply #79 on: February 13, 2011, 12:44:32 pm »
Well first off, if you want realistic pysics, you might want to consider switching to x256 format.  This is where every time you add 256 to your position variables, your character moves 1 pixel.  This will allow you to have more precision and possibly get the gravity effects you are looking for.

However, If you do not want to use x256 format and stick with what you have, you can try this simple physics piece of code:

Code: [Select]
:Lbl G
:Θ*3+GDB0->r5
:If {r5+3}>>-7
:{r5+3}+1->{r5+3}
:End
:
:{r5+1}+{r5+3}->{r5+1}
:While sub(GN,{r5}+5,{r5+1}+5
:If {r5+3}>>0
:{r5+1}-1->{r5+1}
:Else
:{r5+1}+1->{r5+1}
:End
:End
:
:If sub(GN,{r5}+5,{r5+1}+5)=0 && getKey(4)
:-7->{r5+3}
:End
:Return

This piece of code *should* give you some realistic gravity and jumping in your game
Your code don't work, no gravity affect my worms, and I don't understand what does it mean. :/

What happens to the worms?  What goes wrong?

Offline kindermoumoute

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 836
  • Rating: +54/-3
    • View Profile
Re: Physics Lessons
« Reply #80 on: February 15, 2011, 02:44:54 am »
They don't move...
Projects :

Worms armageddon z80 :
- smoothscrolling Pixelmapping : 100%
- Map editor : 80%
- Game System : 0%

Tutoriel français sur l'Axe Parser
- 1ère partie : en ligne.
- 2ème partie : en ligne.
- 3ème partie : en ligne.
- 4ème partie : 10%
- Annexe : 100%

Offline Waave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
Re: Physics Lessons
« Reply #81 on: May 05, 2011, 09:50:40 pm »
It's basically where your X and Y values are inflated (multiplied) by 256 for greater accuracy. In other words, when you actually display it, you'd use

Code: (Axe) [Select]
Pt-Change(X/256,Y/256,Pic0)

To move one pixel, then, you'd add 256 to either X or Y.

The point here is to simulate "half-pixels" and other fractions for the motion to seem smoother. For example, you could have the character move 1.5 pixels every frame by adding 384 to X instead of 256.

Thanks for this... I was wondering if there was a more elegant solution than just using 'Pause 250' at the end of my loop.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Physics Lessons
« Reply #82 on: May 18, 2011, 07:10:51 pm »
Single Segmented Rope Physics

   The situation is always the same, you are making your physics puzzle game that may or may not be a platformer, you want to add in a new game element, but you can't think of anything to add!  Maybe you want to add in some sort of rope element, but how in the world could we do this fast and efficiently?  Well this tutorial is going to teach you how to make a Single Segmented Force Based Rope Physics engine :D  That might sound complicated but lets break it down:

1) Single Segmented

    What single segmented means is that your rope will consist of only a single segment.  What this means is that we will only simulate the start and end of the rope, none of the stuff in the middle where the rope bends or whatnot.  Since one end of the rope is going to be fixed to a wall, in this tutorial we will only be simulating the object that is attached to the end of the rope.  When you go to display the rope however, you don't have to simply draw a line between the anchor and the object, you can get creative and do fancy things, but that is outside this tutorial.

2) Force Based

    There are many types of physics engines, and when combining them to create complicated effects, weird things can start to happen.  If one part of the physics engine devoted to rope physics moves an object without checking the other parts, it might accidentally move it into a wall or another object!  For this reason, the only thing this physics engine will do is impart forces onto the object, it will not move them.  We are also going to be assuming this rope is infinitely strong, and we will be ignoring the mass of the object.  By using only a force based engine, this gives us the highest possibility that this code will be able to be implemented into your existing engine.

3) The Conecpt

    So what happens to an object that is attached to a string?  What forces are exerted on it?  Before we can answer those questions, we need to decide how to define the string itself.  It needs an anchor point, and a length, so we will use three variables X,Y and L, to represent these.  We also need to use certain variables from our object, such as its position (I,J) and velocity (A,B).
 
Whenever the distance D from the object to the anchor is less than the length, we know that the rope is not going to be taught, and therefor cannot be imparting any force on the ball.  We could calculate the distance using the pythagorean theorem:

Code: [Select]
Sqrt((X-I)^2+(Y-J)^2)=D
But that uses a square root, and those are slow and sad.  Instead, we are going to square both sizes of the equation, since squaring is fast and happy!

Code: [Select]
(X-I)^2+(Y-J)^2=D^2
So now when we compare D to L, we will actually be comparing D^2 to L^2.  The size difference doesnt matter, since are only seeing if D is greater than L.  If you are using a x256 precision engine (which you should be :P) you can use this piece of code to find the distance in pixels (not x256!)

Code: [Select]
abs(X-I)->r1*^r1+(abs(Y-J)->r1*^r1)->D
So now we can tell if the object is pulling the string taught, or if it is letting it slack.  Obviously if it is slack (D<L) then we do not need to apply any force to the ball at all.  It is what happen when the string it taught (D>=L) that we start needing to apply a force. 

When a ball is moving out of the allowable ring of movement that the string allows, the first thing we need to do is eliminate the velocity that is taking it outside the ring.  This does not mean setting the velocity to 0.  This does also not mean we negate the velocity.  Imagine this:  The ball is falling straight down with the string above it.  When the string pulls taught, the ball will be yanked back up again and lose almost all velocity very quickly.  But if you drop it from an angle, off to the side, it will lose a little speed, but most of it will be transfered into a pendulum motion.  In a sense, all of the velocity that is directed out of the ring is being removed, and anything that is in line with the ring (tangential, its the motions it moves in when it swings) is kept.  Now this is a bit tricky to do, so bear with me here:

I-X//L->N
J-Y//L->O

This finds the Normal Vector of the string.  The math behind it isn't that important except that you know that it points in the same direction as the string, from base to object.  This will be important in determining how much velocity to remove, and from what direction to remove it from.  The string is the only thing that can take velocity from the object, and as such, it can only affect the object in the same direction the string is pointing.  The significance of this is that any change in the velocity of the object will be a multiple of this Normal Vector!  All that is left is to find this coefficient!

That coefficient just so happens to be the dot product of the objects velocity and the ropes normal vector.  A**N + (B**O)*-1->K
Spoiler For Spoiler:
Yeah, this isn't the 100% right dot product.  Technically speaking we would have to multiply two Normal Vectors, and AB is not normal.  But since we are going to be multiplying by AB anyways, we just combine the steps by not dividing and multiplying by AB :P I put this in a spoiler just because its pretty involved vector physics and not needed for the tutorial.  Similarly, multiplying by -1 isn't something that is done in a dot product, but is done later twice so i moved it back here so we only had to do it once :)

Now we need to do a quick test.  On the off chance that the ball is outside L, but actually moving *towards* the rope, K will be positive, and you don't want to affect the ball in this case.

Code: [Select]
If K<<0
Now all that is left to do is to add the velocity to the object!

Code: [Select]
K**N+A->A
K**O+B->B

Note that this will remove all velocity that is taking the ball outside the ring of string; stop it dead.  Sometimes this is undesired, so you can make it pull a little bit more and give the string a little "bounce"

Code: [Select]
K**N*3//2+A->A
K**O*3//2+B->B

And thats it!  You are done!  This is what the final result should look like when you piece all this together:
(Note that L^2 has been precomputed and put into M)

Code: [Select]
If abs(X-I)->r1*^r1+(abs(Y-J)->r1*^r1)<M
I-X//L->N
J-Y//L->O
A**N+(B**O)*-1->K
If K<<0
K**N*3//2+A->A
K**O*3//2+B->B
End
End

And this is what it should look like when implemented into a simple program!


« Last Edit: September 06, 2011, 10:42:04 pm by Builderboy »

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Physics Lessons
« Reply #83 on: May 18, 2011, 07:13:20 pm »
Awesome! I was just about to ask you how in hell you got it to work so nicely ^^
In-progress: Graviter (...)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Physics Lessons
« Reply #84 on: May 19, 2011, 10:39:59 am »
Yeah, I was surprised in the end how little lines of code I could do this in O.O  It took lots and lots of time to get it down to that level, the original even had 2 square root functions o.O

Offline yunhua98

  • You won't this read sentence right.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2718
  • Rating: +214/-12
  • Go take a dive in the River Lethe.
    • View Profile
Re: Physics Lessons
« Reply #85 on: May 19, 2011, 12:23:48 pm »
amazing.  O.O

Spoiler For =====My Projects=====:
Minor setback due to code messing up.  On hold for Contest.
<hr>
On hold for Contest.


Spoiler For ===Staff Memberships===:






Have you seen any good news-worthy programs/events?  If so, PM me with an article to be included in the next issue of CGPN!
The Game is only a demo, the code that allows one to win hasn't been done.
To paraphrase Oedipus, Hamlet, Lear, and all those guys, "I wish I had known this some time ago."
Signature Last Updated: 12/26/11
<hr>

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Physics Lessons
« Reply #86 on: May 19, 2011, 12:28:29 pm »
O.O That is some short source code (AC.8XP). Thanks a lot Builderboy. However, I didn't get a word since I never used velocity and I didn't even get the first lesson so would I get this.

Nice job!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Physics Lessons
« Reply #87 on: May 25, 2011, 04:29:57 am »
Do I smell a Donkey Kong Country port? O.O (there were ropes and that stuff in the original game)

Offline Darl181

  • «Yo buddy, you still alive?»
  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3408
  • Rating: +305/-13
  • VGhlIEdhbWU=
    • View Profile
    • darl181.webuda.com
Re: Physics Lessons
« Reply #88 on: July 08, 2011, 07:21:39 pm »
Quote from: http://ourl.ca/4279/169823 (masked sprites thing)
Code:
Lbl MSK
{r2^8+{r2/8*W+(r1/8)+L1}+Str1}e r1
Return


Where r1 is your X position, r2 is your Y position, W is the width of your tilemap (in tiles), L1 is the location of your matrix data buffer, and Str1 is the start of your Mask Sprite table.

I get what this does, but what exactly is the matrix data buffer?  Does it determine which sprite to read or something?


EDIT:
How do you use tile masking with 5 by 5 tiles rather than 8 by 8 tiles?
Quote from: line I don't understand completely but most of it I do :P
Where r1 is your X position, r2 is your Y position, W is the width of your tilemap (in tiles), L1 is the location of your matrix data buffer, and Str1 is the start of your Mask Sprite table.
Just change W, afaict ;)
« Last Edit: July 08, 2011, 07:38:12 pm by Darl181 »
Vy'o'us pleorsdti thl'e gjaemue

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Physics Lessons
« Reply #89 on: July 08, 2011, 07:53:25 pm »
I'm pretty sure he means "pointer to matrix data," since "buffer" typically refers to locations of free RAM