Finding line slope, endpoint
I''m writing an Asteroids clone (yes, I know that''s lame, but...) and one of the weapons the player can get is a laser. In order to display a laser being shot I want to draw a line from the ship to the edge of the screen. If I know the (X,Y) location of the ship and the angle the ship is pointing, how would I calculate the endpoint of a line somewhere off the screen so I could draw a line for the laser? Or is there some other better way to do this?
Thanks,
Ishamael
Okay if you know the angle the player is facing, and want to draw a line 100 pixels long, then use trigonometry:
You will also have to invert the y endpoint because on the computer screen y=0 is at the top of the screen while on the cartesian plane y=0 is at the bottom (of the 1st quadrant).
Hope that helps!
start_x = player.x;start_y = player.y;end_x = start_x + (radius * sin[angle]);end_y = start_y + (radius * cos[angle]);
;You will also have to invert the y endpoint because on the computer screen y=0 is at the top of the screen while on the cartesian plane y=0 is at the bottom (of the 1st quadrant).
Hope that helps!
Don''t draw off the edge of the screen. It is a bad habit. Use the slope of the line and the distance to the edge. Based upon the direction of the ray you can only hit two sides. Pick one and use it. If the point you hit that side is past the other side then repeat for the other side otherwise use it. If you have an angle instead of a direction vector, then the tangent is the slope. The only thing you have to watch out for is vertical lines, but then that is easy. It is either the top edge or bottom edge with the same x.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement