List of usage examples for com.badlogic.gdx.physics.box2d Body setType
public void setType(BodyType type)
From source file:org.box2d.box2d.Box2dWorldProxy.java
License:Open Source License
@Kroll.method public BodyProxy addBody(Object[] args) { synchronized (this) { SpriteProxy viewproxy = (SpriteProxy) args[0]; HashMap props = (HashMap) args[1]; Log.d(LCAT, "printing viewproxy: " + viewproxy); Log.d(LCAT, "printing props: " + props); /*//ww w. j a v a 2s .co m Thread t = new Thread("Thread1") { @Override public void run() { // some code #2 runOnUiThread(new Runnable() { public void run() { // some code #3 (that needs to be ran in UI thread) addBodyToView(viewproxy); } }); } }; t.start(); t.join(); */ /* Activity_Name.this.runOnUiThread(new Runnable() { @Override public void run() { // your stuff to update the UI } }); */ addBodyToView(viewproxy); PlatinoSprite ti_physical_view = viewproxy.getSprite(); /* // TiUIView ti_physical_view = viewproxy.getOrCreateView(); Log.d(LCAT, "printing ti_physical_view: " + ti_physical_view); View native_physical_view = ti_physical_view.getNativeView(); Log.d(LCAT, "printing native_physical_view: " + native_physical_view); int physical_width = native_physical_view.getWidth(); int physical_height = native_physical_view.getHeight(); int physical_left = native_physical_view.getLeft(); int physical_bottom = native_physical_view.getBottom(); int physical_center_x = (physical_width/2)+physical_left; int physical_center_y = (physical_height/2)+physical_bottom; */ float physical_width = ti_physical_view.getWidth(); float physical_height = ti_physical_view.getHeight(); float physical_center_x = ti_physical_view.getCenterX(); float physical_center_y = ti_physical_view.getCenterY(); float box_width = physical_width / PTM_RATIO / 2.0f; float box_height = physical_height / PTM_RATIO / 2.0f; // Log.d(LCAT, "printing worldView: " + worldView); // Log.d(LCAT, "printing physical_left: " + physical_left); // Log.d(LCAT, "printing physical_bottom: " + physical_bottom); Log.d(LCAT, "printing box_width: " + box_width); Log.d(LCAT, "printing box_height: " + box_height); // int surface_height = theSurface.getOrCreateView().getNativeView().getHeight(); float surface_height = theSurface.getView().getGameViewHeight(); // int surface_height = worldView.getNativeView().getHeight(); // if GameView height is not yet set so we assume default size if (surface_height == 0) { /* NSInteger orientation = [surface.orientation intValue]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { if (UIGraphicsBeginImageContextWithOptions != NULL) { height = 640; } else { height = 320; } } else { if (UIGraphicsBeginImageContextWithOptions != NULL) { height = 960; } else { height = 480; } } */ surface_height = 960.0f; } Log.d(LCAT, "surface_height: " + surface_height); // Define the dynamic body. BodyDef body_def = new BodyDef(); body_def.type = BodyDef.BodyType.DynamicBody; body_def.position = new Vector2(physical_center_x / PTM_RATIO, (surface_height - physical_center_y) / PTM_RATIO); body_def.angle = (float) (java.lang.Math.toRadians(ti_physical_view.getAngle())); if (null == listOfBodies) { listOfBodies = new ArrayList(); } // TiBox2dBodyProxy *bp = nil; box_width = 50; box_height = 50; if (null != theWorld && box_width > 0 && box_height > 0) { // Tell the physics world to create the body Body the_body = theWorld.createBody(body_def); // Define the dynamic body fixture. FixtureDef fixture_def = new FixtureDef(); float radius = 0; Log.d(LCAT, "createBody: radius " + radius); Object radius_object = props.get("radius"); if (null != radius_object) { radius = TiConvert.toFloat(radius_object); } if (radius > 0) { CircleShape circle = new CircleShape(); circle.setRadius(radius / PTM_RATIO); fixture_def.shape = circle; } else { // Define another box shape for our dynamic body. PolygonShape shape = new PolygonShape(); Object[] shape_objects = (Object[]) props.get("shape"); if (null != shape_objects) { int count = shape_objects.length; Vector2[] vertices = new Vector2[count / 2]; int x = 0; for (int c = 0; c < count; c += 2) { vertices[x++] = new Vector2(TiConvert.toFloat(shape_objects[c]) / PTM_RATIO, TiConvert.toFloat(shape_objects[c + 1]) / PTM_RATIO); } shape.set(vertices); } else { shape.setAsBox(box_width, box_height); } fixture_def.shape = shape; } float density = 3.0f; Object density_object = props.get("density"); if (null != density_object) { density = TiConvert.toFloat(density_object); } fixture_def.density = density; float friction = 0.3f; Object friction_object = props.get("friction"); if (null != friction_object) { friction = TiConvert.toFloat(friction_object); } fixture_def.friction = friction; float restitution = 0.5f; Object restitution_object = props.get("restitution"); if (null != restitution_object) { restitution = TiConvert.toFloat(restitution_object); } fixture_def.restitution = restitution; the_body.createFixture(fixture_def); Object body_object = props.get("type"); String body_string = TiConvert.toString(restitution_object, "dynamic"); if (body_string.equals("dynamic")) { the_body.setType(BodyDef.BodyType.DynamicBody); } else if (body_string.equals("static")) { the_body.setType(BodyDef.BodyType.StaticBody); } else if (body_string.equals("kinematic")) { the_body.setType(BodyDef.BodyType.KinematicBody); } BodyProxy body_proxy = new BodyProxy(the_body, viewproxy, theSurface); the_body.setUserData(body_proxy); /* // we abuse the tag property as pointer to the physical body physicalView.tag = (int)body; bp = [[TiBox2dBodyProxy alloc] initWithBody:body viewproxy:viewproxy pageContext:[self executionContext]]; body->SetUserData(bp); [lock unlock]; return bp; */ return body_proxy; } } return null; }
From source file:rosthouse.rosty.systems.PhysicsSystem.java
public <T extends Shape> PhysicsComponent<T> createPhysicsComponent(BodyDef.BodyType type, T shape, Vector2 position, FixtureDef fixture) { fixture.shape = shape;//from w w w. ja v a 2s . c o m bodyDef.position.set(position); Body body = world.createBody(bodyDef); body.setLinearDamping(0.1f); body.setAngularDamping(0.1f); body.setType(type); return new PhysicsComponent<>(shape, body.createFixture(fixture)); }
From source file:rosthouse.rosty.systems.PhysicsSystem.java
public <T extends Shape> SensorComponent<T> createSensorComponent(BodyDef.BodyType type, T shape, Vector2 position, FixtureDef fixture) { fixture.shape = shape;//from ww w .j av a 2 s . c om fixture.isSensor = true; bodyDef.position.set(position); Body body = world.createBody(bodyDef); body.setLinearDamping(0.1f); body.setAngularDamping(0.1f); body.setType(type); return new SensorComponent<>(shape, body.createFixture(fixture)); }