Storyboard + PhysicsObject + Position

Mar 23, 2010 at 3:00 PM

Hello!

I'm having a few problems when i'm trying to animate/move using a storyboard on my rectangle with a PhysicsObjectBehavior, I just want a brick that moves forward and back repeatedly, and i want my ball to bounce on it :)

I'm also having some trouble using this.. How can i move an object?

_physicsController.PhysicsObjects["ball"].GeometryObject.Position
Coordinator
Mar 23, 2010 at 3:05 PM

Animating a Physics Object with a Storyboard would be hard to pull off.

You need to do the animation programmatically - that is, have a couple of variables for x,y and update the postion like so:

_physicsController.PhysicsObjects["ball"].GeometryObject.Position = new Vector2(x, y);

Mar 23, 2010 at 3:22 PM

ah thanks!

So using storyboards is out of the question?
One question... What is Vector2? Dont seem to have that class :(..

Coordinator
Mar 23, 2010 at 3:29 PM

Vector2 is part of the Farseer classes.

If you type in Vector2 in your code, you can right-click it and select "Resolve" and you should see the Farseer namespaces to include.

 

Mar 23, 2010 at 3:47 PM

Thanks again... But how can you set the position when its read only?

Coordinator
Mar 23, 2010 at 3:59 PM

Looks like that should be BodyObject, not GeometryObject?

_physicsController.PhysicsObjects["ball"].BodyObject.Position = new Vector2(x, y);

Mar 24, 2010 at 2:33 PM

Thanks alot!


Still have some more questions :)

 

I'm using a "ball" as my main charater for the easy use of movement, but what if i would like to have a rectlangle? How easy is it to make it moveable? I'm also using PhysicsApplyForceBehavior for jumping, but i can jump even if i'm in the air :( and i would like to move in the air.. And where can i set a Max velocity for an object? :D

Coordinator
Mar 24, 2010 at 3:12 PM

To try and cap Velocity, you could try playing with LinearDragCoefficient

     _physicsController.PhysicsObjects["ball"].BodyObject.LinearDragCoefficient = 0.3F;

Not sure you could pull of moving a rectangle by applying torque - it would take a good amount of torque to get it rolling, but I suppose it's possible.

The Behaviors for applying force and torque are definitely not "one size fits all." For custom physics effects you will need to either write code or create your own Behaviors. You could try making your own copy of PhysicsApplyForceBehavior and tweaking it as needed.

Mar 25, 2010 at 12:44 PM

Ahh, How can i cap the jumping? so i can only jump if i touch the ground

Coordinator
Mar 25, 2010 at 12:48 PM

You would need to handle this in code (there isn't a predefined Behavior for this).

I would create a boolean flag, _bJumping in your code. Set this to true when the user Jumps. Don't let the user jump again while this is true.

Then, in the PhysicsController's Collision event, When the player collides with the ground, set _bJumping to false. This should allow them to jump again.

I think that would work, anyway :)

Mar 29, 2010 at 3:32 PM

 

	private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                    _keyLeftDown = true;
			_playerBody.ApplyTorque(-30000);
				if(_playerBody.Torque > -30000)
					{
						_playerBody.ApplyTorque(-12000);
					}
					lbl2.Content = _playerBody.Torque.ToString();



I've been trying to limit the torque.. this wont work :(

 

 

 

private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                    _keyLeftDown = true;
_playerBody.ApplyTorque(-30000);
if(_playerBody.Torque > -30000)
{
_playerBody.ApplyTorque(-12000);
}
lbl2.Content = _playerBody.Torque.ToString();

 

 

Coordinator
Mar 29, 2010 at 3:41 PM

In your code it looks like you are always applying -30000 torque.

Why not remove that line so you just have...

if(_playerBody.Torque > -30000)
{
   _playerBody.ApplyTorque(-12000);
}

Mar 30, 2010 at 11:58 AM

Ah... Another problem i have is that when i import an image (Jpg 17000px X 5000px)...The image wont show in design view, only when i run the project.. Any ideas?

 

Coordinator
Mar 30, 2010 at 12:14 PM

Wow, that's a big image. You might want to consider chunking that up into smaller pieces that represent different pieces of the background like a tree, building, whatever you have in there.

It it Blend or Visual Studio that is not showing the image in design view?

Mar 30, 2010 at 3:08 PM

Blend..... I used a smaller image, seems to work now.. However i can't get the Torque limit to work correctly.. The jumping is working kinda good :)

Apr 7, 2010 at 12:46 PM

Hello, I've been trying to make a games.. but it seems when i'm using path as a floor, is there a limit for how many physicsobject you can have in one application? after a while the application wont start.. it just freezes... :(

Coordinator
Apr 7, 2010 at 1:18 PM

I am guessing that you have so many unique objects that the boundary detection algorithm is taking a _long_ time figuring out the shape of all of them. It does this by "tracing" the shape of each element that is a Physics Object.

In any large game, you would want to create re-usable controls (in Silverlight you can use User Controls) that you can use over and over again in your application. The Physics Helper will cache the boundary for the first instance of the user control, and then re-use that when it encounters the same control type.

You can think of these as "tiles" if you want, like in a Mario type game where the same tile graphics are used over and over again.

Another thing you can do to increase startup time is to use the Boundary Cache mechanism. You can read more about this here under "Speeding Up Boundary Detection" :  http://physicshelper.codeplex.com/