Help with physics
Help with physics
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
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
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: Help with physics
Are you mad? ( )nxtboyiii wrote:I have tried to use sind and cosd functions, but I just can't figure it out.
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
}
}
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.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Help with physics
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
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
-
- Posts: 26
- Joined: 12 Oct 2010, 08:19
Re: Help with physics
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
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
Can someone post code of all of that in NXC?
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: Help with physics
Why am I being ignored today*?nxtboyiii wrote:Can someone post code of all of that in NXC?
This should work good enough**.
* Actually I lied, I'm not being ignored by others. I'm ignoring myself.
** Well enough?
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Help with physics
well, e.g. this formula from DaAnxtboyiii wrote:Can someone post code of all of that in NXC?
Code: Select all
X = X_start+V*cos(Angle)*time
Y= Y_start+V*sin(Angle)*time-.5*g*time^2
and my formula is
Code: Select all
y=tand(Angle*x)- (g*x*x)/( 2*start_speed * cosd(Angle)*cosd(Angle) )
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
doc-helmut~
Is it the same code for the y axis?
What are all the variables?
Is it the same code for the y axis?
What are all the variables?
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: Help with physics
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)
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)
Last edited by HaWe on 15 Dec 2010, 19:33, edited 1 time in total.
Re: Help with physics
Does X just add 1 each time it loops?
If not, what does it add?
What should the gravity be?
If not, what does it add?
What should the gravity be?
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Who is online
Users browsing this forum: No registered users and 4 guests