calculating bellow body's angular velocity.

Aug 25, 2009 at 10:26 PM
Edited Aug 25, 2009 at 10:28 PM

in the image bellow there is a character standing on a moving ground that shifts from left to right. Imagine that the character was jumping from the moving ground below it to several other ones that have a different X velocity as they move from left to right. Is there a efficient way to find out the moving body below the characters angular velocity from the characters class? so as an example whenever the character is standing on a different moving block he would be able to tell me what the current velocity of that block is.

if you can't see the bellow image here is the link:  http://img.photobucket.com/albums/v623/MegaManzZ/findingBodyVel.jpg

Photobucket

Aug 26, 2009 at 12:01 AM

If you cast a ray below your character you can get the geom below him. With its geom you can get also its body (Geom.Body), and then you can get the Body's velocity :)

Aug 26, 2009 at 9:53 AM
Edited Aug 26, 2009 at 9:55 AM

aaaah!

Is it possible to link me to an example of how to properly cast a ray and find out what that ray detected. I don't remember reading up on anything like that before.

Aug 26, 2009 at 10:42 AM

No problem. I think rays are new in Farseer 2.1. But after that genbox made some changes in the code making rays more easy to use (in changeset 58100). So for using the code, first you will need:

- Apply the changes from the changeset 58100, by changing the RayHelper.cs file (go to the SourceCode tab above, find the changeset, copy the RayHelper.cs file from the collisions folder, paste it replacing the old one in your Farseer folder... if you don't have a RayHelper.cs file in your Farseer folder, find the "RayTester.cs" which should be the one to replace). Don't forget to backup your project before :)

For this example I assume that you have a small body as feet, called "bodyBottom" (a circle, rectangle, whatever). Here we will check a maximum distance of 100. And has a geom whose tag is the "Hero" class, which is the same class of your character (this is for detecting if the ray collides with the same character). So, the code should be:

 

Vector2 p1 = bodyBottom.Position;
Vector2 p2 = bodyBottom.Position
	+ new Vector2(0, 100);
List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1,
	ref p2,
	physicsSimulator,
	false);
foreach (GeomPointPair gpp in lg)
{
	Hero h = gpp.Geom.Tag as Hero;
	if ((h == null) && (gpp.Geom.Body.Enabled))
	{
		Vector2 linearVelocity = gpp.Geom.Body.LinearVelocity;
		// Do whatever you want with the velocity :)
		// maybe you want to check only the velocity of 
		// the nearest geom, in that case
		// use gpp.Points to check which Geom
		// is closer. Enjoy!
	}
}

 

 

 

Sep 9, 2009 at 6:50 PM

Right now im trying to write code that finds the distance from the character to the nearest ground object bellow it. I tried to insert rayhelper.cs into my project, but immidiatly got the error:

Error 1 The namespace 'FarseerGames.FarseerPhysics.Collisions' already contains a definition for 'GeomPointPair' d:\Projects\Ninjics\Farseer Physics 2.1.1 XNA\Collisions\RayHelper.cs 334 19 FarseerPhysicsXNA
  • Error 1 The namespace 'FarseerGames.FarseerPhysics.Collisions' already contains a definition for 'GeomPointPair' d:\Projects\Ninjics\Farseer Physics 2.1.1 XNA\Collisions\RayHelper.cs 334 19 FarseerPhysicsXNA
So i commented out the bottom most GeomPointPair:
  • //    public struct GeomPointPair
  • //    {
  • //        public Geom Geom;
  • //        public List<Vector2> Points;
  • //    }
Probobly not the best idea, but i am trying to get past this roadblock and continue on my project.
so now i got no errors with rayhelper.cs and tried to see how to use rayhelper. i tried the code in my player class:
  •             Vector2 p1 = body_.Position;
  •             Vector2 p2 = body_.Position + new Vector2(0, -100);
  •             List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, Farseer.Physics, false);
  •             foreach (GeomPointPair gpp in lg)
  •             {
  •             }
but then got the error:
  • Error 7 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) d:\Projects\Ninjics\Ninjics\Ninjics\Entities\PlayerSphere.cs 733 13 Ninjics
Reading this post shows that no doubt i am very confused. If anyone gots the spare time to help me out with getting this simple script to work ill be very happy and thankful :)
Coordinator
Sep 9, 2009 at 7:04 PM

GeomPointPair should only be present in the RayHelper.cs file. (have you removed the old CollisionsHelper.cs file by the way?)

List is inside System.Collections.Generic and that namespace should already be included in the top of the file.

Sep 9, 2009 at 7:47 PM

thanks genbox.. now im getting a problem inside "vertices.cs":

 

Error 1 The name 'CollisionHelper' does not exist in the current context d:\Projects\Ninjics\Farseer Physics 2.1.1 XNA\Collisions\Vertices.cs 1881 25 FarseerPhysicsXNA

  • Error 1 The name 'CollisionHelper' does not exist in the current context d:\Projects\Ninjics\Farseer Physics 2.1.1 XNA\Collisions\Vertices.cs 1881 25 FarseerPhysicsXNA
if i replace vertices.cs with the new one i get a whole new list of errors.

 

Coordinator
Sep 9, 2009 at 11:10 PM

Yeah, I thought I used the RayHelper inside the Vertices class. You have my email right? (Jay might have it) If the send me the project I will fix all compile errors you have and make sure you have a working RayHelper.

Sep 10, 2009 at 2:41 PM
Edited Sep 10, 2009 at 2:49 PM

thanks for the help.. Everything seems to work!!