Author Topic: Box2D Rope  (Read 11512 times)

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Box2D Rope
« on: 2012-07-16, 02:18:32 PM »
Odd place to ask, but does anyone here have experience with rope physics in either Box2D or your own engine?

I would love to have a SpriteBase class such as RopeClass that uses Box2D physics to act as a rope. I really don't want anyone to necessarily do this for me, but I'd love to know the:

Method for having a custom SpriteBase Class extension that uses Box2D rather than the default physics

Implementing some form of chainlink mechanism so it acts as a rope (I see vague documentation on this)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #1 on: 2012-07-17, 04:43:09 AM »
I haven't looked at this project in a while - can't even remember the name of the 2D physics engine it used (something wrapped for .NET though), but the spider's web was like a rope. It might help: http://sgdk2.enigmadream.com/ben/Spider.zip

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #2 on: 2012-07-17, 10:16:14 AM »
This really helps, and I'd love to have a Sprite class known as HairClass.

I was planning on using the ropes as an option for long hair in my game. I'm nearing completion and have yet to add any long hairstyles.

If I can get a custom sprite class that can attach a link (web) between two sprites, that's independent of the default SpriteBase class (AKA the base SpriteBase class doesn't need to use the physics engine)

I can implement some realistic long hairstyles (Because my current system would make the long hair stiff and unrealistic).

Bravissimo!! AdvanceMath and Box2D use different initializations for SpriteBase than the default library, would I have to create my own SpriteBase class that inherited GeneralRules, or could I create a class that used AdvanceMath and Box2D that inherited SpriteBase? (Such as the UserSprite class on the earlier thread)
« Last Edit: 2012-07-17, 11:27:26 AM by #Sharp »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #3 on: 2012-07-17, 02:36:29 PM »
I would advise against breaking up your sprites into too many pieces until/unless you know that your game is not going to run too slowly as a result.

I don't understand exactly what you mean by AdvanceMath and Box2D using initializations for SpriteBase. AdvanceMath and Box2D know nothing about SpriteBase, right? So how can they use it?

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #4 on: 2012-07-17, 04:09:49 PM »
Forgive my wording, I'm not really good with this kind of speak, but in the SpriteBase class for the alternate physics engine, SpriteBase needs certain other variables to calculate positions. It's modified (greatly?).

I'd like to preserve SpriteBase the way it is in my project (The Default SpriteBase.cs) and simply just have an extension of SpriteBase for the Physics. The same way I had the UserSprite Class.

It shouldn't slow down my game to simply have a sprite known as Scalp attach a link to a sprite known as HairWeight. (Or should it?)

At max I want the hair to be comprised of 5 ropes.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #5 on: 2012-07-18, 07:07:44 AM »
Oh, yes, I would suggest trying to make another class derived from SpriteBase that represents the physics-based sprite. You might also have to change some of the code in LayerBase and possibly other areas because I think other code changed to accommodate the very different architecture of realistic physics versus old-school 2D scrolling game physics. If you have another class derived from SpriteBase, you should be able to select it as the base class while defining a sprite definition. Then you can have other code check if a sprite is of that special sub-class before applying physics rules to it.

If I recall, there is some global physics processing that goes on, and you may have to somehow include only the physics-based sprites in that. But that's part of what could affect performance. If you want realistic physics the way that the Spider project has it, simply implementing the engine would potentially mean that you have to encode all the tiles into physics objects too (that's what it does). You can certainly exclude that part if you don't want your physics-based sprites to interact with tiles, but you will have to go through the code to identify what you need and what you don't. And even having the physics engine active could impact performance.

For something as simple as hair (if that's the only thing you want to apply physics to) I would not incorporate an entire physics engine just for that. You should be able to come up with some simpler modelling mathematics to deal with that. Maybe post on http://stackoverflow.com/ to get some help modelling a few segments of hair without having a whole physics engine involved.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #6 on: 2012-07-18, 11:30:10 AM »
I'm going to do some major testing with the Spider project to see the performance limitations of how many ropes can be active, and how the number of ropes affects FPS with added layers. I've done a good job at keeping the game performing well so far.

(Except for areas of the game with heavy 3D drawing which bug down the FPS -I really can't control this well, but I may have to do some heavy consistency checking for the multiplayer due to the fact that even with FPS Limiting and VSync, different computes will run it at slightly different speeds- )

I won't need tiles, and the only two objects that will need to use the engine are a sprite attached to the player's head, and another sprite that acts as a hair weight. Its for the better.

I planned on using the physics engine later on in the game anyways (Platforms floating in the water)

From your experience with this, was performance ever really a problem? The game seems to run fine as is.

Is it the fact that I'm using an actual physics engine and an artificial "old-school" quasi-physics engine simultaneously?

I was recommended towards something on NeHe that used custom math, but actually, I'll eventually need to implement this physics engine in my game sometime anyways

EDIT


I'm gonna follow the NeHe Tutorial. I don't see a way to modify the RGB of physical links in Box2D. I'll eventually need to import the library in for the physics of objects floating in water, but for now, I'll try to follow this tutorial to the best of my ability.

http://nehe.gamedev.net/tutorial/rope_physics/17006/
« Last Edit: 2012-07-18, 12:44:58 PM by #Sharp »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #7 on: 2012-07-18, 04:27:46 PM »
It's been too long since I played with the physics engine. I think there may have been performance issues when I had to convert the tiles into physics objects. But otherwise I think it took quite a few objects to bog down the engine.

I don't understand how to can have a problem in trying to "modify the RGB of physical links". A physics engine has nothing to do with colors, and isn't that what RGB refers to?

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #8 on: 2012-07-18, 05:04:38 PM »
I wanted to change the color of the links (I wanted to links to be the actual hair), but it's not really much of an issue now.



I figured out another way to do this, that wasn't as complicated. NeHe was right, I just needed to use a "point-mass" system, Have the hair based on a bunch of points.
(A bunch of SpriteBases that moved about like real hair)

The only problem now is, I have the point-mass system working, but my hair looks like a bunch of unconnected dots. It operates like real hair though, it just needs the final touch.

So my question is:

What would be the easiest and most efficient way to draw a line from one sprite to another? (Could I also have RGB control of said line? I want to color the lines as if they were colors of hair)
I was thinking of using GetNearestSpriteIndex and just drawing a line from the sprite that called it to the neighboring sprite. But If I remember, that doesn't actually return a SpriteBase object.

I wanted to create a function that found the nearest sprite of a certain category, and drew a line from the center of this sprite, to the center of that sprite.

Once I figure that out it'll be finished. (Unless someone wants to help me  :D)
« Last Edit: 2012-07-18, 05:08:40 PM by #Sharp »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #9 on: 2012-07-18, 07:13:52 PM »
Why not just make an array of 5 points and draw from the first to the second to the third to the fourth to the fifth? You don't need anything so complex as a sprite, just a coordinate if you want to simply draw lines. You can use the BeginLine and LineTo functions of the Display to draw lines, but I have not experimented to see how to draw from one color to another. I think it's possible, but the function may need some changing. Or it might not. You might be able to call SetColor, then BeginLine, then SetColor, then LineTo, then SetColor, then LineTo, etc. Might work.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #10 on: 2012-07-19, 07:35:18 AM »
Why not just make an array of 5 points and draw from the first to the second to the third to the fourth to the fifth? You don't need anything so complex as a sprite, just a coordinate if you want to simply draw lines.

I'm essentially doing the same thing that I would be doing in a particle mass system- creating a particle, calculating weight and mass of said particle, and calculating how that particle would react to other forces. This way also gives me the option of having hair that interacts with solidity.

The weather effects in my game generate up to 6,000 sprites in a similar way at any given time (particles with mass and weight which consider other forces like wind) and it doesn't affect the framerate much, maybe only cutting it by 1 fps.

I'll try BeginLine and report my results, thanks.

Edit: Trying to understand how indexes work, so how can I use the index of a sprite returned by GetNearestIndex and convert it to the actual sprite that it corresponds to? Target[index] returns a SpriteBase object?
« Last Edit: 2012-07-19, 11:29:32 AM by #Sharp »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #11 on: 2012-07-19, 06:15:34 PM »
Yes, but Target is the parameter, so actually whatever sprite collection you pass in represents target. MyCollection[index] returns a SpriteBase object.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #12 on: 2012-07-19, 07:15:58 PM »
Whoo. I have it working- Somehow. I'll try to post a video soon (Agh, I probably won't be able to log back on until Monday but it seems to run pretty smoothly)

I have 2 classes calling a base of SpriteBase

Particle Emitter (HairSpriteEmitter) The beginning of a hair strand.
Particle (HairSprite) The particles of hair that constitute for the hair strand.

The system I'm using tries not to confuse strands of hair. Earlier today if two hair strands from different players crossed paths, the jumped from one scalp to another.
I like this strand by strand system. I can directly alter stiffness and bounciness, as well as outside forces acting on each strand (Like wind and water)

I'll have to see how it performs in my game though... so far I've been making it in a separate project.

If I'm lucky, this can even be the solution to my realistic cape/flag issue.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Box2D Rope
« Reply #13 on: 2012-07-20, 04:57:32 AM »
Yeah, I don't know why you would use GetNearestSprite when the sequence of points should never change. Hair doesn't change its structure, it just bends. So did you find a solution that didn't involve GetNearestSprite?

10,000th post on the forums!

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Box2D Rope
« Reply #14 on: 2012-07-22, 08:02:08 PM »
Ok, so I'm back after a 2 day non game developing hiatus due to travelling..

Congrats on the landmark post. :D

I had the hair working before I left. The system I use uses GetNearestSprite for the calculation of forces acting on the hair (I have sprites that act on the hair like the forces of wind resistance, inertia)

The solution I had worked pretty fine. Things are somewhat realistic, and it uses multiple forces to determine the center of motion (Like a pendulum) and the force of gravity, all things that you can learn from a 9th grade physics class.

Hair strands are comprised of particles that disintegrate after a short lifespan and are pushed towards forces like the force of gravity, normal force, and somewhat simple harmonic motion

The best part is

I took the concepts of forces on realistic hair

But I only touched the concepts on the water's surface. I didn't have to dive deep to use a single line of complex math to do this (Disregarding sprite conditions)

For some reason or another, the lines drawn are a light grey, instead of the LineTo()'s chalk white. I'm going to look into that. I'll also upload a video for approval to see if it looks somewhat decent.

Edit: I made a video of what I had so far

http://www.youtube.com/watch?v=Egqt1GKVWG8&feature=plcp

I need to fix the grey and allow dynamic tinting of the lines, and I need to find a way to position the lines drawn behind the sprites of this layer (Which is hard because the hair/line drawing happens after the rest of the layer is complete drawing. But the best part about using 3D space is that I can simply position the lines behind the sprites with the Z axis :D)

Don't let Fraps fool you. The FPS is fantastic!
« Last Edit: 2012-07-22, 09:25:16 PM by #Sharp »