Need help with a few things(classes, debug view, onCollision)

Mar 14, 2009 at 12:11 AM
Edited Mar 14, 2009 at 3:00 AM
Hello, first off let me say
I am extremely and unimaginably thankful for you guys putting such amazing effort into a wonderful program
and letting people like myself have access to it.
Also thank you for being so responsive to the community,
i can tell by these discussions that you guys are extremely helpful
and around quite often to help
/apreciation

Anyway ive been using farseer for quite some time now and ive been able to do so much by just following your guide,
but there is an extreme lack of tutorials around so there are some things i cant figure out

1. Whenever i put bodies and geometries inside of a class and add them to my physics simulator, they do not respond to anything and do not function.
Is it not possible to have bodies or geometries inside of a class?     *RESOLVED*(wasnt updating the physics engine properly due to a type-o *face palm)

2. I have no idea how to go into debug view, ive seen other peoples code with something like PhysicsSImulatorView
but i cant find this method and i dont know how to enable it can someone help me out?

3. I want to have my character jump, so i apply an Impulse when a certain button is pressed, but of course if you hold it down the person goes flying and infinitely goes up, and when i put in a simple delay its obviously not gonna work in every situation as its only possible to guess set a time.
So what i want to be able to happen is for it to check if there is a collision occuring (onCollision?) and if there is let the player jump.
Also this allows me to add a time limit to how long they can hold it so they can do a small jump or big jump.       *RESOLVED (read rogueCommanders explanation its great)

Is there a better way to do jumping? and can i do this with the onCollision?
Also in the on collision what should i put in to fill the argument "contact list"

4. Can i not modify joint overlolads in the update method? do i have to delete the old joint and replace it to apply any changes? Also when i even try to make a joint/spring in the update method it gives me an error in the velocity or angular velocity.


Also texture to vertices is the best thing ever(period)
Mar 14, 2009 at 1:52 AM
Edited Mar 14, 2009 at 2:14 AM
Welcome to the community and thank you for the praise! while i am not a member of the development team, i try to hang around here a lot and generally answer questions people have. Now about those questions:
1. you resolved this while i was posting...
2. What you have to do is raid the samples and extract the PhysicsSimulatorView (and the 4 files in the DrawingSystem folder) and move them into your namespace (assuming you have XNA otherwise there is no debug view sorry...) then set it up like the samples do (in the DemoX.cs files), although you should play around with the view's settings to see what you want in your debug view.
3. Well there is a few ways to do this, but hte easiest is to check Y velocity and if it low (or non-existent) then allow jumping (assuming that you are allowed to jump, see further on). What you would also want to do is rig your OnCollision so that you detect if you are hitting a floor geom and reset a JumpsLeft variable, so you would have something like this pseudo code:

... (somewhere where your jump code would go)
if (Body.Velocity.Y is above -0.05 or Body.Velocity.Y is below 0.05) then
    if (this.JumpsLeft is above 0)
        body.ApplyImpulse(JumpForce)
        this.JumpsLeft -= 1
...
...(other code inside this class)
void OnCollision(Geom geom1, Geom Geom2, ContactList list) //take note, because i think that this is how you form the code for an OnCollision event!
    if (geom1.Tag.IsPlayer)
        if (geom2.Tag.IsFloor)
            geom1.Tag.JumpsLeft = geom1.Tag.MaxJumps
    if (geom1.Tag.IsFloor)
        if (geom2.Tag.IsPlayer)
            geom2.Tag.JumpsLeft = geom2.Tag.MaxJumps
...
4.You should be able to set the parameters of a jointat any time. Not sure about this tho. Also, can you clarify about your error when trying to make joints?
Mar 14, 2009 at 2:24 AM
Edited Mar 14, 2009 at 2:25 AM
Thank YOU sir for replying
The idea for checking velocity in the y is great i think i will implement it rather than the collision checker as i forsee quite a bit of problems using that.

But thank you for clarifying how to do oncollision checks, im not quite sure what that tag is,
is it basically wher eyou can plug in whatever variable you like (such as jumpsleft)
if so that is quite handy and i think i will use that in the future.

Again, thanks for the speedy, and very helpful, reply

One other thing, if you dont mind,
i am using inkscape to draw maps then export them as png, texture to vertices, etc.
But i plan to chop it up since the maps are very large and there are limits, my question is
If i were to chop up the map into sections(each being the screens width and height) how would i position them on the screen?
I know this isnt a farseer question neccesarily but i would you could help.
I ask this because if theres a limit to how large the picture itself can be wouldnt there be a limit to how far you can define a vector 2.
Say (2000, 500) if that were a pictures dimension it wouldnt be allowed, but if i positioned the sections to be next to eachother, and one went that far would it not allow me to define that position?
Im not drawing them or adding them to the simulation unless they are onscreen by the way so its strictly whether or not i can define a vector 2 that big?
Mar 14, 2009 at 2:47 AM
The tag supports sticking in an arbitrary object so you could stick in a generic parent class or an info class that offers information on object properties and then do something like ObjectInfo Info1 = Geom1.Tag as ObjectInfo (i think that will work dont take my word for it though) and extract data from it.
As i understand, the Vector2 class supports extremely large vectors, all the way up to Float.PositiveInfinity. So you shouldn't have a problem there. You are very welcome by the way, any questions you have please ask!
Mar 14, 2009 at 2:58 AM
Edited Mar 14, 2009 at 3:03 AM
thanks again man
although i dont feel comfortable, only positive infinity?
Well I guess ill try

O yeah and i just did your code for jumping, i simply made the condition if
(body.linearvelocity.y = 0)
{player.Jump(player.jump);}
as at the peak of an arc the velocity is near zero so the character was able to double jump, having it at 0 seems to work perfect though.
Im just a bit worried little bumps or hiccups make cause it to not allow the jump, if that happens i will make it a bit higher but the double/tripple/quadruple jumps are a tad bit more anoying.

Once more thanks for all your help
you would know how to implement a camera into the farseer world would you haha, i have a simple chase camera but since it is controlled by changing positions relative to the camera, it meens that to zoom i wuld have to scale.
And scaling dont fly in farseer.
Is there a 2d camera that just controls the actual viewport and not the objects? It would be amazingly helpful to have that type instead of the crappy one i have now as it feels like im on a ball and chain, which sucks.
Also, yes i am on good ole' xna

Sorry for bombarding you with questions, i know you said there welcome but i feel like im burdening you a bit.
Mar 14, 2009 at 6:52 AM
You're not burdening me at all! Actually vectors can accept any negative value up to float.NegativeInfinity as well (should have mentioned that). The whole JumpsLeft thing was to ensure that you didnt have double/triple/etc. jumps so that would work if you wanted. You have a working camera? wow! i had one that could do some nifty things (or it could if i programmed it to) that involved a rendertarget bigger than the screen size (so zooming/rotating etc. would be easy) but it doesn't work under windows and slows the xbox down to a crawl. I must be doing something wrong, but... Anyway, i have plans to milement a camera like yours (but don't know what im gonna do about the zooming.... ill have to think about it). Again, you're questions are welcome, always glad to help.
Mar 14, 2009 at 2:03 PM
Edited Mar 14, 2009 at 2:05 PM
haha thanks, i was just joking about the positive infinity but i do feel a bit more comfortable haha

but yes this camera is quite simple to use, theres only one thing thats a bit of a burden.
You have to convert the position for everything you draw...among other problems like no zoom, BUT
what ive done is made the draw method inside of each class to calculate it for me, which makes it extremely easy.
Also this camera has code for whether or not things are on screen or not so i just added a line of code inside the draw method to check if things are on screen then draw.
Its quite a great little tool, a bit to limiting though.

Im planning to add a method where if something is offscreen to remove it from the simulator to optimize but i imagine that would cause alot of problems, i think ill make a new method in the camera class specific to physicssim removal, ill just make it so if its extremly far off screen remove it and make a variable to control it.

Its a shame yours doesnt work well as having zoom and rotation would make life so much easier, im sure its easy without farseer to get this cam to zoom (make it scale everything) but since in farseer your bodies and geometries reference the height not the apparentheight you cant do it, i tried to create apparent height but it wouldnt do texture to vertices as the described area was larger than the image, plus it would have to change the bodies and geometries way to often and probaly kill the computer indefinatley.

Also, quick question, im trying to do a diving board for a test, should i use a revolute joint, and fixed or not? and can i get my body to ignore rotation, or can i change how hard it is to rotate it?

Thanks for all your help again
Mar 14, 2009 at 10:32 PM
Well i think i'm doing something wrong with the render target because other people use render targets left and right and they don't have a problem. What i was planning to do was use the AABB of the geom to calculate wether i need to draw my textures (do an intersection test between a camera AABB and each geoms AABB). It just occured to me: if you create a matrix like this:
Matrix tempMatrix = Matrix.CreateTranslationY(camera.PosY) * Matrix.CreateTranslationX(camera.PosX);
and if you added a Matrix.CreateRotationZ(camera.Rotation) on the start you could have rotation (it needs to be on the start or everything gets screwed up) (although it just plain might not work
and if you added a Matrix.CreateScale(camera.ZoomLevel) to the start then it would provide zooming (but if you want rotation you would have to place it after that)
then go:
spriteBatch.Begin(SpriteBlendMode.None /* or AlphaBlend if you need that */, SpriteSortMode.Deferred /* this is the default mode */, SaveStateMode.None /* again, the default mode */, tempMatrix /* it will take this matrix and auto-transform/scale/rotate any sprites drawn in this call */);
[the rest of your drawing code]
About your diving board, check out the AngularSpring class it should do just what you need. You're welcome!
Mar 15, 2009 at 1:30 AM
Edited Mar 15, 2009 at 2:00 AM
well, considering im completely unaware of how matrices work or how to use them i have seen them before, what your saying sounds correct and definatley like it would work.
for the .createscale* is that to create the effect of scale(zoom) or will it literaly scale the images. Because if it would literaly scale the images in the spritebatch the geometries and bodies would not be able to work correctly.

o and yes i just implement a angle spring and revolute joint and it works, but now it just keeps spinning and it wont stay in a fixed position, it stays in the revolute pin pint but it wont stay at a certaint angle even thought i set the target angle.
Mar 15, 2009 at 2:39 AM
As i understand it the CreateScale matrix function ought to provide an effect similar to zooming into an image (with the pixelation effects asscociated with it). Odd; the simple samples should have an angle limit joint in use (its one of the middle ones i think) take a look at that.
Mar 15, 2009 at 4:50 AM
also
the only way im able to make springs (and joints)

SpringFactory spring;
spring = SpringFactory.Instance:
spring.CreateAngleSpring(blah blah);

Is this correct or is there a better way as
the problem here is if i wanna do something like

spring.TargetAngle = 0;
or
spring.maxTorque = 0;

it wont allow it for whatever reason
it wont allow me to use spring again everything that has to do with the spring(joint) remains inside of the one statement!

As you can imagine this is a great annoyance and quite confusing.
Because i can do
spring.CreateAngleSpring(blah blah).Target angle = 0;
but it wont allow me to add any more parameters to the end, like max torque or anything described in the farseer manual.

I think i must be doing something wrong but this is the only way it will let me make a spring or joint.
Mar 15, 2009 at 4:59 AM
So you're saying it won't let you change any variables after creation? i don't know why.... You'll have to wait for genbox (if thats the case).
Mar 15, 2009 at 5:05 AM
yes, basically i cannot edit or access any of the information from the spring(joint) i just created, its terribly frustrating.
Mar 15, 2009 at 7:27 AM
Edited Mar 15, 2009 at 9:21 AM
What kind of error do you get when you try to edit a property?
also, ive implemented zoom/rotation via the method above, assuming you (or someone else like Shpaidaman) has implemented a similar method, how did they fix the camera zooming/rotating around the top left as opposed to the center?
Mar 15, 2009 at 5:23 PM
well after making the spring i go to write something like

spring.targetangle = 180;

or 

spring.body1.targetangle = 180;

but after write spring. it only gives me (createanglespring, create linear spring, toString, etc) useless methods as they obviously do not have anything to do with the presvious spring.

Also get it to zoom around the center, im not sure.
You could get it to zoom about the origin of whatever you get it, im not sure as i have not had success implementing a camera like that.
Is there any way to access where it zooms to?
Or perhaps is there a way to get the screens origin to be the center considering its scaling the screen, if you can change the screens origin centered then it would work.
Meh i dunno im not to good with c#
Mar 16, 2009 at 11:12 PM
Edited Mar 16, 2009 at 11:13 PM
What you need to do is:
(spring/joint type here) item = (factory type here).Instance.Create(joint/spring type here(...);
item.ParameterYouWantToChange = ValueYouWant;

so a linear spring will be something like this:
LinearSpring spring = SpringFactory.Instance.CreateLinearSpring(...);
spring.RestLength = 20;


Basically, you need to stare the return value from the factory into a local variable and edit its properties that way.
With the camera, subtracting half of the screen size from the translation matrix creation seems to sort of fix it.
Just keep practicing! i used to barely be able to write out simple console programs!
Mar 17, 2009 at 1:39 AM
i tried to add
LinearSpring spring = SpringFactory.Instance.CreateLinearSpring(...);
but it doesnt recognize linearspring
or angle spring
or any joint type
i have all the using statements and everything setup but it cant recognize the joints or springs types. Only spring and joint factories
It can create them and they work in the simulator though
Mar 17, 2009 at 3:47 AM
you need to have these two using statements in your usings:
using FarseerGames.FarseerPhysics.Dynamics.Joints;
using FarseerGames.FarseerPhysics.Dynamics.Springs;
After you do that it should work.
Mar 17, 2009 at 5:13 AM
Edited Mar 17, 2009 at 6:07 AM
o jeez, thanks man i feel dumb for not having those
they just didnt come up in the list of using statements when i entered them so ya no
well i look like a fool
because i am one
thanks alot though i would feel stupider if i never asked the question

edit:
Although that is working now the thing still wont act like a diving board, it just keeps swinging
this is my code currently

            body = Body;
            bodyImage = BodyImage;
            anchor1 = new Vector2(body.Position.X + (bodyImage.Width/2), body.Position.Y);
            FixedRevoluteJoint joint = JointFactory.Instance.CreateFixedRevoluteJoint
                (Farseer.physics, body, anchor1);
            FixedAngleSpring spring = SpringFactory.Instance.CreateFixedAngleSpring
                (body, 1000000, 5000);
            spring.TargetAngle = 180;
            spring.MaxTorque = 3000;

ive tried it with and without the target angle and max torque, with a low and high dampn constant and spring constant, what am i doing wrong?
Mar 17, 2009 at 6:16 AM
You're not stupid! in fact, i think that they went a tad overboard with the namespace organization (just having Factories, Dynamics, Collisions, and Constraints (for joints n springs) would be fine with me).
About your problem, lemme play around with some of the demos that come with farseer and see if i can get it working.