Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: Jyujinkai on January 03, 2009, 05:44:04 PM

Title: How can I make a sprite move from one location to another on an ARC?
Post by: Jyujinkai on January 03, 2009, 05:44:04 PM
I am trying to do somthign that seams simple but I can not get it to work.

What I am trying to do is move a sprite from one point on the screen to another and CURVE while doign it instead of movign alogn a strait line.

Any ideas on hwo to do this?

I have this code... but it returns a wonky curve... mabey it can be improved or there is another way to do this?

Code: WME Script
  1. on "LeftClick"
  2. {
  3.   var FireBall = Game.LoadEntity("actors\fireball\fireball.entity");
  4.   FireBall.X = actor.X;
  5.   FireBall.Y = actor.Y - actor.Height;
  6.   FireBall.Active = true;
  7.   var Slope =  (this.Y - (actor.Y - actor.Height)) / (this.X - actor.X);
  8.   var curve_size = 3;
  9.   Game.Msg(Slope);
  10.   while (FireBall.X  <  this.X) { // shoot left 
  11.         FireBall.X = FireBall.X + 4;
  12.         FireBall.Y = FireBall.Y + 4 * Slope;
  13.         FireBall.Y = FireBall.Y + Math.Sin( (-1*180)* FireBall.X / ( actor.X - this.X ) ) * curve_size; 
  14.         Sleep(20);
  15.   }
  16.   while (FireBall.X > this.X) { // shoot Right
  17.         FireBall.X = FireBall.X - 4;
  18.         FireBall.Y = FireBall.Y - 4 * Slope;
  19.         FireBall.Y = FireBall.Y + Math.Sin( (-1*180)* FireBall.X / ( actor.X - this.X) ) * curve_size; 
  20.         Sleep(20);
  21.   }
  22.   FireBall.Active = false;
  23. }

This code takes the X,Y of the player actor as one point to move teh sprite from and the X,Y of the spriet entity that you click on as teh XmY of where the sprite is ment to end up.

Demo Of how it is now (http://www.sendspace.com/file/sg1wfv)
Source used in Demo (http://www.sendspace.com/file/q91ukq)
Title: Re: How can I make a sprite move from one location to another on an ARC?
Post by: Jyujinkai on January 05, 2009, 02:48:45 AM
Ok, I have finaly found a solution for this... i have no idea if it is correct or if tehre ie a better one... but it works.. so screw it... If you wish to use it feel free!!

How it works... (Have a look at these pics)
(http://www1.picturepush.com/photo/a/1296819/220/1296819.png) (http://www1.picturepush.com/photo/a/1296819/img/1296819.png)(http://www5.picturepush.com/photo/a/1296823/220/1296823.png) (http://www5.picturepush.com/photo/a/1296823/img/1296823.png)


You have 2 known points on A-B on the X,Y Plane of screenspace.
Say your Actor and what ever your actor is thorwing somthing at.

You also create a value for D. This is the angle that determins the height of the curve. The closer this value is to 0 the flatter the curve will be , the closer it is to 45, the more rounded it will be.... no idea what is beyond 45... but 45 makes a perfect circle, with the line from a-b teh diameter.

So a good idea is that you can set up some code to make D a value based off the distence of the A and B from each other... or w/e. This angle is made up and the code uses a variable for D, so you can manipulate it somehow if you like..

Anyway.

Once you have A,B and D values you can use them to find the location of H (see drawing)
Once you have A,B,H you can use these values to find the exact center (C) of the circle whos arc passes though A,B and H.
Once you have A,B,C,D,H You can work out the X,Y location of every point along the arc that connect A,B,C.

Ok... how dose this work?

Well, I have made a small demo scene for you. So just have a look though it.

All the code is in a few places, each button acivates a step of the code, and all teh variables are stored in BASE.INC for ease... there is some work to get this code working in a real enviroment... but well... the math works so it is just playign about and buildign functions and methods and tuff from it....

So Have fun!!!


Source Code of Curve Demo (http://files.filefront.com/CurveArc+Demo+Sourcerar/;12873101;/fileinfo.html)
ExE Demo of Code to Curve (http://files.filefront.com/Math+CurveTestrar/;12873093;/fileinfo.html)


Again, Thanks to Jerrot, Od, Mnem and Sound.