Aligning two picts on an Angle
I have 36 images of a ship and 36 of a laser (basically a line) for 36 different angles at 10 degree increments. I'm having trouble figuring out how to align the laser's image according to the ship's image so that the laser always comes out of the laser cannon which is located in the top center of the image when the ship is pointing straight up at 90 degrees.
Here's the code I came up with:
[SOURCECODE]Laser.x = cx + (Cos(direction * PI / 180) * (shipWidth/2))
Laser.y = cy + (Sin(direction * PI / 180) * (shipHeight/2))[/SOURCECODE]
Where (cx, cy) is the center of the ship and direction is the angle in degrees that the ship is pointed.
Now, if i use a circular projectile image (aka a bullet instead of a laser line), then I can verify that the image itself is correctly being positioned at the laser cannon's location. The only problem is, that if I use a large 48x48 image with a thin 4px line in it, you can clearly see that the laser's image also needs to be offset to in order to align the *center* of the laser with the laser cannon instead of the upper left corner of the laser's image.
I came up with some code of my own, but it didn't work:
[SOURCECODE]Laser.x = cx + (Cos(direction * PI / 180) * (shipWidth/2)) + (Cos(direction * PI / 180) * (LaserWidth/2))
Laser.y = cy + (Sin(direction * PI / 180) * (shipHeight/2)) + (Sin(direction * PI / 180) * (LaserHeight/2))[/SOURCECODE]
If someone can help me wrap my head around that part I'd appreciate it.
Here's the code I came up with:
[SOURCECODE]Laser.x = cx + (Cos(direction * PI / 180) * (shipWidth/2))
Laser.y = cy + (Sin(direction * PI / 180) * (shipHeight/2))[/SOURCECODE]
Where (cx, cy) is the center of the ship and direction is the angle in degrees that the ship is pointed.
Now, if i use a circular projectile image (aka a bullet instead of a laser line), then I can verify that the image itself is correctly being positioned at the laser cannon's location. The only problem is, that if I use a large 48x48 image with a thin 4px line in it, you can clearly see that the laser's image also needs to be offset to in order to align the *center* of the laser with the laser cannon instead of the upper left corner of the laser's image.
I came up with some code of my own, but it didn't work:
[SOURCECODE]Laser.x = cx + (Cos(direction * PI / 180) * (shipWidth/2)) + (Cos(direction * PI / 180) * (LaserWidth/2))
Laser.y = cy + (Sin(direction * PI / 180) * (shipHeight/2)) + (Sin(direction * PI / 180) * (LaserHeight/2))[/SOURCECODE]
If someone can help me wrap my head around that part I'd appreciate it.
seems to be your making a lot more work for yourself than you need to be.. if you are just using simple pictures of lasers why not just do the real calculations and draw a laser from within the API your using.. whichever it may be.. I am sure it has a line function..
QuickDraw for example has many functions for drawing primitive objects like polygons, circles and lines etc..
I assume that since your using a pict and your lasers go straight that your calculating the equation of the line between the starting point and the targets (x,y) location..
using the two points, start and target just find the equation of the line.. and then transform your laser along it .. redrawing the line along that line...
this would save on memory and allow a lot more flexibility if you wanted to use the function for other things...
y = mx + b is the general equation of a line if I remember correctly... but don't quote me it's late and I am tired.. but using a drawing function will no doubt be more useful for you..
I'm out.
Hank
QuickDraw for example has many functions for drawing primitive objects like polygons, circles and lines etc..
I assume that since your using a pict and your lasers go straight that your calculating the equation of the line between the starting point and the targets (x,y) location..
using the two points, start and target just find the equation of the line.. and then transform your laser along it .. redrawing the line along that line...
this would save on memory and allow a lot more flexibility if you wanted to use the function for other things...
y = mx + b is the general equation of a line if I remember correctly... but don't quote me it's late and I am tired.. but using a drawing function will no doubt be more useful for you..
I'm out.
Hank
/* Drunk...... fix later.... */
Quote:Originally posted by Muffinking
seems to be your making a lot more work for yourself than you need to be.. if you are just using simple pictures of lasers why not just do the real calculations and draw a laser from within the API your using.. whichever it may be.. I am sure it has a line function..
Because I have 36 laser images (they're basically lines, not purely lines) which look nice and pretty, match the ship nicely, and I get automatic collision detection for using them. I'm using these images and there's no getting around it.
Oh and also because I'm using REALbasic and it's pointless to try and explain it, but I can't draw things like that in it. Well I can, but doing so is even more pointless because of the collision detection and my object orientedness makes all a breeze.
Reading the rest of your message: that won't work. Well it may, but not in this case. All I have to do is offset it the way I already am, just again and in reverse, so I think I've got it figured out. Thanks for the reply though. Sorry if this is a bit curt, but it's late, I'm tired, and I just finished the basic systems of the game
I just realized I posted this in the wrong forum. Sorry...
Anyway, this code works for me:
[SOURCECODE] x = cx + (Cos(direction * PI / 180) * (shipWidth/2)) + (Cos(direction * PI / 180) * (projWidth/2))  projWidth/2
y = cy + (Sin(direction * PI / 180) * (shipHeight/2)) + (Sin(direction * PI / 180) * (projHeight/2))  projHeight/2[/SOURCECODE]
Where direction is the degree the ship is pointing in, projWidth and projHeight are the width and height of the laser/projectile image, and shipWidth, shipHeight is the width and height of the ship. (cx, cy) is the global coordinate of the center of teh ship.
Cool runnings,
Anyway, this code works for me:
[SOURCECODE] x = cx + (Cos(direction * PI / 180) * (shipWidth/2)) + (Cos(direction * PI / 180) * (projWidth/2))  projWidth/2
y = cy + (Sin(direction * PI / 180) * (shipHeight/2)) + (Sin(direction * PI / 180) * (projHeight/2))  projHeight/2[/SOURCECODE]
Where direction is the degree the ship is pointing in, projWidth and projHeight are the width and height of the laser/projectile image, and shipWidth, shipHeight is the width and height of the ship. (cx, cy) is the global coordinate of the center of teh ship.
Cool runnings,
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Formula for converting angle to vector?  komirad  2  12,561 
Jul 29, 2011 07:29 AM Last Post: ThemsAllTook 

Question Regarding the Reflect Angle of a Transition  iBaby  3  4,359 
Apr 27, 2010 03:15 PM Last Post: JustinFic 

ending location from angle and speed  Kazooless  5  5,518 
Apr 3, 2009 02:40 PM Last Post: Gillissie 

Angle between two points?  Graphic Ace  6  6,498 
Nov 8, 2008 12:11 PM Last Post: macnib 

calculating X and Y coordinates w/ an angle and distance  ferum  13  18,192 
Jun 25, 2008 10:53 PM Last Post: rosenth 