Jump to content


- - - - -

Projectile help


9 replies to this topic

#1 rajesh kumar

    Member

  • Members
  • PipPip
  • 30 posts

Posted 09 February 2010 - 12:55 PM

Hi to all,
I tried to do cannon projectiles for a game.I know the destination point where the cannon should land and also have a option of selecting the speed it travels say from 80 Kph to 100 Kph.I need to know the angle.How can i find the angle (theta) for the projectile .MY Launching position is higher than the landing position.Please help me fix this.Thanks in Advance




---Rajesh---

#2 refleX

    New Member

  • Members
  • PipPip
  • 10 posts

Posted 09 February 2010 - 01:40 PM

Read this article which explains inverse trajectory determination.

If you want to rotate the projectile sprite in the direction it is traveling, then subtract your current position by your previous position. This will give you your linear velocity at that current point. With this you can calculate the direction of the projectile.

Direction in radians:

dirInRad = atan2(linearVelocity.y, linearVelocity.x);

Convert this value into degrees:

dirInDeg = 180 / pi * dirInRad;

Hope this is the answer you were looking for.

I would also suggest using a physics library if you don't have the time to learn the technical stuff. You can google physics library followed by your language of choice.

#3 rajesh kumar

    Member

  • Members
  • PipPip
  • 30 posts

Posted 10 February 2010 - 11:41 AM

Hi reflex,
I tried it but i don't know how to solve the equation with unknown variable theta.Any help guys

#4 Nerd_Skywalker

    Valued Member

  • Members
  • PipPipPip
  • 215 posts

Posted 10 February 2010 - 11:56 PM

I think that we'll need more information, I was working on this exact thing today but I need more information to help you.

EDIT: I need information like if you have two points to work from (i.e. are you using the mouse to control the cannon or what?)

If you have the angle in radians, the move vector is:
Vector2 move;
float rotation; //The rotation in radians
int speed; //The speed in pixels per frame

move.X = (float) Math.Cos(rotation) * speed;
move.Y = (float) Math.Sin(rotation) * speed;
(The code is in C# because I have been using XNA recently, so C# it is :))

And the way to get degrees to radians is:
int degrees;

float radians = degrees * PI / 180;

And if you want the angle between two points:
float dx, dy;
float rotation; //Angle in radians

dx = x2 - x1;
dy = y2 - y1;

rotation = (float)Math.Atan2(dy, dx);

I hope I helped some :D

P.S. I am not entirely sure how much of this will actually work, but you can try it ;)
Re-dun-dant adj. 1. See redundant

TheNut said:

"Hmm, yes. Strong is the force with this one"

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 11 February 2010 - 12:10 AM

Nerd Skywalker: maybe it would be more helpful if you said what additional information you want? ;)

reflex: I can't seem to see the article you linked to (I "reached a page that is unavailable for viewing or reached my viewing limit for this book"). However, OP, you may find this page useful, particularly the last section "Angle of Launch".
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 rajesh kumar

    Member

  • Members
  • PipPip
  • 30 posts

Posted 11 February 2010 - 04:55 AM

Hi thank you for your reply guys,
I need the angle of elevation.I know the launch point its greater than the target,I know the destination point where it should land,i know the speed it travels(its option for users to select the speed between (60 - 100),I need the angle of elevation to reach the destination point.
Ex : Angle of elevation differs based on the value of the variable speed.
Help me guys.Thanks in advance.


---Rajesh---

#7 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 11 February 2010 - 06:27 AM

Learn some algebra. This is a pretty easy problem to solve yourself; you already have all the equations. ;)
reedbeta.com - developer blog, OpenGL demos, and other projects

#8 Nerd_Skywalker

    Valued Member

  • Members
  • PipPipPip
  • 215 posts

Posted 11 February 2010 - 01:09 PM

If I understand your problem correctly, you need to take the locations of the cannon and where it will land, then plug it in to my first equation up there. That should work (unless I completely don't understand the problem :))
Re-dun-dant adj. 1. See redundant

TheNut said:

"Hmm, yes. Strong is the force with this one"

#9 refleX

    New Member

  • Members
  • PipPip
  • 10 posts

Posted 11 February 2010 - 01:41 PM

@Reedbeta - The link I posted was a chapter from Game Programming Gems 2, maybe rajesh kumar can try getting hold of a copy. Judging by his latest reply, the section "Angle of Launch", or page 207 of Game Programming Gems 2(if the link works), is exactly what he needs.

#10 rajesh kumar

    Member

  • Members
  • PipPip
  • 30 posts

Posted 12 February 2010 - 01:02 PM

reflex thank you for your reply,
I am trying it but i am doing some mistake while solving equation.I don't know where i did wrong.Your link and reedbeta both helped me a lot.
Thank you





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users