Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By sunde887

Thread: So apparently sin((pi * 0) / 180)) * 0 * -1 = 1

  1. #1
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default 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:
    Java Code:
     hspeed = cos( pi * direction / 180 ) * speed
    vspeed = -sin( pi * direction / 180 ) * speed
    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.

    Here is my source code. I must warn you, though: it is very lengthy.

    Java Code:
    import javax.swing.JFrame;
    public class runner {
    	public static void main(String[] args){
    		int count;
    		room0 room0var= new room0();
    		room0var.setSize(640, 480);
    		while (count>=0){
    Java Code:
    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
    	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());
    Java Code:
    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() {
    	public void goAround(){ //This method is executed every time 
    		//First things first: Update the mob's position, speed, and friction.
    	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;
    		vspeed=Math.sin(pidir) * -1;
    	private void crunchFriction(){ //Slow the mob down based on its friction
    		if (speed>0){
    		if (speed<0){
    		//if the speed is close enough to zero, then just set it to zero.
    		if (Math.abs(friction) >= Math.abs(speed)){
    	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);
    PS: I've taken the liberty to heavily comment the code. Is this good practice, or is it overdoing it?
    Last edited by falconfetus8; 12-11-2011 at 04:21 AM. Reason: apparently there is no "spoiler" tag on this forum >.>

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power

    Default Re: So apparently sin((pi * 0) / 180)) * 0 * -1 = 1

    Java Code:
    pidir=Math.PI * (direction / 180) * speed;
    Is where your problem lies. When the speed reaches zero you perform
    Java Code:
    pi * (direction / 180) * 0
    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:
    numberAsRadians = direction * (Math.PI / 180);
    value = cos ( numberAsRadians );
    hspeed = value * speed;
    I hope this makes sense to you, if not, let me know.

    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.
    Fubarable likes this.

Similar Threads

  1. Replies: 4
    Last Post: 10-30-2011, 06:52 AM
  2. simple array processing(apparently!)
    By stateofyouboi in forum New To Java
    Replies: 2
    Last Post: 02-05-2011, 05:17 PM
  3. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 01:30 AM
  4. The field "name" doesnt apparently exsist :/
    By Addez in forum New To Java
    Replies: 6
    Last Post: 10-22-2010, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts