Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
909c98fa
Commit
909c98fa
authored
Oct 16, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Animated sprite physics bodies now correctly transfers energy to dynamic bodies
parent
46415fa0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
4 deletions
+45
-4
physics_body.dart
packages/flutter_sprites/lib/src/physics_body.dart
+2
-0
physics_node.dart
packages/flutter_sprites/lib/src/physics_node.dart
+43
-4
No files found.
packages/flutter_sprites/lib/src/physics_body.dart
View file @
909c98fa
...
...
@@ -42,6 +42,8 @@ class PhysicsBody {
Vector2
_lastPosition
;
double
_lastRotation
;
Vector2
_targetPosition
;
double
_targetAngle
;
Object
tag
;
...
...
packages/flutter_sprites/lib/src/physics_node.dart
View file @
909c98fa
...
...
@@ -74,6 +74,37 @@ class PhysicsNode extends Node {
// Remove bodies that were marked for destruction during the update phase
_removeBodiesScheduledForDestruction
();
// Assign velocities and momentum to static and kinetic bodies
for
(
box2d
.
Body
b2Body
=
b2World
.
bodyList
;
b2Body
!=
null
;
b2Body
=
b2Body
.
getNext
())
{
// Fetch body
PhysicsBody
body
=
b2Body
.
userData
;
// Skip all dynamic bodies
if
(
b2Body
.
getType
()
==
box2d
.
BodyType
.
DYNAMIC
)
{
body
.
_lastPosition
=
null
;
body
.
_lastRotation
=
null
;
continue
;
}
// Update linear velocity
if
(
body
.
_lastPosition
==
null
)
{
b2Body
.
linearVelocity
.
setZero
();
}
else
{
Vector2
velocity
=
(
body
.
_targetPosition
-
body
.
_lastPosition
)
/
dt
;
b2Body
.
linearVelocity
=
velocity
;
body
.
_lastPosition
=
null
;
}
// Update angular velocity
if
(
body
.
_lastRotation
==
null
)
{
b2Body
.
angularVelocity
=
0.0
;
}
else
{
double
angularVelocity
=
(
body
.
_targetAngle
-
body
.
_lastRotation
)
/
dt
;
b2Body
.
angularVelocity
=
angularVelocity
;
body
.
_lastRotation
=
0.0
;
}
}
// Calculate a step in the simulation
b2World
.
stepDt
(
dt
,
10
,
10
);
...
...
@@ -113,22 +144,30 @@ class PhysicsNode extends Node {
}
void
_updatePosition
(
PhysicsBody
body
,
Point
position
)
{
if
(
body
.
_lastPosition
==
null
)
body
.
_lastPosition
=
body
.
_body
.
position
;
if
(
body
.
_lastPosition
==
null
&&
body
.
type
==
PhysicsBodyType
.
static
)
{
body
.
_lastPosition
=
new
Vector2
.
copy
(
body
.
_body
.
position
);
body
.
_body
.
setType
(
box2d
.
BodyType
.
KINEMATIC
);
}
Vector2
newPos
=
new
Vector2
(
position
.
x
/
b2WorldToNodeConversionFactor
,
position
.
y
/
b2WorldToNodeConversionFactor
);
double
angle
=
body
.
_body
.
getAngle
();
body
.
_body
.
setTransform
(
newPos
,
angle
);
if
(
body
.
type
==
PhysicsBodyType
.
dynamic
)
{
body
.
_body
.
setTransform
(
newPos
,
angle
);
}
else
{
body
.
_targetPosition
=
newPos
;
body
.
_targetAngle
=
angle
;
}
body
.
_body
.
setAwake
(
true
);
}
void
_updateRotation
(
PhysicsBody
body
,
double
rotation
)
{
if
(
body
.
_lastRotation
==
null
)
body
.
_lastRotation
=
body
.
_body
.
getAngle
();
Vector2
pos
=
body
.
_body
.
position
;
double
newAngle
=
radians
(
rotation
);
body
.
_body
.
setTransform
(
pos
,
newAngle
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment