Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to throw a spear
Started by alex81 Mar 10 2014 06:32 PM

9 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alex81

[GLOBAL: userInfoPane.html]
alex81
  • Starter
  • PipPip
  • 12 posts
  • Member

I'm trying to figure out how to throw a spear.

Drew up a simple object that looks kind of like (the image is 100 width by 15 height):

 

------------------>

When a spear flies in real life, it usually flies up with sharp end first, and falls down with same end. The handle/stick portion follows it.  I don't understand how I can accomplish this.

 

I tried changing center of the throw first with this api:

arrow:applyLinearImpulse(forceMag*xComp * distance / 2, forceMag*yComp * distance / 2, arrow.x + 10, arrow.y) 

(the calculation isn't very important, it seemed to give ok force for what I was trying)

 

But this does not give different weight to spear head and the "stick" portion, so it looks like an awkwardly thrown stick.

 

So I created a complex body based on this: https://developer.coronalabs.com/content/game-edition-physics-bodies#Complex_body_construction

But that didn't help either, this is what I tried:

 

local arrowHead = { 50,10, 50,-10, 30,-10, 30, 10 }
local arrowRest = { -50,5, 30,5, 30,-5, -50,-5 }            
physics.addBody( arrow,
                { density=1.0, friction=100, bounce=0.3, shape=arrowHead },
                { density=1.0, friction=100, bounce=0.3, shape=arrowRest }
                ) 

And the spear doesn't seem to rotate when it's thrown with complex body.

 

Again: I'd like the spear head to lead and body to follow, as it happens in reality.

 

Any help is appreciated.  I'm looking to assign weight/mass to different physics bodies, is that the solution? how do I do that?



[TOPIC: post.html]
#2

Alex@PaNc

[GLOBAL: userInfoPane.html]
Alex@PaNc
  • Pro
  • PipPipPipPipPipPip
  • 802 posts
  • Jedi

I'd say using a couple different things will achieve the result for which you're looking.

 

First, you'll probably want to review the applyForce API that's available. Using the physics joints to attach separate bodies to each other would be a good next step. The link to the Joint guide can be found here.

 

With all that, if it were me, I'd create three objects; the shaft, the spearhead, and another invisible object to attach to the tip of the spearhead. I would then create the physics bodies, making the invisible object a high density, and attaching the shaft to the spearhead, and the spearhead to the heavy invisible object. Then, I would attempt to apply force to the back of the invisible object, and allow it to rotate slightly in flight, so that the connected bodies would rotate with it. 

 

Maybe not the absolute best way to accomplish it, since I haven't tested the above, but it's what I would try first.



[TOPIC: post.html]
#3

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Pro
  • PipPipPipPipPipPip
  • 1,922 posts
  • Jedi


[TOPIC: post.html]
#4

Burhan J

[GLOBAL: userInfoPane.html]
Burhan J
  • Basic
  • PipPipPipPipPipPip
  • 106 posts
  • Jedi

Hi,

 

@jstrahan LOL. :lol: Only wish if coding is easy as throwing stuff.

 

@alex81 This is the link to arrow examples in the code exchange dated 3 years ago from other user. Still works.

 

You probably won't find it if you do a search at the code exchange since they revamped the site.

 

I have to dig into my treasure chest. :D Give it a try and get some basic idea how to implement.

http://developer.coronalabs.com/code/arrow-basics

 

 

Good Luck!

 

 

burhan



[TOPIC: post.html]
#5

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Pro
  • PipPipPipPipPipPip
  • 1,968 posts
  • Jedi

Hey,

Glad someone is tackling this because I tried to do it a couple of years ago and failed.

I started with trying to convert the code on this page:
https://www.iforce2d.net/b2dtut/sticky-projectiles

There's some great videos and the guy really knows his stuff. Unfortunately, I could never get the arrow to fly quite right. I'm not sure where my code is right now but you might find it in the old code exchange.

His approach was to model the arrow as a stick but then apply some other forces to give the effect of drag on the feathers, which causes the arrow to take its realistic trajectory.

If I were to try now I would simply throw a ball and have an enterFrame listener place an arrow image with the tip at the ball and the rest of the image following the path the ball took. Some trigonometry needed there but it's fairly simple. (If you need it I posted my mathlib.lua to the new code exchange recently.)

Let us know how you get on.

Matt

[TOPIC: post.html]
#6

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Pro
  • PipPipPipPipPipPip
  • 1,968 posts
  • Jedi

So here's what I managed to produce from converting the code on iForce2d.com:

 

https://dl.dropboxusercontent.com/u/10254959/ArrowDirection.2014-03-11.001.zip

 

It's not perfect and, as I said, I would do it differently now, simply because this version isn't what I wanted.



[TOPIC: post.html]
#7

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Pro
  • PipPipPipPipPipPip
  • 1,968 posts
  • Jedi

I've been looking around again and the same posts are coming up again. This is a good example but I've had trouble converting it to Lua previously:

 

http://www.emanueleferonato.com/2012/12/10/flying-arrows-simulation-with-box2d/



[TOPIC: post.html]
#8

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Basic
  • PipPipPipPipPipPip
  • 273 posts
  • Jedi

Late reply, still looking for an answer?

Q: which falls faster, a brick or a feather? and why? (hint: not mass/gravity)

Box2D doesn't simulate air-resistance, so any "real physics" you happen to know about airfoils (like spear/javelin/arrow/etc) just don't apply. You'll have to fake it. Try two separate bodies (head and shaft) and weld them together. Set gravityScale on the shaft slightly lower (say 0.9 -ish) and viola!

You'll "know" it's working if you can just place the spear in mid-air (without applying any other forces) and it starts tipping head-ward all by itself as it falls. You'll probably have to tweak it until the fakery seems adequately realistic.

hth

[TOPIC: post.html]
#9

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Pro
  • PipPipPipPipPipPip
  • 1,968 posts
  • Jedi

Dave is totally correct and this forms the basis of iForce2D's article on "sticky projectiles" (minus the sticky bit.)

 

As I mentioned before, I've tried working on converting his code before and really not got good working code from it.

 

I believe that I do now. Here's a video for you to judge for yourself: http://screencast.com/t/7f2i6cqilgu

 

And here's the code: https://www.dropbox.com/s/lld8857e40jx7ht/ArrowTest.2014-03-13.001.zip

 

I have boiled down the logic of the drag force to be simply the flight vector (direction and speed as x and y) of the arrow, reverse it and apply it to the tail of the arrow. With a little angular damping on the arrow to counter the effects of swinging (as mentioned in the article) I think this works quite well.

 

One point of note: The screen size of my demo is huge! 1024*3 x 768*3 But this is simply so I could really throw things around. You should reduce some of the values and see what you get.



[TOPIC: post.html]
#10

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Pro
  • PipPipPipPipPipPip
  • 1,968 posts
  • Jedi

Right, this is now improved with a drag limiter, committed to the code exchange, github and with a new video:

 

Code exchange: http://code.coronalabs.com/code/arrow-drag-applied-tail-feathers

Github: https://gist.github.com/HoraceBury/9544932

Video: http://screencast.com/t/vcPLSJR6xkIq

Dropbox: https://www.dropbox.com/s/cfdvyetvqghrkq3/ArrowTest.2014-03-14.001.zip




[topic_controls]
[/topic_controls]