Math and physics

Discussion and feedback on Construct 2

Post » Sun Aug 24, 2014 5:34 pm

Hey Everyone,

I have been doing a lot of work to understand how physics works with construct. Basically trying to figure out all the things that the manual doesn't tell you. Things like this:

Velocity = Impulse/Mass * 2500
Velocity = Force/Mass * 2500 (time)
AngularVelocity = Torque/Mass * 648.78892733564 (time)
Change in velocity = -Current Velocity * Linear Damping Coefficient (time)

...and other stuff, such as the difference between "set velocity" and using forces or impulses. There is also some order and logic peculiarities due to the Construct 2 update cycle.

I wrote more about it here ->
https://www.scirra.com/tutorials/1135/physics-the-rest-of-the-manual
Last edited by ruskul on Mon Aug 25, 2014 4:38 am, edited 2 times in total.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Sun Aug 24, 2014 7:13 pm

Thanks @ruskul that was pretty helpful and needy one! I always use to get C at school, So really appreciate your help.
Every Game is a Good Game it's just few people like and few does not! Keep coding people some day you will be a "NOTCH".
B
21
S
6
G
3
Posts: 85
Reputation: 4,607

Post » Mon Aug 25, 2014 6:08 am

Short version:
1.The 2500 is from the conversion factor (50) from box2d units to pixels.

2. Your equation to calculate won't work for all shapes. Instead of just mass you need to calculate the Moment of Inertia for that particular object. After that the only magic number is 0.02 or (1/50) which is the conversion factor from pixels to box2d units.

3.
AngularVelocity = Velocity / Diameter (pi)
should be:
AngularVelocity = Velocity * 180 / (radius * pi)

Long Version:
Box2d internally uses it's own distance unit (m) which is 0.02 pixels.
The conversion is:
1 [pixel] = 0.02 [m]
or
50 [pixel] = 1 [m]

The set Velocity action and getVelocity expression already do the conversion without help, but the conversion isn't considered with Force or Impulse.

Before I go any further I'd like to suggest that the mass expression is returning a bad value. The formula for mass with units is:
Code: Select all
Mass [d * m^2] = density [d] * area [m^2]

Where [d*m^2] is the same as [kg]

So for a 32x32 pixel square with density 1 the mass should be:
Code: Select all
area [m^2]= (height [pixel] * 0.02 [m/pixel]) * (width [pixel] * 0.02 [m /pixel]) = (32 * 0.02) * (32 * 0.02) [m^2] = 0.4096 [m^2]
mass [d * m^2] = density [d] * area [m^2] = 0.4096 [d * m^2]

But the mass expression returns 20.48 or 50 times 0.4096 which is incorrect since that makes the units [d*pixel*m].

This is only important because if it were correct you'd only have to multipy by 50 instead of 50*50 or 2500 with the Force and Impulse equations.


Torque is a bit trickier. The equation for Torque is:
Code: Select all
Torque [kg*m^2*deg/s^2] = inertia [kg*m^2] * ang_accel [deg/s^2]


Inertia is calculated differently for different shapes. For a square the equation is:
Code: Select all
Inertia = mass * (width^2 + height^2)/12

For mass we'll use 0.4096 and we'll need to covert height and width from [pixel] to [m] and we get:
Inertia = 0.4096 * ((32*0.02)^2 + (32*0.02)^2)/12 =~ 0.02796...

I then ran a test to apply a torque of 1 for 60 ticks. At the end the angular velocity was approximately 35.76. From that the acceleration could be calculated with acceleration=(final_speed-initial_speed)/time which ended up being 35.76.

The numbers agreed with the equation when it was plugged in:
torque = inertia * ang_accel
1 ?= 0.02796 * 35.76
1 ~= 0.9998

This link may also be useful as it predicts motion using equations much like yours.
parabola-tracjectory-tracing-with-physics-box2d_p640838?#p640838

-cheers
B
92
S
32
G
107
Posts: 5,280
Reputation: 69,971


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 14 guests