# moving arrow, shooting

hi there
i am trying to programme something simple for mobile phones using J2ME and midlets, but the problem is a gerneral problem and someone who does not know anything on midlets or J2ME can help, but ideally someone with some background knowledge on mobile phone programming would be good.

the problem i am trying to solve is as follows :
i want to have a arrow on the screen, if you move the right cursor, the arrow should point in cockwise positions, for example suppose that the arrow is initally pointing 12:00 if the right cursor key is pressed the arrow should point 1:00 or if left is pressed the arrow should point at 11:00, when the shoot button is pressed a pixel should shoot in that direction.   This involves trigonemtry any help sould be appreciated
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You would simply use parametric equations.  The general idea is using a formula like this:

See this site for further instruction:
http://mathworld.wolfram.com/ParametricEquations.html

But to make it work you would do a 360 / 12 to get your T offset of 30.

Then you will use this formula:

On mathworld it is giving you a equation that would give you a counter-clockwise rotation.  Pretty much you use that equation and display the line a T Offset where your initial T Offset would be 90 to achieve the arrow pointing to 12 on a clock.  Then incriment or decrment by 30 depending on which arrow is pressed.

The trajectory bullet will travel along that point so you would use another parametric equation to animate it where your radius of the line is the distance the point has traveled from the point.  ;-)

Simple as that.

Let me know if you have any more questions:

~Aqua
Commented:
The above method should work, however it will involve alot of math and some phones will force the game to slow down calculating the results.

2 posibilities are:
1) doing the calculations beforehand and simply putting the results(that would be used by the game) in an array. depending on how many factors you wish to include(wind factor, angle, power, ect) or exclude this can be memory/process efficent.

2) if you only have a limited number of selections(you can only more the arrow right 3 times or left 3 tims) then you can simply assign a "field" of where the arrow will hit. if the target is in that field then you could count it as a hit.
None the less Diane he will still need to use the parametric equations to determine the predefined vectors.  Plus that equation is not process intensive enough to slow anything down noticibly.  With Micro Apps for phones and such you are more at a limitation of space rather than processing speeds.  The processing for those equations are not overly complicated, the sin / cos are simply lookups and the multiplication is a standard process.  Round about... You will save next to no time saving the data over calculating it.  It saves some processing power.. yes... but not enough to overly worry about it.

~Aqua

((Plus trajectory of the projectiles will almost always need to use a parametric equation, predefined vectors will not efficiently work in that case))
Author Commented:
hi there, thanks for the replies
i havnt got the chance to see if any of your responses work yet guy, my laptop short circuited and is driving me mad, please bear with me, ill check real soon and give my response, in the mean time if any one want to extend on there response please be my guest, the more info the better =))

Author Commented:
ok, my laptop has serious problems, but ive taken  a look at what you guys have written, ok i realise you have shed some light to the clockwise thing, but how would the arrow image move in that direction, or would i have pre rendered images pointing at the angle?
Author Commented:
also how would i use this to shoot the projectile?
You use the line classes and draw a line, your XY Coords of the end you are rotating is what you are calculating using the Parametric equations.  YOu change the End Point you want rotated to the new endpoint you calcuated and it will redisplay the line when you repaint() and validate() the window.

~Aqua

PS:  Plus you use the equations I spelled out word for word for you.  You increment the T value instead of leaving it the length of the line and the projectile will travel along the path of the line.  If your T value is 1 you are 100% if you raise it then the length continues to go... say you have a line length 1 you say in your parametric equation you want the projectile to start at the tip of the barrel. You get the center coord by using the parametric equation with a T value of 1.. Which gives you a percentage by 1 * 100 = 100%  Then say you want to make it go 2 units away... thus you use a T value of 2 which is 200%.. .Which then doubles the length of the line to 2 units.

Pretty simple.  Play around with it a bit and ask questions if you can't figure it out after at least ATTEMPTING to apply my algorithm.
Author Commented:
i am using j2me, midp, are there any line classes available for this?
Author Commented:
hi there just wanted 2 let you know that im almost done sorting my computer out, just having difficulties setting up j2me and will be able to try experimenting with the moving arrow thing soon, thanks
Author Commented:
yes!!!!! got my wtk running now i can get to business, aqua you or anyone else know how which library to use to call sin/cos for j2me?
Author Commented:
also there is no cos/sin in j2me how do i make my own?
Author Commented:
ok guys i think im nearly there,
all i used was the drawLine(int,int,int,int); function and i got my arrow moving round in circles, now what i need is a projectle to follow the line, how do i do that?

I QUOTE:

"PS:  Plus you use the equations I spelled out word for word for you.  You increment the T value instead of leaving it the length of the line and the projectile will travel along the path of the line.  If your T value is 1 you are 100% if you raise it then the length continues to go... say you have a line length 1 you say in your parametric equation you want the projectile to start at the tip of the barrel. You get the center coord by using the parametric equation with a T value of 1.. Which gives you a percentage by 1 * 100 = 100%  Then say you want to make it go 2 units away... thus you use a T value of 2 which is 200%.. .Which then doubles the length of the line to 2 units."

DO try to read our posts before asking the same question over again!

Good Luck,

~Aqua
Author Commented:
hi aqua sorry bout the delay man my computer got busted and went in for repairs, when they gave it back they formatted it and i lost everything, hopefully ill solve this soon and the points will b yours
cheers
If you need further clarification on any of the equations or methods to solve these equations feel free to let me know.  The only thing I was previously saying is if you have a question on what I've previously said let me know, but don't restate the initial question.

In all reality you use the two end points of your initial equation and use a standard parametric equation to determine the new end point.  If you use a unit vector (a line of length 1) your percentage will be the unit length of the line.  So essentially it pays off quite a bit to use a line of length 1 as your base line for further calculation.  If you want to factor gravity, drag or wind into these calculations you can do a mutation of the final point you get and alter it's coordinates by weight and air drag.  But that is probabally more than you want to get into for a cell phone game.

If you need any help with your math let me know... Just make sure you post what you have thus far so I can take a look and make the needed corrections to your algebra.

~Aqua

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
hi there aqua

i have used the drawline method in j2me where g.drawLine(m_prevX, m_prevY, m_x, m_y);

the following is my code:
x2,y2,x1,y1,m,c,cs,ys1,xs1;
......
......
g.drawline(x,y,r,l);  //where x,y is the starting position and r and l are end position draw these
.....
....
switch(getGameAction(key)){

case UP:  l+=50;r+=50;
x1 = x; y1 = y;

x2 = r; y2 = l;
m = (y2 - y1) / (x2 - x1);

c = y1 - m * x1;

ys1 = man[2].gety();    //target this man
xs1 = man[2].getx();  //get his x,y cordinates

cs = ys1 - (m * xs1);

if(c==cs){
System.out.println("gota yahhhhhhhhhhhhhhhhhhhhhh");

}

i dont need 2 draw the projectile, as long as the line moves over the soldier and prints out hte message "gota yahhh" im happy
Author Commented:
here is what the equations do:

m = (y2 - y1) / (x2 - x1);  // get the gradient of the line
c = y1 - m * x1;   //where the line crosses y axis
cs = ys1 - (m * xs1); // working out hte value guy being shot at

if(c == cs) { }   // if the coordinates are the same
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.