Page 2 of 2

Re: Help with physics

Posted: 15 Dec 2010, 19:29
by HaWe
nothing adds anything by itself unless you write a program that will do it.
You may use my provided function and increase x in each loop if you want to calculate y based on x,
and you may use DaA's function and increase t in each loop if you want to calculate x and y (x,y) based on t.

g is a physical constant, called gravitational acceleration.
It is g=9.81 m/s²
http://en.wikipedia.org/wiki/Gravitational_acceleration

Sorry, I can't explain the mathematical/physical principles (in Germany kids learn that stuff at grammar school, when they are about 13/14 years old , IIRC).
I can only tell you what's the formula (function) like, to understand it I think you have to read a physics textbook.

Re: Help with physics

Posted: 15 Dec 2010, 23:00
by nxtboyiii
Here is the code and .rxe for shooting. I think it will shoot at 40 degrees.

Re: Help with physics

Posted: 15 Dec 2010, 23:51
by nxtboyiii
I tried all the things you guys posted, but none of them work. :(

Re: Help with physics

Posted: 16 Dec 2010, 01:26
by muntoo
Using the code Doc posted (based on DaA):

Code: Select all

X = X_start+V*cos(Angle)*time
Y= Y_start+V*sin(Angle)*time-.5*g*time^2
We go like this:

Code: Select all

#define g 10
#define TIME_STEP 100

int X_start = 20;
int Y_start = 0;
int X;
int Y;
int angle = 40;
long V = 5; // pixels per second

for(long time = 0; 1; time += TIME_STEP)
{
    X = X_start + V*cosd(angle)*time/1000;
    Y = Y_start + V*sind(angle)*time/1000 - g*time*time/1000/1000/2;

    if(Y < Y_start)
        break;

    // Draw
    CircleOut(X, Y, 3, DRAW_OPT_FILL_SHAPE);
}
Notice I used sind() and cosd(), so the input is in degrees. With sin() and cos(), the input is in radians.

I recommend using this version, though:

Code: Select all

#define g 10
#define TIME_STEP 100

int X_start = 20;
int Y_start = 0;
int X = X_start;
int Y = Y_start;
int angle = 40;
long V = 5; // pixels per second

for(long time = 0; 1; time += TIME_STEP)
{
    X += V*cosd(angle)*(TIME_STEP)/1000;
    Y += (V*sind(angle) - g)*(TIME_STEP)/1000;

    if(Y < Y_start)
        break;

    // Draw
    CircleOut(X, Y, 3, DRAW_OPT_FILL_SHAPE);
}
EDIT: I fixed the code above. (The `Y += ...;` part. And the messed up #TIME_STEP)
This is what happens when LaTeX is not used. :)
Image

Re: Help with physics

Posted: 17 Dec 2010, 00:25
by dad-and-adam
Hello nxtboyiii
Does X just add 1 each time it loops?
If not, what does it add?
What should the gravity be?
Anything can be added to X each time it loops.
Each loop will probably need a time delay so the "projectile" doesn't go too fast.
The number for gravity would be 9.8 if the units are meters per second squared.
If you are using pixels (or anything else) for the length then gravity will be a different number (like 32.2 for feet per second squared).
I would recommend making an initial guess for the variables and adjusting them based on how the display looks.

Make the gravity value bigger to get the projectile to drop faster (but I'm sure you already knew that).
I tried all the things you guys posted, but none of them work.
What specifically doesn't work? Do you get anything on the display at all?
Dave

Re: Help with physics

Posted: 17 Dec 2010, 01:34
by nxtboyiii
I have a working version!!
Can someone improve it at all?
It is based on muntoo's version.
Anyways,

Code: Select all

#define STARTING_POWER 20
#define AIR_RESISTANCE 0
#define GRAVITY 0
#define GRAVITY2 0.2
#define GROUND 0

float x = 50;
float y = 20;
float s=3;
byte power;
task main()
{
int Angle=Random(130);
float dx = cosd(Angle);
float dy = sind(Angle);
power=STARTING_POWER;
while(1)
{
x += dx;
y += dy;
if(power != 0)
{
power--;
}
else
{
dy-=GRAVITY2;
}
if(x > 0)
{
x -= AIR_RESISTANCE;
if(x < 0)
{
x = 0;
}
}
else if(x < 0)
{
x += AIR_RESISTANCE;
if(x > 0)
{
x = 0;
}
}
y -= GRAVITY;
if(y <= GROUND)
{
y = GROUND;
//Explosion
break;
}
RectOut(x,y,2,2);
Wait(30);
ClearScreen();
}
Wait(3000);
}

Re: Help with physics

Posted: 17 Dec 2010, 03:03
by muntoo
BTW, here's a good tutorial for Game Physics. It's very easy to understand, even for beginners. (The first/second ones, at least.)

Re: Help with physics

Posted: 17 Dec 2010, 20:24
by HaWe
muntoo wrote:BTW, here's a good tutorial for Game Physics.
muntoo,
have you honestly ever implemented RK4 (Runge Kutta order 4 integrator) by using NXC? I'm really curious to see your code!