Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
1
10-LibGDX-Box2D
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
a.guest
10-LibGDX-Box2D
Commits
3854d0df
Commit
3854d0df
authored
Feb 09, 2018
by
a.guest
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
objects update
parent
3b6bd7e5
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
287 additions
and
96 deletions
+287
-96
dynamicObject.java
core/src/game_objects/dynamicObject.java
+0
-82
dynamicObject.java
core/src/physics/dynamicObject.java
+146
-0
gameObject.java
core/src/physics/gameObject.java
+6
-2
staticObject.java
core/src/physics/staticObject.java
+113
-0
world_simulation.java
core/src/physics/world_simulation.java
+3
-0
MainGameScreen.java
core/src/screens/MainGameScreen.java
+19
-12
No files found.
core/src/game_objects/dynamicObject.java
deleted
100644 → 0
View file @
3b6bd7e5
package
game_objects
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType
;
import
com.badlogic.gdx.graphics.Color
;
import
com.badlogic.gdx.graphics.Texture
;
import
com.badlogic.gdx.Input.Keys
;
import
com.badlogic.gdx.graphics.g2d.Sprite
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.physics.box2d.*
;
import
com.badlogic.gdx.physics.box2d.BodyDef.BodyType
;
import
physics.world_simulation
;
public
class
dynamicObject
extends
gameObject
{
// Used to create the object in the simulation world
public
Body
objBody
;
Fixture
objFixture
;
float
objradius
;
public
dynamicObject
()
{
}
public
dynamicObject
(
world_simulation
worldSim
,
Vector2
screenPos
,
float
radius
,
int
SHAPE
)
{
BodyDef
objBodyDef
;
FixtureDef
objFixtureDef
;
objradius
=
radius
;
// Create object in simulation
// We need a BodyDef to hold all the information
objBodyDef
=
new
BodyDef
();
// We want a dynamic body, one which will move and be affected by the physics
objBodyDef
.
type
=
BodyType
.
DynamicBody
;
// Calculate the position in the simulation based on the desired on screen position
Vector2
simPos
=
new
Vector2
(
worldSim
.
getSimPos
(
screenPos
));
objBodyDef
.
position
.
set
(
simPos
);
// Create our body in the world using the body definition
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
// Create a circle shape with a radius of 6
CircleShape
circle
=
new
CircleShape
();
circle
.
setRadius
(
radius
*
worldSim
.
meterToPixel
);
// Create a fixture definition to apply physics to our shape.
objFixtureDef
=
new
FixtureDef
();
objFixtureDef
.
shape
=
circle
;
objFixtureDef
.
density
=
0.5f
;
// a measure of the objects solidness
objFixtureDef
.
friction
=
0.4f
;
// a measure of how much the object will stick to other objs
objFixtureDef
.
restitution
=
0.6f
;
// a measure of bounciness.
// Create the fixture and attach it to the body
objFixture
=
objBody
.
createFixture
(
objFixtureDef
);
// shape no longer needed so can be disposed of
circle
.
dispose
();
}
public
void
Update
()
{
}
public
void
Render
(
world_simulation
worldSim
,
ShapeRenderer
shapeRend
)
{
Vector2
mySimPos
=
new
Vector2
(
objBody
.
getPosition
().
x
,
objBody
.
getPosition
().
y
);
Vector2
myRenderPos
=
worldSim
.
getRenderPos
(
mySimPos
);
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
circle
(
myRenderPos
.
x
,
myRenderPos
.
y
,
objradius
);
shapeRend
.
setColor
(
Color
.
WHITE
);
shapeRend
.
end
();
}
}
core/src/physics/dynamicObject.java
0 → 100644
View file @
3854d0df
package
physics
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType
;
import
com.badlogic.gdx.graphics.Color
;
import
com.badlogic.gdx.graphics.Texture
;
import
com.badlogic.gdx.Input.Keys
;
import
com.badlogic.gdx.graphics.g2d.Sprite
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.math.MathUtils
;
import
com.badlogic.gdx.physics.box2d.*
;
import
com.badlogic.gdx.physics.box2d.BodyDef.BodyType
;
public
class
dynamicObject
extends
gameObject
{
// Used to create the object in the simulation world
public
Body
objBody
;
Fixture
objFixture
;
int
shape
;
Vector2
simSize
;
Vector2
renderSize
;
Color
colour
;
public
dynamicObject
()
{
}
public
dynamicObject
(
world_simulation
worldSim
,
Vector2
screenPos
,
Vector2
size
,
float
angle_deg
,
int
SHAPE
,
Color
theCol
,
float
density
,
float
friction
,
float
restitution
)
{
BodyDef
objBodyDef
;
FixtureDef
objFixtureDef
;
colour
=
theCol
;
renderSize
=
new
Vector2
(
size
);
simSize
=
new
Vector2
(
size
);
shape
=
SHAPE
;
Vector2
simPos
=
new
Vector2
(
worldSim
.
getSimPos
(
screenPos
));
// We need a BodyDef to hold all the information
objBodyDef
=
new
BodyDef
();
// We want a dynamic body, one which will move and be affected by the physics
objBodyDef
.
type
=
BodyType
.
DynamicBody
;
if
(
SHAPE
==
worldSim
.
RECTANGLE
)
{
simSize
.
x
=
simSize
.
x
/
2
;
simSize
.
y
=
simSize
.
y
/
2
;
simPos
.
x
=
simPos
.
x
+
simSize
.
x
;
simPos
.
y
=
simPos
.
y
+
simSize
.
y
;
objBodyDef
.
position
.
set
(
simPos
);
objBodyDef
.
angle
=
(
angle_deg
/
(
180
f
))*
MathUtils
.
PI
;
// Create our body in the world using the body definition
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
// Create a circle shape with a radius of 6
PolygonShape
rect
=
new
PolygonShape
();
rect
.
setAsBox
(
simSize
.
x
,
simSize
.
y
);
// Create a fixture definition to apply physics to our shape.
objFixtureDef
=
new
FixtureDef
();
objFixtureDef
.
shape
=
rect
;
objFixtureDef
.
density
=
density
;
// a measure of the objects solidness
objFixtureDef
.
friction
=
friction
;
// a measure of how much the object will stick to other objs
objFixtureDef
.
restitution
=
restitution
;
// a measure of bounciness.
// Create the fixture and attach it to the body
objFixture
=
objBody
.
createFixture
(
objFixtureDef
);
// shape no longer needed so can be disposed of
rect
.
dispose
();
}
else
if
(
SHAPE
==
worldSim
.
CIRCLE
)
{
objBodyDef
.
position
.
set
(
simPos
);
// Create our body in the world using the body definition
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
// Create a circle shape with a radius of 6
CircleShape
circle
=
new
CircleShape
();
circle
.
setRadius
(
simSize
.
x
);
// Create a fixture definition to apply physics to our shape.
objFixtureDef
=
new
FixtureDef
();
objFixtureDef
.
shape
=
circle
;
objFixtureDef
.
density
=
density
;
// a measure of the objects solidness
objFixtureDef
.
friction
=
friction
;
// a measure of how much the object will stick to other objs
objFixtureDef
.
restitution
=
restitution
;
// a measure of bounciness.
// Create the fixture and attach it to the body
objFixture
=
objBody
.
createFixture
(
objFixtureDef
);
// shape no longer needed so can be disposed of
circle
.
dispose
();
}
}
public
void
Update
()
{
}
public
void
Render
(
world_simulation
worldSim
,
ShapeRenderer
shapeRend
)
{
Vector2
mySimPos
=
new
Vector2
(
objBody
.
getPosition
().
x
,
objBody
.
getPosition
().
y
);
Vector2
myRenderPos
=
worldSim
.
getRenderPos
(
mySimPos
);
float
deg_angle
=
(
objBody
.
getAngle
()
/
MathUtils
.
PI
)*
180
f
;
if
(
shape
==
worldSim
.
RECTANGLE
)
{
myRenderPos
.
y
-=
renderSize
.
y
/
2
;
myRenderPos
.
x
-=
renderSize
.
x
/
2
;
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
setColor
(
colour
);
shapeRend
.
rect
(
myRenderPos
.
x
,
myRenderPos
.
y
,
renderSize
.
x
/
2
,
renderSize
.
y
/
2
,
renderSize
.
x
,
renderSize
.
y
,
1
f
,
1
f
,
deg_angle
);
shapeRend
.
end
();
}
else
if
(
shape
==
worldSim
.
CIRCLE
)
{
myRenderPos
.
y
-=
renderSize
.
y
/
2
;
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
setColor
(
colour
);
shapeRend
.
circle
(
myRenderPos
.
x
,
myRenderPos
.
y
,
renderSize
.
x
);
shapeRend
.
end
();
}
}
}
core/src/
game_object
s/gameObject.java
→
core/src/
physic
s/gameObject.java
View file @
3854d0df
package
game_object
s
;
package
physic
s
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.graphics.Texture
;
import
com.badlogic.gdx.graphics.Texture
;
...
@@ -6,13 +6,17 @@ import com.badlogic.gdx.Input.Keys;
...
@@ -6,13 +6,17 @@ import com.badlogic.gdx.Input.Keys;
import
com.badlogic.gdx.graphics.g2d.Sprite
;
import
com.badlogic.gdx.graphics.g2d.Sprite
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.physics.box2d.*
;
import
com.badlogic.gdx.physics.box2d.*
;
import
physics.world_simulation
;
public
class
gameObject
{
public
class
gameObject
{
// Texture and Sprite are used to render the object to the screen
// Texture and Sprite are used to render the object to the screen
public
Texture
texture
;
public
Texture
texture
;
public
Sprite
sprite
;
public
Sprite
sprite
;
public
gameObject
()
public
gameObject
()
{
{
...
...
core/src/
game_object
s/staticObject.java
→
core/src/
physic
s/staticObject.java
View file @
3854d0df
package
game_object
s
;
package
physic
s
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.Gdx
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer
;
...
@@ -11,13 +11,14 @@ import com.badlogic.gdx.math.Vector2;
...
@@ -11,13 +11,14 @@ import com.badlogic.gdx.math.Vector2;
import
com.badlogic.gdx.physics.box2d.*
;
import
com.badlogic.gdx.physics.box2d.*
;
import
com.badlogic.gdx.physics.box2d.BodyDef.BodyType
;
import
com.badlogic.gdx.physics.box2d.BodyDef.BodyType
;
import
physics.world_simulation
;
public
class
staticObject
extends
gameObject
{
public
class
staticObject
extends
gameObject
{
// Used to create the object in the simulation world
// Used to create the object in the simulation world
Body
objBody
;
Body
objBody
;
Fixture
objFixture
;
Fixture
objFixture
;
Vector2
objSize
;
int
shape
;
Vector2
simSize
;
Vector2
renderSize
;
Color
colour
;
public
staticObject
()
public
staticObject
()
...
@@ -25,33 +26,56 @@ public class staticObject extends gameObject{
...
@@ -25,33 +26,56 @@ public class staticObject extends gameObject{
}
}
public
staticObject
(
world_simulation
worldSim
,
Vector2
screenPos
,
Vector2
size
,
int
SHAPE
)
public
staticObject
(
world_simulation
worldSim
,
Vector2
screenPos
,
Vector2
size
,
int
SHAPE
,
Color
theCol
)
{
{
BodyDef
objBodyDef
;
BodyDef
objBodyDef
;
PolygonShape
objShape
;
objSize
=
new
Vector2
(
size
);
colour
=
theCol
;
// Create object in simulation
// We need a BodyDef to hold all the information
renderSize
=
new
Vector2
(
size
);
objBodyDef
=
new
BodyDef
();
simSize
=
new
Vector2
(
size
);
shape
=
SHAPE
;
Vector2
simPos
=
new
Vector2
(
worldSim
.
getSimPos
(
screenPos
));
Vector2
simPos
=
new
Vector2
(
worldSim
.
getSimPos
(
screenPos
));
simPos
.
y
=
simPos
.
y
+
(
size
.
y
/
2
);
objBodyDef
.
position
.
set
(
simPos
);
// Create our body in the world using the body definition
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
if
(
SHAPE
==
worldSim
.
RECTANGLE
)
{
simSize
.
x
=
simSize
.
x
/
2
;
simSize
.
y
=
simSize
.
y
/
2
;
objBodyDef
=
new
BodyDef
();
simPos
.
x
=
simPos
.
x
+
simSize
.
x
;
simPos
.
y
=
simPos
.
y
+
simSize
.
y
;
objBodyDef
.
position
.
set
(
simPos
);
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
PolygonShape
objShape
;
objShape
=
new
PolygonShape
();
objShape
=
new
PolygonShape
();
objShape
.
setAsBox
(
size
.
x
/
2
,
size
.
y
/
2
);
objShape
.
setAsBox
(
simSize
.
x
,
simSize
.
y
);
// Create the fixture and attach it to the body
objBody
.
createFixture
(
objShape
,
0.0f
);
// shape no longer needed so can be disposed of
objShape
.
dispose
();
}
else
if
(
SHAPE
==
worldSim
.
CIRCLE
)
{
objBodyDef
=
new
BodyDef
();
objBodyDef
.
position
.
set
(
simPos
);
objBody
=
worldSim
.
world
.
createBody
(
objBodyDef
);
CircleShape
objShape
;
objShape
=
new
CircleShape
();
objShape
.
setRadius
(
simSize
.
x
);
// Create the fixture and attach it to the body
// Create the fixture and attach it to the body
objBody
.
createFixture
(
objShape
,
0.0f
);
objBody
.
createFixture
(
objShape
,
0.0f
);
// shape no longer needed so can be disposed of
// shape no longer needed so can be disposed of
objShape
.
dispose
();
objShape
.
dispose
();
}
}
}
...
@@ -64,12 +88,25 @@ public class staticObject extends gameObject{
...
@@ -64,12 +88,25 @@ public class staticObject extends gameObject{
{
{
Vector2
mySimPos
=
new
Vector2
(
objBody
.
getPosition
().
x
,
objBody
.
getPosition
().
y
);
Vector2
mySimPos
=
new
Vector2
(
objBody
.
getPosition
().
x
,
objBody
.
getPosition
().
y
);
Vector2
myRenderPos
=
worldSim
.
getRenderPos
(
mySimPos
);
Vector2
myRenderPos
=
worldSim
.
getRenderPos
(
mySimPos
);
myRenderPos
.
y
-=
objSize
.
y
/
2
;
if
(
shape
==
worldSim
.
RECTANGLE
)
{
myRenderPos
.
y
-=
renderSize
.
y
/
2
;
myRenderPos
.
x
-=
renderSize
.
x
/
2
;
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
rect
(
myRenderPos
.
x
,
myRenderPos
.
y
,
objSize
.
x
,
objSize
.
y
);
shapeRend
.
setColor
(
colour
);
shapeRend
.
setColor
(
Color
.
WHITE
);
shapeRend
.
rect
(
myRenderPos
.
x
,
myRenderPos
.
y
,
renderSize
.
x
,
renderSize
.
y
);
shapeRend
.
end
();
shapeRend
.
end
();
}
}
else
if
(
shape
==
worldSim
.
CIRCLE
)
{
myRenderPos
.
y
-=
renderSize
.
y
/
2
;
shapeRend
.
begin
(
ShapeType
.
Filled
);
shapeRend
.
setColor
(
colour
);
shapeRend
.
circle
(
myRenderPos
.
x
,
myRenderPos
.
y
,
renderSize
.
x
);
shapeRend
.
end
();
}
}
...
...
core/src/physics/world_simulation.java
View file @
3854d0df
...
@@ -24,6 +24,9 @@ public class world_simulation {
...
@@ -24,6 +24,9 @@ public class world_simulation {
final
boolean
DEBUG
=
false
;
final
boolean
DEBUG
=
false
;
public
final
int
RECTANGLE
=
0
;
public
final
int
CIRCLE
=
1
;
Vector2
RenderMin
;
Vector2
RenderMin
;
Vector2
RenderMax
;
Vector2
RenderMax
;
Vector2
SimMin
;
Vector2
SimMin
;
...
...
core/src/screens/MainGameScreen.java
View file @
3854d0df
...
@@ -6,16 +6,20 @@ import com.badlogic.gdx.graphics.GL20;
...
@@ -6,16 +6,20 @@ import com.badlogic.gdx.graphics.GL20;
import
com.mygdx.game.*
;
import
com.mygdx.game.*
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.Input
;
import
com.badlogic.gdx.Input
;
import
com.badlogic.gdx.graphics.Color
;
import
game_objects.*
;
import
physics.*
;
import
physics.world_simulation
;
public
class
MainGameScreen
extends
ScreenTemplate
{
public
class
MainGameScreen
extends
ScreenTemplate
{
world_simulation
theWorld
;
world_simulation
theWorld
;
dynamicObject
circle1
;
dynamicObject
dynCircle
;
staticObject
ground
;
dynamicObject
dynBlock
;
staticObject
wall
;
staticObject
statGround
;
staticObject
statBall
;
float
density
=
0.5f
;
float
friction
=
0.4f
;
float
restitution
=
0.6f
;
float
torque
=
1.0f
;
float
torque
=
1.0f
;
...
@@ -27,9 +31,11 @@ public class MainGameScreen extends ScreenTemplate{
...
@@ -27,9 +31,11 @@ public class MainGameScreen extends ScreenTemplate{
Gdx
.
input
.
setInputProcessor
(
this
);
Gdx
.
input
.
setInputProcessor
(
this
);
circle1
=
new
dynamicObject
(
theWorld
,
new
Vector2
(
300
,
300
),
30
,
0
);
dynCircle
=
new
dynamicObject
(
theWorld
,
new
Vector2
(
300
,
300
),
new
Vector2
(
50
,
0
),
45
f
,
theWorld
.
CIRCLE
,
Color
.
PURPLE
,
density
,
friction
,
restitution
);
ground
=
new
staticObject
(
theWorld
,
new
Vector2
(
0
,
0
),
new
Vector2
(
600
,
20
),
0
);
dynBlock
=
new
dynamicObject
(
theWorld
,
new
Vector2
(
200
,
100
),
new
Vector2
(
200
,
20
),
0
f
,
theWorld
.
RECTANGLE
,
Color
.
ORANGE
,
density
,
friction
,
restitution
);
wall
=
new
staticObject
(
theWorld
,
new
Vector2
(
0
,
0
),
new
Vector2
(
10
,
600
),
0
);
statBall
=
new
staticObject
(
theWorld
,
new
Vector2
(
100
,
500
),
new
Vector2
(
40
,
0
),
theWorld
.
CIRCLE
,
Color
.
RED
);
statGround
=
new
staticObject
(
theWorld
,
new
Vector2
(
0
,
0
),
new
Vector2
(
600
,
20
),
theWorld
.
RECTANGLE
,
Color
.
GREEN
);
}
}
...
@@ -44,7 +50,7 @@ public class MainGameScreen extends ScreenTemplate{
...
@@ -44,7 +50,7 @@ public class MainGameScreen extends ScreenTemplate{
public
void
update
(
float
delta
)
public
void
update
(
float
delta
)
{
{
theWorld
.
update
();
theWorld
.
update
();
circle1
.
objBody
.
applyTorque
(
torque
,
true
);
}
}
public
void
render_me
(
float
delta
)
public
void
render_me
(
float
delta
)
...
@@ -61,9 +67,10 @@ public class MainGameScreen extends ScreenTemplate{
...
@@ -61,9 +67,10 @@ public class MainGameScreen extends ScreenTemplate{
// render our sprite using the sprite batch set up in the game core
// render our sprite using the sprite batch set up in the game core
circle1
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
dynCircle
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
ground
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
dynBlock
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
wall
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
statGround
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
statBall
.
Render
(
theWorld
,
game_handle
.
shapeRenderer
);
game_handle
.
batch
.
end
();
game_handle
.
batch
.
end
();
}
}
...
...
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