### How to program a grappling hook?

closed account (iNU7ko23)
Could anyone help me with programming a grappling hook for a 2d game. Only...I'm a bit clueless so tips would be very helpful.
you mean the drawing or the math behind it ?
closed account (iNU7ko23)
Well, mostly the maths though I don't really know how to draw a physical like rope object.
I think he means programming it so that when the hook hits a spot you can hook onto it will catch and pull you across. Of course when he says Grappling Hook I think of the Hook shot from Legend of Zelda Link to the Past.
http://img806.imageshack.us/img806/589/grapple.png
all colors i am using can be found on the site above.
i made a little drawing. so first start with a dot and make it longer and longer until you hit the square (yellow line).
start drawing the red line and make it longer and longer in the same direction of the yellow line. once it has the size you want you should make the angle between the square and the red rope smaller and smaller untill it is 0. then split the red rope length into 2 parts:
1. same length as width of square
2. red rope length - width of square (becomes blue rope)

keep doing this kind of algorithm untill you can't split the rope anymore or untill the rope hits something

closed account (iNU7ko23)

Sorry if I didn't explain very well but basically, I am programming a 2d plat former and would like to make a grappling hook object. In the image link I quickly provided hopefully it shows that I plan for the player to be able to click and hook and shoot their grappling hook so they swing along, I know it's complicated. But however I'm not sure how to program this, I have never programmed physics like this before and have recently started making 2d games.
o i thought you ment the shooting of a grapple around some horizontal pilar
i am trying to explain the main thing. post me if you don't know what i mean or if anything isn't clear.
so what i should do is a horizontal line at.( leftside at (x1,y1) rightside at (x2,y2))
x2,y2 is the point on which the rope is turning around so stays the same
length = length of your rope;
so `sqrt((x1-x2)²+(y1-y2)²)=length;`
your x1 value shoud go from x1 to x1+(2*length). so somekind of loop
`for (float x1=-20;x1<=20;x1=x1+(100.f*App.GetFrameTime())) //or however you do the speed and frames. `
so in the loop we see that length=20; // actually loop should depends of length but it's an example
you have a value for x2 and y2
and you are looping through all values of x1. so you have enough variables to solve the equation:
`sqrt((x1-x2)²+(y1-y2)²)=length`
which becomes
`y1=sqrt(length²-((x1-x2)²))+y2;`
then you just have to put the drawing into your loop
i invented a function drawline() (i hope it is clear what it does)but i dont know what function you can use in your library, so just replace it any function avaible
`drawline(x1,y1,x2,y2)`
and make sure you replace the lines and not just add a new one.

ok i have tried to translate this to some code in my library everything is quite oblivious i think and i got this:(it works nice for me)
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include "stdafx.h" #include #include #include #include #include #include #include #include #define pi 3.14145926535 int main() { reboot: sf::RenderWindow App(sf::VideoMode(800, 600), "made by Jannes"); float x1=100;float x=x1; float y1=100;float y=y1; float x2=200; float y2=100; float length=sqrt(pow((x1-x2),2)+pow((y1-y2),2)); int a=0; sf::Shape grapplerope=sf::Shape::Line(x1,y1,x2,y2,2.0f,sf::Color(128,128,128)); // Start game loop while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::R)) { App.Close();goto reboot; } } // Clear screen App.Clear(sf::Color(10,10,10)); // Draw shapes App.Draw(grapplerope); //elapsed time float ElapsedTime = App.GetFrameTime(); float alpha=asin((y1-y2)/length)+0.001f; if (x1<2*(length)+x-0.1f && a==0) {//change 0.1f to a bigger number if you want it to sling less at the left side x1=x1+(100.f*alpha*App.GetFrameTime()); } else { a=1; x1=x1-(100.f*alpha*App.GetFrameTime()); if (x1-0.1f<=x) {//change the 0.1f to a bigger number if you want it to sling less to the right side. a=0; } } y1=sqrt(pow(length,2)-(pow((x1-x2),2)))+y2; grapplerope=sf::Shape::Line(x1,y1,x2,y2,2.0f,sf::Color(128,128,128)); App.Display(); } return EXIT_SUCCESS; }``````
Last edited on
Assuming your hero(heroine, alien,whatever) is standing still while they shoot the grappling hook and there are no moving platforms there, you could:

 ``` Keep the direction and length of the rope linked to the hook for each game tick: advance the rope a little bit(dt * speed) check if the rope intersects with a platform if the rope intersects with a platform, clip the rope so the hook touches the platform if the hook can grapple that kind of platform, make the hero move in a circle with the hook as center and the rope as radius else make the hook fall end if clear the 'shooting a hook' flag end if repeat until -the rope has reached max length -or the hook has hit a platform or enemy ```
Last edited on
closed account (iNU7ko23)
Thankyou very much you were all very helpful.
i have been searching for a nicer way to do this and now i have a program that has 0 speed at top height and max speed at the bottom. (by using physics) here is the code for anyone who is interested: (i used the same display method as my previous post here)
 ``12345678910111213141516171819202122`` ``````//initialization: double x1=276.1782; double y1=35.2166; double x2=200;//center of rope double y2=100;//center of rope double dt=1/100.0;//speed double length=sqrt(pow((x1-x2),2)+pow((y1-y2),2)); double theta=atan2(x1-x2, y1-y2); double E = g*length*(1-cos(theta)); double v=0; //in the game loop double a = -g*sin(theta); double s = v*dt + 0.5*a*dt*dt; double vForce = v + a*dt; theta = theta + s/length; v = sqrt(2*(E-g*length*(1-cos(theta)))); if (vForce < 0) { v = -v; } x1 = x2 + length*sin(theta); y1 = y2 + length*cos(theta);``````
Topic archived. No new replies allowed.