Friday, 3 February 2012

JBox2D with JavaFX 2.0 Tutorial: Attaching an image to body

In Write your first JBox2D with JavaFX 2 program posts we have seen how to create a ball and its GUI representation using JavaFX Circle shape. We can also use images for GUI representation. Following code snippet shows how to attach an image to the body.

import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;

/**
 *
 * @author dilip
 */
public class Ball extends Parent{

    
    //X and Y position of the ball in JBox2D world
    private float posX;
    private float posY;
    
    //Ball radius in pixels
    private int radius;
    
    public Ball(float posX, float posY, int radius){
        this.posX = posX;
        this.posY = posY;
        this.radius = radius;
        create();
    }

    
    private void create(){
        
        /**
         * Set ball position on JavaFX scene. We need to convert JBox2D coordinates 
         * to JavaFX coordinates which are in pixels.
         */
        this.setLayoutX(Utils.toPixelPosX(posX)); 
        this.setLayoutY(Utils.toPixelPosY(posY));
        
        //Create an JBox2D body defination for ball.
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;;
        bd.position.set(posX, posY);
        
        CircleShape cs = new CircleShape();
        cs.m_radius = radius * 0.1f;  //We need to convert radius to JBox2D equivalent
        
        // Create a fixture for ball
        FixtureDef fd = new FixtureDef();
        fd.shape = cs;
        fd.density = 0.5f;
        fd.friction = 0.3f;        
        fd.restitution = 0.5f;

        /**
        * Virtual invisible JBox2D body of ball. Bodies have velocity and position. 
        * Forces, torques, and impulses can be applied to these bodies.
        */
        Body body = Utils.world.createBody(bd);
        body.createFixture(fd);
        this.setUserData(body);

        //Attach an image.
        ImageView iv = new ImageView();
        iv.setSmooth(true);
        iv.setImage(new Image(Ball.class.getResourceAsStream("images/ball.png")));
        getChildren().add(iv);
    }
}

8 comments:

  1. This isn't working for me, I'm always getting compiler errors on the last line:
    "getChildren().add(iv);"

    ReplyDelete
    Replies
    1. nevermind, I just forgot to extend parent

      Delete
  2. Herbs Solutions By Nature offer Herbal Supplement for Health and Skin Diseases. Natural Herbal Treatment really does help you recover from your health condition.

    ReplyDelete
  3. Herbs Solutions by Nature offer Herbal Supplement for Health and Skin Diseases. Natural Herbal Treatment really does help you recover from your health condition.

    ReplyDelete
  4. The exact cause of this problem is yet to be determined. The disease affects 1% of men mostly middle-aged but can occur in younger individuals as well. Natural Remedies for Peyronie’s Disease manufactures by Herbs Solutions by Nature is available for its natural treatment. These remedies include herbal aliment which works deeply without any side effects.

    ReplyDelete
  5. This avocado is probably a good Herbal Supplement for Granuloma Annulare because that works on granuloma annulare which is packed with vitamin E. You can also take a daily tablespoon of apple cider vinegar verbally or apply it to your skin as they are a Granuloma Annulare Natural Treatment. Coconut oil absorbs into the skin and penetrates very effectively - Granuloma Annulare Coconut Oil will help relieve any itching and inflammation from your rash.

    ReplyDelete
  6. Natural Remedy for Achalasia includes some most effective herbs and natural remedies to treat this disease. Herbal Supplement can help relieve the symptoms.

    ReplyDelete

  7. A hydrocele is a fluid-filled sac that surrounds a testicle and produces enlargement in the scrotum. The majority of hydrocele cases are caused by birth, and newborn kids are at a greater risk of developing hydrocele. It mostly affects males rather than women. They may strike men of any age. So Organic Herbal Supplements are the best option to cure from that disease.

    ReplyDelete