Help with physics

News, rumors, and other broad discussion topics.
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Help with physics

Post 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
Thanks, and have a nice day,
nxtboy III

programnxt.com
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Help with physics

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Help with physics

Post 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:
Image
where ß is the shooting angle and v0 is the start speed
dad-and-adam
Posts: 26
Joined: 12 Oct 2010, 08:19

Re: Help with physics

Post 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
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: Help with physics

Post by nxtboyiii »

Can someone post code of all of that in NXC?
Thanks, and have a nice day,
nxtboy III

programnxt.com
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Help with physics

Post 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. :twisted:
** Well enough?
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Help with physics

Post 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)
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: Help with physics

Post by nxtboyiii »

doc-helmut~
Is it the same code for the y axis?
What are all the variables?
Thanks, and have a nice day,
nxtboy III

programnxt.com
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Help with physics

Post 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)
Last edited by HaWe on 15 Dec 2010, 19:33, edited 1 time in total.
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: Help with physics

Post by nxtboyiii »

Does X just add 1 each time it loops?
If not, what does it add?
What should the gravity be?
Thanks, and have a nice day,
nxtboy III

programnxt.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest