ApplyForce for shooting and Rotation based on another fsbody

Topics: Developer Forum
Jul 19, 2012 at 10:48 AM
Edited Jul 19, 2012 at 10:52 AM

Hi all! I'm currently developing a game using Faseer to incorporate physics in a 2d game. I have a game character that throws a stone according to the angle that the arrow (up/down) is rotated up till by the player of the game. This stone would thus fly from left to right side of the scene, usually up in the air then pulled down by gravity.

I have the following piece of code that is currently working but not according to what I'm hoping for:


		var localDirection = new FVector2(0,1);
		var wd = body.GetWorldVector(localDirection);
		wd *= 10;


Right now, I'm able to create the stone upon a mouse click or touch on mobile and the stone will be launched, but it merely drop off from the point it was instantiated after the click, but not fly according to the direction.

Secondly, I did set the stonewall.rotation to the arrow.rotation on instantiation of my stonewall sprite using orthello framework, so it would be facing the right angle, but I'm not sure if this would be the right way when I incorporate faseer, as I'm not sure how to get the reference to my Arrow fsBody in c#. Maybe someone could guide me on this?

Am I using the wrong method, as in "ApplyForce"? A little advice or direction to where I could look at under the documentation would be great! thank you in advance!

Jul 19, 2012 at 2:54 PM

Is you arrow pointing up when created? Your code would imply that the tip is at (0,1) in local coordinates.

Perhaps the force you are giving (10N) is too small. How heavy is the stone? I read in the Orthello guide that it uses 1 unit=1 pixel. Farseer uses the mks system so 1 unit=1 meter or kg or s. That would make for very large and heavy bodies if the scale is not adjusted.

Jul 19, 2012 at 3:27 PM
Edited Jul 19, 2012 at 3:40 PM

Hi Jerry, thanks for your reply, I've learnt a lot from your answers in other posts. Yes, my arrow is pointing diagonally upwards to the right. You are right about the mass of my stone object. I changed the force to 100N and the mass of my stone remained at 1. When the force is at 100N, the ball will fly now according to the direction i'm pointing to, but once it hit onto something, it seems to have profuse vibration and it jumps here and there before coming to a complete stop. The jumps consists of rebounce that is so strong that at times the stone will be bounced off the scene when it touches any other fsbody objects or the ground too. It seems like it is not reacting naturally to the gravitational pull, like a throw of a basketball.

Do you have any advice? Is it about the mass and force, friction or density? they are all at default values. I have trial and error a little, but does not seems to help :( Thanks a lot! Sorry, that I'm asking so much.

Jul 19, 2012 at 8:01 PM
Edited Jul 19, 2012 at 8:05 PM


As a first step I'd check the actual mass just to be sure. Mass of 1 is just a too nice round value :) Just put a breakpoint after you create the body and examine the member variables of the Body class.

Secondly you need to play with the parameters (Farseer has a lot of them). Some vibration is "normal" for a physics engine since it does not provide an exact solution to the equations of motion but instead converges to a solution with each step (and sub-step iterations). It's important that the instability is imperceptible. You are lucky you don't deal with complex joints - I can tell you it's a world of pain sometimes :)

Important variables:

Restitution - this affects the bounciness of the object. 1 means perfectly elastic collision whereas 0 means all energy is lost in the collision.

Friction - you can up the friction to make the object slide less.

Angular&Linear Damping - increasing those will definitely eliminate small oscillations (sort of like simulating viscosity only linearly). I use them always.

Farseer has some global settings too (FarseerSettings) - it's usually not recommended to change those but might be necessary:

Velocity/position iterations - increasing those (in your case more the velocity iterations) will make the solver work harder each step to converge to the solution. Results in much higher CPU use so unfortunately can't go too high.

There is a setting for max velocity - again a problem of scale - if you're shooting with a velocity too high (I don't remember the exact value but let's say above 400 km/h give or take) the engine won't simulate it properly. This value can be changed safely if the scale cannot be otherwise altered but the engine is fine-tuned to work with those values so it's best kept as a last resort.

Decreasing the time-step - will make the simulation more precise but at the cost of CPU. If the time-step is too big the simulation will be unstable. For example if I ask the engine to simulate 5s of the time per step instead of 15ms (at 60Hz which is a "good" update frequency to aim for same as 60 fps for graphics) the error of integration will be so high that it will look completely unrealistic. This is a common problem in network games where there is "lag" and it's a tough issue to solve.

Another problem I've often had myself is having the sprite positioned incorrectly with regards to the actual physics so everything seems to be working but collisions look very weird. I have no experience with the framework you are using but Farseer has a DebugViewXNA so in XNA you can actually draw the physics shapes on top of the graphics to quickly see if there are issues.

Finally Farseer has a special setting for bullet type bodies (IsBullet=true) which could be useful (it has to do with continuous collision detection and fast moving objects).

HTH but in the end of the day trial and error is the best weapon.

Jul 20, 2012 at 5:22 AM

Thank you Jerry, for taking your time to answer my query. I'll continue to trial and error with the additional parameters you have mentioned. I will post back my results, as soon as I get it working properly, I really appreciate your kindness in sharing your knowledge! :) have a great weekend!

Jul 20, 2012 at 6:45 AM

Hi Jerry, thank you so much for your advice and explanation on the different parameters. I have finally managed to solve my issue. However, it wasn't really an issue with the parameter settings. It was actually the way I coded my application of force. The initial code I had within my update() function is continuously applying force to my stone ball object:

	void Update () {
			var localDirection = new FVector2(0,1);
			var wd = body.GetWorldVector(localDirection);
			wd *= 100;


That causes all the weird jumps and strong re-bounces. The new edited codes that work nicely is as follow:

	void Update () {
		if (count>0){
			var localDirection = new FVector2(0,1);
			var wd = body.GetWorldVector(localDirection);
			wd *= 50;
		count -= 1;

Of course, I believe this is not the best practice, but it worked nicely for me. I have an integer variable, "count" which is set at 1 on instantiation. Once, the force is applied to my object, the count would be reduced by 1, which would stop the force to be applied to our object again as the update() function is called during every frame. I have also changed ApplyForce to ApplyLinearImplulse which made the throw more natural to my needs, just like a basketball throw.


Thanks for your help Jerry!