Results 1 to 2 of 2
- 12-11-2011, 04:19 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
So apparently sin((pi * 0) / 180)) * 0 * -1 = 1
Hello. I'm fairly new to java, but I feel like I know enough to atleast get part of my game engine written. However, there is one annoying error that just doesn't seem to want to fix itself. I'm trying to convert a direction(in degrees) and speed (in pixels per frame) into hspeed(the amount of pixels to move the object horizontally) and vspeed(the amount of pixels to move the object vertically).
Thanks to a very nice person at the YoYoGames forum, I have been able to find out that the correct formula to use for this is as follows:
However, I'm having a slight problem: once the object has slowed down to a speed of 0, the hspeed is reported as 1.0.Java Code:hspeed = cos( pi * direction / 180 ) * speed vspeed = -sin( pi * direction / 180 ) * speed
Here is my source code. I must warn you, though: it is very lengthy.
runner.java
room0.java:Java Code:import javax.swing.JFrame; public class runner { public static void main(String[] args){ int count; count=1; room0 room0var= new room0(); room0var.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); room0var.setSize(640, 480); room0var.setVisible(true); while (count>=0){ room0var.goAround(); } } }
General_Mob.java:Java Code://imports import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class room0 extends JFrame { private JLabel mob1Coords; //declare mobCoords private General_Mob mob1; //declare mob1 public room0(){ super("Room 0"); //set the title mob1= new General_Mob(); //create a new instance of General_Mob setLayout(new FlowLayout()); mob1Coords= new JLabel(" X: " + mob1.getX() + " Y: " + mob1.getY() + " Z: " + mob1.getZ()); //declares the JLabel add(mob1Coords); } public void goAround(){ mob1.goAround(); //tells mob1 to "do its thing". mob1Coords.setText(" X: " + mob1.getX() + " Y: " + mob1.getY() + " Z: " + mob1.getZ() + " Speed: " + mob1.getSpeed() + " Hspeed: " + mob1.getHspeed() + " Vspeed: " + mob1.getVspeed()); } }
PS: I've taken the liberty to heavily comment the code. Is this good practice, or is it overdoing it?Java Code://imports import java.lang.Math; import org.lwjgl.input.Keyboard; public class General_Mob { //declare variables. I like to declare them all at the start so I know where the declaration is. private double x, y, z, vspeed, hspeed, zspeed, friction, speed, direction, pidir; private int xround, yround, zround; //The constructor; when the mob is created, this is executed. It's kind of like the "create" event in Game Maker. public General_Mob() { x=0; y=0; vspeed=0; hspeed=0; friction=1; direction=90; speed=50000; } public void goAround(){ //This method is executed every time //First things first: Update the mob's position, speed, and friction. crunchPhysics(); } public void crunchPhysics(){ crunchFriction();//Slow the mob down based on its friction crunchDirection();//compute the hspeed and vspeed of the mob. //Move the mob based on its speed. x += hspeed; y += vspeed; z += zspeed; roundCoords();//Round the positions to integers so that they can be drawn on the screen. After all, how can you draw something that's .01 pixels? } /* * * * * * * * * * This comment is a separator. * * * * * * * * * */ //The following methods are used by other objects to access private variables. public int getX(){ return xround; } public int getY(){ return yround; } public int getZ(){ return zround; } public double getSpeed(){ return speed; } public double getHspeed(){ return hspeed; } public double getVspeed(){ return vspeed; } //The following methods act as shortcuts. They serve to make the code cleaner and easier to understand. private void crunchDirection(){ //Convert "direction" and "speed" to hspeed and vspeed. Credit for the algorithm goes to Yoyo Games. Hopefully, they didn't patent it or anything :P pidir=Math.PI * (direction / 180) * speed; hspeed=Math.cos(pidir); vspeed=Math.sin(pidir) * -1; } private void crunchFriction(){ //Slow the mob down based on its friction if (speed>0){ speed-=friction; } if (speed<0){ speed+=friction; } //if the speed is close enough to zero, then just set it to zero. if (Math.abs(friction) >= Math.abs(speed)){ speed=0; } } private void roundCoords(){ //rounds the coordinates of the mob to the nearest whole number. This is for graphical purposes only. xround=(int) Math.rint(x); yround=(int) Math.rint(y); zround=(int) Math.rint(z); } }Last edited by falconfetus8; 12-11-2011 at 04:21 AM. Reason: apparently there is no "spoiler" tag on this forum >.>
- 12-11-2011, 05:30 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: So apparently sin((pi * 0) / 180)) * 0 * -1 = 1
Is where your problem lies. When the speed reaches zero you performJava Code:pidir=Math.PI * (direction / 180) * speed; hspeed=Math.cos(pidir);
Which equals 0, you then apply cos to this result. cos(0) results in a 1. I'm guessing you want speed of 0, to result in a 0. Change how you perform the calculation.Java Code:pi * (direction / 180) * 0
I hope this makes sense to you, if not, let me know.Java Code:numberAsRadians = direction * (Math.PI / 180); value = cos ( numberAsRadians ); hspeed = value * speed;
As far as commenting goes, you don't want to go too overboard. The main point of comments is to improve readability of the code and help others understand what is being done. Descriptive names and brief comments should probably be preferred.
Similar Threads
-
KeyListener ignores key events despite apparently having focus
By jfio in forum AWT / SwingReplies: 4Last Post: 10-30-2011, 06:52 AM -
simple array processing(apparently!)
By stateofyouboi in forum New To JavaReplies: 2Last Post: 02-05-2011, 05:17 PM -
instruction apparently not executed
By rippon in forum AWT / SwingReplies: 4Last Post: 11-30-2010, 01:30 AM -
The field "name" doesnt apparently exsist :/
By Addez in forum New To JavaReplies: 6Last Post: 10-22-2010, 11:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks