Page 1 of 2
Help with physics
Posted: 11 Dec 2010, 03:29
by nxtboyiii
I need help with physics in NXC.
There is a game for the iPhone and iPod Touch called iShoot where you shoot each other in tanks(2D), just like Scorched Earth.
I have been trying to figure out how to make a shooting bullet shoot from a cannon like in real life.
I have tried to use sind and cosd functions, but I just can't figure it out.
I would need a way to have a function where you input the power, starting x and y axis, and the angle.
Can someone please help?
Thanks, and have an nice day,
nxtboy III
Re: Help with physics
Posted: 11 Dec 2010, 03:47
by muntoo
nxtboyiii wrote:I have tried to use sind and cosd functions, but I just can't figure it out.
Are you mad? (
)
Why use that when simple method will do? (With just a bit of atan2(), but it's better if you know the slope originally.)
Code: Select all
//#define AIR_RESISTANCE 0.1
#define AIR_RESISTANCE 0
#define GRAVITY 2
#define GROUND 0
x = 0;
y = 0;
dx = 5;
dy = 5;
while(1)
{
// dx or dy is basically speed
x += dx;
y += dy;
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
}
}
Brian Davis will probably scream at me about the terrible air resistance physics. (Not to mention all the other stuff, but mostly the air resistance.
)
In order to find dx or dy:
Code: Select all
#define SPEED 5
float slope = 1.0; // anything you want; slope = dy/dx
//slope = atan(degrees); // probably won't work perfectly
dx = (1.0 / slope) * SPEED;
dy = slope * SPEED;
There may be mistakes (a lot of them), so if you want to correct me, physics professors of the world, and company, feel free to shoot yourself in the head first. Only if you survive, you are worthy of criticizing me.
Re: Help with physics
Posted: 11 Dec 2010, 09:39
by HaWe
it's not a sin or cos but a parabola = polynom of 2nd degree ("parabolic trajectory", open to below: negative square coefficient!)
simulation see this:
http://de.wikipedia.org/wiki/Ballistik
if you want the ballistc formula y(x) it's like this:
where ß is the shooting angle and v0 is the start speed
Re: Help with physics
Posted: 11 Dec 2010, 10:11
by dad-and-adam
Hi,
Let pretend the tanks are on the moon -so we can ignore air resistance.
As an alternative to the equaion provided by doc-helmut, consider X and Y as functions of time.
X = X_start+V*cos(Angle)*time
Y= Y_start+V*sin(Angle)*time-.5*g*time^2
Where:
X_start and Y_start is the initial position in length units (like meters)
V is the velocity in length units/time (like meters/second)
Angle is the elevation angle above horizontal
g is the acceleration due to gravity in length units/time^2 (like meters/seconds^2)
time is time in time units (like seconds)
Using X and Y as functions of time could be an advantage if the target tank is moving -so it's position is also a function of time.
Dave
Re: Help with physics
Posted: 15 Dec 2010, 03:14
by nxtboyiii
Can someone post code of all of that in NXC?
Re: Help with physics
Posted: 15 Dec 2010, 06:55
by muntoo
nxtboyiii wrote:Can someone post code of all of that in NXC?
Why am I being ignored today*?
This should work good enough**.
* Actually I lied, I'm not being ignored by others. I'm ignoring myself.
** Well enough?
Re: Help with physics
Posted: 15 Dec 2010, 10:00
by HaWe
nxtboyiii wrote:Can someone post code of all of that in NXC?
well, e.g. this formula from DaA
Code: Select all
X = X_start+V*cos(Angle)*time
Y= Y_start+V*sin(Angle)*time-.5*g*time^2
is actually the formula you can use by NXC...!
and my formula is
Code: Select all
y=tand(Angle*x)- (g*x*x)/( 2*start_speed * cosd(Angle)*cosd(Angle) )
(tand=tangens[degrees], cosd=cosinus[degrees]
g = 9,81 m/s², Angle= shooting angle [degrees], use start_speed in [m/s], x in [m] )
Be careful that
Angle*x ≠±90°
and
Angle ≠±90° // cos=>division by zero, tan=>undefined (infinite!)
(CMIIW)
Re: Help with physics
Posted: 15 Dec 2010, 13:09
by nxtboyiii
doc-helmut~
Is it the same code for the y axis?
What are all the variables?
Re: Help with physics
Posted: 15 Dec 2010, 13:32
by HaWe
my provided formula is the dependency of the height of the bullet (y) from the flown distance (x)
f(x)=y(x)=.... (put in x and what you get is y)
DaA's formula calculates the height of the bullet (y) and the flown distance (x) as a function of time
f(t)= f(x(t), y(t)) (put in time t and what you get are the related coordinates (x,y) of the bullet at that special time)
which parameter or variable don`t you understand?
Angle? the uplifted angle by that you raise your cannon to the sky (0=horizontal, 90=vertical)
x? the flown distance
y? the height of the bullet above ground
g? gravity constant
x_start? start at x-axis (e.g.,x_start=0)
y_start? height at start, y-axis (e.g.,y_start=0 if the cannon is on the ground level and not on a hill etc)
time? elapsed time, time axis (e.g., start time=0)
Re: Help with physics
Posted: 15 Dec 2010, 17:26
by nxtboyiii
Does X just add 1 each time it loops?
If not, what does it add?
What should the gravity be?