Physics engine beginner

3 views
Skip to first unread message

Mike Baranczak

unread,
Mar 27, 2011, 8:59:42 PM3/27/11
to the-rend...@googlegroups.com
I'm trying to get started with the physics engine, and can't get anywhere. I'm creating a square object, which should start in the middle of the play area, then fall down. Instead, it gets placed in the upper left corner, and never moves. What am I doing wrong?

        /** Run the game as soon as all resources are ready.
         */
        run: function() {
            var gravity = Vector2D.create(0, 650);
            this.simulation = Simulation.create("simulation", this.fieldBox, gravity);
            this.simulation.setIntegrations(3);
            this.renderContext.add(this.simulation);
            Engine.getDefaultContext().add(this.renderContext)
            this.cModel = SpatialGrid.create(this.fieldWidth, this.fieldHeight, 8);
            this.addThing(Box.create(), 100, 100)
        },
        addThing: function(thing, x, y) {
            var p = Point2D.create(x, y)
            thing.setSimulation(this.simulation)
            this.renderContext.add(thing)
            thing.simulate()
            thing.setPosition(p)
            p.destroy()
        },

And the Box class:

Engine.initObject("Box", "PhysicsActor", function() {

    var Box = PhysicsActor.extend({

        constructor: function() {
            this.base("Box")
            this.setBoundingBox(0, 0, 30, 30)
            var body = BoxBodyComponent.create('physics', this.getBoundingBox().getDims())
            body.setDensity(2)
            body.setFriction(0)
            this.add(body)
            body.setRenderComponent(SpriteComponent.create("draw"))
        },

        setPosition: function(point) {
            this.base(point)
            this.getComponent("physics").setPosition(point)
        },

        update: function(renderContext, time) {
            //console.log('update: '+time);
            this.base(renderContext, time)
            renderContext.setFillStyle("#ff0000")
            renderContext.drawFilledRectangle(this.getBoundingBox())
        }

    }, { // Static

        getClassName: function() {
            return "Box"
        }

    })

    return Box
})


mwebi

unread,
Mar 28, 2011, 7:42:29 AM3/28/11
to The Render Engine
Well one thing that cought my eye is:
thing.simulate()
thing.setPosition(p)

That won't work because you can't set a position while a object is
being simulated. At least in v 1.5.3
For the rest I don't have time atm too look closer at the code but I
will try to find some time.

Is there any error output in the console?

mwebi

unread,
Mar 28, 2011, 11:26:06 AM3/28/11
to The Render Engine
also try calling this.setupWorld(); after
"this.simulation.setIntegrations(3);"

Mike Baranczak

unread,
Mar 28, 2011, 11:45:49 AM3/28/11
to the-rend...@googlegroups.com
Actually, I also tried putting setPosition first, and I had the same results.

If you can't use setPosition while it's being simulated, then what's the correct way to move it? Ultimately, I want to have a user-controllable object that can walk left or right across a platform.

> --
> You received this message because you are subscribed to "The Render Engine" group.
> To post to this group, send email to the-rend...@googlegroups.com
> To unsubscribe from this group, send email to
> the-render-eng...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/the-render-engine?hl=en

mwebi

unread,
Mar 28, 2011, 4:29:53 PM3/28/11
to The Render Engine
Well normaly if you use a physic engine you dont set the position to
new coordinates.
But instead you add a force or impulse to the object you want to move.
You would do this with physicsobject.applyForce(forceVector,
position).
That function is explained in the documentation

However if you really want to set the position you can: stop the
simulation, call setposition(), start the simulation.
However i think the applyForce method is the better way
Reply all
Reply to author
Forward
0 new messages