Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 7:09 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Feb 18, 2012 5:42 am 
Offline

Joined: Tue Jan 03, 2012 5:48 pm
Posts: 11
Well, to start this out; I'm only in 8th grade. I'm quite proficient with the library already, (not a master by any means) but I've been struggling with some of the more advanced math. For instance, I'm only in Algebra 1, which isn't even getting into any sort of fun Geometry (math, fun? what is this!?), and some of the stuff I'm doing is trigonometry.

Namely two things I've been struggling with. One more being about the engine itself rather than the math for it.

I'll start off with the engine one. I have an image ~32x64 pixels. Currently it rotates around the center of the 2x2 spot in the middle of the picture. Now, this might not sound too bad, but I'm making a turret sort of thing. And not the top-view ones either.
So, tl;dr: How do I change where an image rotates around

And secondly, the problems I've been having with trigonometry. Now, that same image is in the shape of a rectangle, how would I calculate the tip (where it fires from) based off of the angle of the rectangle and the height of the rectangle? I've been trying to both do this out on paper and in actual code for a week or so now, but I just couldn't get it.


Thank you for your help
Crockeo


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 6:14 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
You can change the center of rotation on an image by using setCenterOfRotation.

As for the top of the turret, it depends on what your goal is. If, for example, you wanted to render another image or particle system at the head of the gun, you would be better off rendering the two elements in place (i.e. initial angle), and then making it rotate by calling g.rotate before and after drawing the various turret parts.

On the other hand, if you are only trying to find the rotated point for the sake of shooting a bullet, you might find it easier to use vectors for bullet movement. See here:
http://www.rodedev.com/tutorials/gamephysics/

Lastly... if you really need to find the rotated point, you could look into 2D rotation:
http://www.siggraph.org/education/mater ... 2drota.htm


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 4:33 pm 
Offline

Joined: Tue Jan 03, 2012 5:48 pm
Posts: 11
Thank you! I guess I'm not all that proficient with the engine after all :P

I'll check out the links you posted. If I have any more problems I'll come back here.

Thank you!
~Crockeo


Top
 Profile  
 
PostPosted: Wed Feb 29, 2012 7:30 pm 
Offline

Joined: Thu Sep 01, 2011 10:15 pm
Posts: 31
Location: United States
Another trick is instead of calculating the tip of the gun.... just launch the bullets from the center of the turret, just render the turret after rendering the bullets so that the turret will cover up the fact the bullets are launching from it's center.

Math stuff

Sine = y/h
cos = x/h
tan = y/x

Remember, if you ever need to use atan.... use Math.atan2 not Math.atan. Math.atan only calculates half of a circle -90degrees to +90 degrees.

I currently like the Point library in java. It has a nice helper function called distance and is an easy way to store lots of x/y pairs. there is also Point2d.Float and Point2d.double if you need higher precision for points as well.


Top
 Profile  
 
PostPosted: Mon Mar 19, 2012 3:41 pm 
Offline

Joined: Fri Dec 16, 2011 5:15 pm
Posts: 6
You can use some maths to find the location at the end of the turret, you need to know the angle the turret has been rotated by and the length of the turret from the middle of the sprite (the radius).

x = sprite_pos_x + cos(angle) * radius;
y = sprite_pos_y + sin(angle) * radius;

You might have to convert the angle from degrees to radians depending on how you want to store angle.
Assuming I have got sin and cos the right way around (can't check just now I'm afraid) you can use a similar technique to calculate the x and y component of your bullet movement:

dx = cos(angle) * speed;
dy = sin(angle) * speed;

you end up with dx and dy which you can add to the x and y coords of your bullet every frame to make it move in the direction the turret is pointing.

cheers,

nick


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Cbeed, Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group