Commit 3854d0df authored by a.guest's avatar a.guest

objects update

parent 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();
}
}
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 / (180f))* 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)*180f;
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,1f,1f, 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();
}
}
}
package game_objects;
package physics;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
......@@ -6,13 +6,17 @@ 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 physics.world_simulation;
public class gameObject {
// Texture and Sprite are used to render the object to the screen
public Texture texture;
public Sprite sprite;
public gameObject()
{
......
package game_objects;
package physics;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
......@@ -11,13 +11,14 @@ 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 staticObject extends gameObject{
// Used to create the object in the simulation world
Body objBody;
Fixture objFixture;
Vector2 objSize;
int shape;
Vector2 simSize;
Vector2 renderSize;
Color colour;
public staticObject()
......@@ -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;
PolygonShape objShape;
objSize = new Vector2(size);
// Create object in simulation
colour = theCol;
// We need a BodyDef to hold all the information
objBodyDef = new BodyDef();
renderSize = new Vector2(size);
simSize = new Vector2(size);
shape = SHAPE;
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.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
objBody.createFixture(objShape,0.0f);
// shape no longer needed so can be disposed of
objShape.dispose();
}
}
......@@ -64,12 +88,25 @@ public class staticObject extends gameObject{
{
Vector2 mySimPos = new Vector2(objBody.getPosition().x,objBody.getPosition().y);
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.rect(myRenderPos.x, myRenderPos.y,objSize.x,objSize.y);
shapeRend.setColor(Color.WHITE);
shapeRend.setColor(colour);
shapeRend.rect(myRenderPos.x, myRenderPos.y,renderSize.x,renderSize.y);
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();
}
}
......
......@@ -24,6 +24,9 @@ public class world_simulation {
final boolean DEBUG = false;
public final int RECTANGLE = 0;
public final int CIRCLE = 1;
Vector2 RenderMin;
Vector2 RenderMax;
Vector2 SimMin;
......
......@@ -6,16 +6,20 @@ import com.badlogic.gdx.graphics.GL20;
import com.mygdx.game.*;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import game_objects.*;
import physics.world_simulation;
import physics.*;
public class MainGameScreen extends ScreenTemplate{
world_simulation theWorld;
dynamicObject circle1;
staticObject ground;
staticObject wall;
dynamicObject dynCircle;
dynamicObject dynBlock;
staticObject statGround;
staticObject statBall;
float density = 0.5f;
float friction = 0.4f;
float restitution = 0.6f;
float torque = 1.0f;
......@@ -27,9 +31,11 @@ public class MainGameScreen extends ScreenTemplate{
Gdx.input.setInputProcessor(this);
circle1 = new dynamicObject(theWorld, new Vector2(300,300), 30, 0);
ground = new staticObject(theWorld,new Vector2(0,0), new Vector2(600,20),0);
wall = new staticObject(theWorld, new Vector2(0,0), new Vector2(10,600), 0);
dynCircle = new dynamicObject(theWorld, new Vector2(300,300), new Vector2(50,0),45f, theWorld.CIRCLE, Color.PURPLE, density, friction, restitution);
dynBlock = new dynamicObject(theWorld, new Vector2(200,100), new Vector2(200,20),0f, theWorld.RECTANGLE, Color.ORANGE, density, friction, restitution);
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{
public void update(float delta)
{
theWorld.update();
circle1.objBody.applyTorque(torque, true);
}
public void render_me(float delta)
......@@ -61,9 +67,10 @@ public class MainGameScreen extends ScreenTemplate{
// render our sprite using the sprite batch set up in the game core
circle1.Render(theWorld, game_handle.shapeRenderer);
ground.Render(theWorld, game_handle.shapeRenderer);
wall.Render(theWorld, game_handle.shapeRenderer);
dynCircle.Render(theWorld, game_handle.shapeRenderer);
dynBlock.Render(theWorld, game_handle.shapeRenderer);
statGround.Render(theWorld, game_handle.shapeRenderer);
statBall.Render(theWorld, game_handle.shapeRenderer);
game_handle.batch.end();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment