CyanPrime Posted September 3, 2012 Report Share Posted September 3, 2012 So I'm having trouble calculating my drawn part's angles. I'm doing it the same way the guide said to, but I'm still getting bad results (It spins way too much). I'm using slick2d, and Java. The slick2d Image.rotate(angle) function seems to take degrees and not radians, so I don't think that's the problem. Under the code I'll post my CSML file so everyone can see the what I'm working with. (Maybe I should post the images in a zip too?) Alright, so this is my drawing method (or function for those who prefer C terms) EDIT: sorry, it only posts all green, so it's kinda hard to read. Just copy the text into notepad++ and set the lang to Java or open it in a java editor. There's really nothing I can do about the color. public void draw(Vector2f pos){ //recieves a point to draw the sprite on the screen at for(int i = 0; i < myAnim.timelines.size(); i++){ // loop around the current animation's timelines for(int j = 0; j < myAnim.timelines.get(i).keys.get(currentFrame).keyObjects.size(); j++){ // loop around the current timeline's key's (based on the current key/frame) objects (parts to draw) KeyFrame prevFrame = myAnim.timelines.get(i).keys.get(0); //this is a one frame animation, so the previous frame is always 0 (Cause I'm only trying to go from 0 to one right now) KeyFrame keyFrame = myAnim.timelines.get(i).keys.get(currentFrame); // current frame can eather be 1 or 0 (0 to see the basic stance, and 1 to see the animation) KeyObj startObj = prevFrame.keyObjects.get(j); //the previous frame's object (part to draw) KeyObj obj = keyFrame.keyObjects.get(j); // the current frames object (part to draw) Image img = images.get(imgNum[obj.folder][obj.file]).img.copy(); // copy the image needed to be drawn float offX = obj.offset.x; //set the final X offset value to the object's offset value if the current frame is 0 float offY = obj.offset.y; //set the final Y offset value to the object's offset value if the current frame is 0 float angle = obj.angle; //set the final Angle offset value to the object's offset value if the current frame is 0 if(currentFrame != 0){ //don't do any of this if the current frame is 0 boolean subB = false; //boolean to control wether to subtract 360 from the current object's angle in the following equasion boolean addB = false; //boolean to control wether to add 360 to the current object's angle in the following equasion if(obj.angle - startObj.angle < 0 && keyFrame.spin == 1) addB = true; // if the current object's angle minus the last frame's object's angle is negitive and the keyframe's spin variable is 1 add 360 to the current object's angle in the following equasion if(obj.angle - startObj.angle > 0 && keyFrame.spin == -1) subB = true;// if the current object's angle minus the last frame's object's angle is positive and the keyframe's spin variable is -1 subtract 360 from the current object's angle in the following equasion //this is where the error is---------- //if subB is true subtract 360 from the current object's angle in this equasion if(subB) angle = startObj.angle + (((obj.angle - 360) - startObj.angle) * (currentTime - prevFrame.time) / (keyFrame.time - prevFrame.time)); //if addB is true add 360 to the current object's angle in the equasion else if(addB) angle = startObj.angle + (((obj.angle + 360) - startObj.angle) * (currentTime - prevFrame.time) / (keyFrame.time - prevFrame.time)); //if nether are true do nothing to the current object's angle in the equasion else angle = startObj.angle + ((obj.angle - startObj.angle) * (currentTime - prevFrame.time) / (keyFrame.time - prevFrame.time)); //error end? -------------------- //X and Y going through the same equasion as angle offX = startObj.offset.x + ((obj.offset.x - startObj.offset.x) * (currentTime - prevFrame.time) / (keyFrame.time - prevFrame.time)); offY = startObj.offset.y + ((obj.offset.y - startObj.offset.y) * (currentTime - prevFrame.time) / (keyFrame.time - prevFrame.time)); } //if the current keyframe's spin value is -1 rotate counter clockwise if(keyFrame.spin == -1){ System.out.println(currentTime + " " + angle); img.rotate(-angle); } //else rotate clockwise else img.rotate(angle); //draw image at proper location and rotation (at least if everything worked right) img.draw(pos.x + offX, pos.y - offY); } } } <?xml version="1.0" encoding="UTF-8"?> Quote Link to comment Share on other sites More sharing options...
timpart Posted September 4, 2012 Report Share Posted September 4, 2012 This is of general interest since plugins will all be doing something like this. First I'm not sure about the test made by subtracting one angle from another. if(obj.angle - startObj.angle < 0 && keyFrame.spin == 1) addB = true; // if the current object's angle minus the last frame's object's angle is negitive and the keyframe's spin variable is 1 add 360 to the current object's angle in the following equasion if(obj.angle - startObj.angle > 0 && keyFrame.spin == -1) subB = true;// if the current object's angle minus the last frame's object's angle is positive and the keyframe's spin variable is -1 subtract 360 from the current object's angle in the following equasion Suppose startobject is at 10 degrees and current object is at 20 degrees and we are spining in a +ve direction Current - start is 10 degrees, and the test works correctly Now suppose start is 355 degrees and current is 5 degrees. This is still a positive rotation but because it goes through zero current - start is -350 degrees and the code above would incorrectly decide it needs to go the long way around with an extra 360 Perhaps you need a test against -180 or something and add 360 if bigger? The other possible problem is if(keyFrame.spin == -1){ System.out.println(currentTime + " " + angle); img.rotate(-angle); } The angles are always counterclockwise rotations. The spin is simply a hint to work out which way to rotate over the time period. It doesn't alter the representation of angles, so you should always be rotating by the angle and not negating it. Also your calculation of angle works out the exact total rotation of the sprite from a zero position. Does img.rotate() turn it from its current rotation rather than the initial position? If so you are turning by the total amount each frame and not just the change. Is there an image.angle() call you should use instead? Hope this helps, I've only had a brief look at the code. Quote Link to comment Share on other sites More sharing options...
CyanPrime Posted September 6, 2012 Author Report Share Posted September 6, 2012 Thank you for the reply. I think I got it working now. I'm gunna run some more tests, streamline it a bit and then release it. Quote Link to comment Share on other sites More sharing options...
thosthom Posted November 8, 2012 Report Share Posted November 8, 2012 Thank you for the reply. I think I got it working now. I'm gunna run some more tests, streamline it a bit and then release it. Hi, I'd really like to get my hands on this code, did you release it anywhere? Cheers, Thom. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.