Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // new module script
- __Rope Rope;
- export Rope;
- struct SRopePoint
- {
- float x;
- float y;
- float ox;
- float oy;
- };
- #define NSEG 32
- SRopePoint rps[33];
- float x1, y1, x2, y2, elen, gravity, damping;
- DynamicSprite *rope_sprite;
- function __Rope::SetStartPoint(float x, float y)
- {
- x1 = x;
- y1 = y;
- }
- function __Rope::SetEndPoint(float x, float y)
- {
- x2 = x;
- y2 = y;
- }
- function __Rope::SetLength(float len)
- {
- elen = len/IntToFloat(NSEG);
- }
- function __Rope::SetGravity(float g)
- {
- gravity = g;
- }
- function __Rope::SetDamping(float d)
- {
- damping = d;
- }
- function noloopcheck __Rope::Update()
- {
- float dt = 1.0/IntToFloat(GetGameSpeed());
- // verlet step
- int i = 1;
- while (i < NSEG)
- {
- float x = rps[i].x;
- float y = rps[i].y;
- rps[i].x = ((2.0 - damping)*x - (1.0 - damping)*rps[i].ox);
- // gravity
- rps[i].y = ((2.0 - damping)*y - (1.0 - damping)*rps[i].oy) + gravity*dt*dt;
- rps[i].ox = x;
- rps[i].oy = y;
- i++;
- }
- // satisfy constraints
- int it = 0;
- while (it < 2)
- {
- i = 0;
- while (i < NSEG)
- {
- float dx = rps[i+1].x - rps[i].x;
- float dy = rps[i+1].y - rps[i].y;
- float dlen = Maths.Sqrt(dx*dx + dy*dy);
- float df = (dlen - elen)/dlen;
- rps[i].x = rps[i].x + 0.5*df*dx;
- rps[i+1].x = rps[i+1].x - 0.5*df*dx;
- rps[i].y = rps[i].y + 0.5*df*dy;
- rps[i+1].y = rps[i+1].y - 0.5*df*dy;
- i++;
- }
- it++;
- }
- // snap end points
- rps[0].x = x1;
- rps[0].y = y1;
- rps[NSEG].x = x2;
- rps[NSEG].y = y2;
- }
- function __Rope::GetGraphic()
- {
- if(rope_sprite!=null){
- return rope_sprite.Graphic;
- } else {
- return -1;
- }
- }
- function __Rope::Create(int rope_area_width , int rope_area_height)
- {
- if(rope_sprite!=null){
- rope_sprite.Delete();
- }
- rope_sprite = DynamicSprite.Create(rope_area_width, rope_area_height, true);
- //bg = DynamicSprite.CreateFromBackground();
- float nseg = IntToFloat(NSEG);
- float dx = (x2 - x1)/nseg;
- float dy = (y2 - y1)/nseg;
- float x = x1;
- float y = y1;
- int i = 0;
- while (i <= NSEG)
- {
- rps[i].x = x;
- rps[i].y = y;
- rps[i].ox = x;
- rps[i].oy = y;
- x = x + dx;
- y = y + dy;
- i++;
- }
- // run a second or two to settle
- int f = 0;
- while (f < 300)
- {
- Rope.Update();
- f++;
- }
- }
- function __Rope::Render()
- {
- DrawingSurface *surf = rope_sprite.GetDrawingSurface();
- surf.DrawingColor = COLOR_TRANSPARENT;
- surf.Clear();
- surf.DrawingColor = 65535;
- int i = 0;
- while (i < NSEG)
- {
- DrawAntialiasedLine(surf, rps[i].x, rps[i].y, rps[i+1].x, rps[i+1].y, 10);
- i++;
- }
- surf.Release();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement