×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    parabolic movement ....

    hi friends

    i m doing a game in that a character willl throw a stone from a side to other side...consider from left to right.

    when the character throws it it should reach other side with a realistic feeling...i m trying to use some ophysics concepts in my code...each and every time he /she throw the stone it should go randomly...

    can anybody help me to sort this out....

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    Hi custom_made,

    to get a "realistic" behaviour, you could try these steps:

    * define some properties to store stone position (x, y) and speed (horizontal and vertical)

    * when launching a new stone, initialize the stone position to some default value, and use a Random object to initialize the horizontal speed. You can assume initial vertical speed as zero, or initialize it as for the horizontal one, so the stone will not be initially thrown horizontally.

    * after each step (tipically a repaint() request) you should modify both the stone speed and position:

    ** the vertical speed will change as for the gravity force effect (so you'll tipically add to it a constant value)
    ** the horizontal speed could be constant (if you assume no air resistance/wind/other forces) or you can gradually increase/decrease it at each step
    ** finally, you can calculate the new stone position by adding the single speed elements

    This way, you should be able to have a quite random and realistic feeling for your stones.

    Here's some basic sample code:
    Code:
    import java.util.Random;
    
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    
    public class RandomStoneCanvas extends Canvas implements Runnable
    {
    	int xPosition = 0;
    	int yPosition = 0;
    	int xSpeed = 0;
    	int ySpeed = 0;
    	int gravity = 1;
    	Random rand = null;
    	
    	int stageWidth = 0;
    	int stageHeight = 0;
    	
    	public RandomStoneCanvas()
    	{
    		rand = new Random();
    		
    		stageWidth = getWidth();
    		stageHeight = getHeight();
    		
    		newStone();
    		
    		new Thread(this).start();
    	}
    	public void newStone()
    	{
    		xSpeed = 5 + rand.nextInt(20);
    		ySpeed = 0;
    		
    		xPosition = 0;
    		yPosition = 0;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setColor(0xffffff);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		g.setColor(0x000000);
    		g.fillRect(xPosition - 2, yPosition - 2, 4, 4);
    	}
    	void moveStone()
    	{
    		ySpeed += gravity;
    		
    		xPosition += xSpeed;
    		yPosition += ySpeed;
    		
    		if(xPosition > stageWidth || yPosition > stageHeight)
    		{
    			newStone();
    		}
    	}
    	public void run()
    	{
    		while(true)
    		{
    			try
    			{
    				repaint();
    				
    				moveStone();
    				
    				synchronized(this)
    				{
    					wait(100L);
    				}
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    If you have any doubts about it, feel free to ask :)

    Pit
    Last edited by jappit; 2008-08-19 at 13:35.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Re: parabolic movement ....

    Quote Originally Posted by jappit View Post
    Hi custom_made,

    to get a "realistic" behaviour, you could try these steps:

    * define some properties to store stone position (x, y) and speed (horizontal and vertical)

    * when launching a new stone, initialize the stone position to some default value, and use a Random object to initialize the horizontal speed. You can assume initial vertical speed as zero, or initialize it as for the horizontal one, so the stone will not be initially thrown horizontally.

    * after each step (tipically a repaint() request) you should modify both the stone speed and position:

    ** the vertical speed will change as for the gravity force effect (so you'll tipically add to it a constant value)
    ** the horizontal speed could be constant (if you assume no air resistance/wind/other forces) or you can gradually increase/decrease it at each step
    ** finally, you can calculate the new stone position by adding the single speed elements

    This way, you should be able to have a quite random and realistic feeling for your stones.

    Here's some basic sample code:
    Code:
    import java.util.Random;
    
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    
    public class RandomStoneCanvas extends Canvas implements Runnable
    {
    	int xPosition = 0;
    	int yPosition = 0;
    	int xSpeed = 0;
    	int ySpeed = 0;
    	int gravity = 1;
    	Random rand = null;
    	
    	int stageWidth = 0;
    	int stageHeight = 0;
    	
    	public RandomStoneCanvas()
    	{
    		rand = new Random();
    		
    		stageWidth = getWidth();
    		stageHeight = getHeight();
    		
    		newStone();
    		
    		new Thread(this).start();
    	}
    	public void newStone()
    	{
    		xSpeed = 5 + rand.nextInt(20);
    		ySpeed = 0;
    		
    		xPosition = 0;
    		yPosition = 0;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setColor(0xffffff);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		g.setColor(0x000000);
    		g.fillRect(xPosition - 2, yPosition - 2, 4, 4);
    	}
    	void moveStone()
    	{
    		ySpeed += gravity;
    		
    		xPosition += xSpeed;
    		yPosition += ySpeed;
    		
    		if(xPosition > stageWidth || yPosition > stageHeight)
    		{
    			newStone();
    		}
    	}
    	public void run()
    	{
    		while(true)
    		{
    			try
    			{
    				repaint();
    				
    				moveStone();
    				
    				synchronized(this)
    				{
    					wait(100L);
    				}
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    If you have any doubts about it, feel free to ask :)

    Pit
    hi jappit

    thanks for suggestion and the sample code ...i didnt try dis ..i willl ry it today its self...thanks alot an...

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Re: parabolic movement ....

    Quote Originally Posted by jappit View Post
    Hi custom_made,

    to get a "realistic" behaviour, you could try these steps:

    * define some properties to store stone position (x, y) and speed (horizontal and vertical)

    * when launching a new stone, initialize the stone position to some default value, and use a Random object to initialize the horizontal speed. You can assume initial vertical speed as zero, or initialize it as for the horizontal one, so the stone will not be initially thrown horizontally.

    * after each step (tipically a repaint() request) you should modify both the stone speed and position:

    ** the vertical speed will change as for the gravity force effect (so you'll tipically add to it a constant value)
    ** the horizontal speed could be constant (if you assume no air resistance/wind/other forces) or you can gradually increase/decrease it at each step
    ** finally, you can calculate the new stone position by adding the single speed elements

    This way, you should be able to have a quite random and realistic feeling for your stones.

    Here's some basic sample code:
    Code:
    import java.util.Random;
    
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    
    public class RandomStoneCanvas extends Canvas implements Runnable
    {
    	int xPosition = 0;
    	int yPosition = 0;
    	int xSpeed = 0;
    	int ySpeed = 0;
    	int gravity = 1;
    	Random rand = null;
    	
    	int stageWidth = 0;
    	int stageHeight = 0;
    	
    	public RandomStoneCanvas()
    	{
    		rand = new Random();
    		
    		stageWidth = getWidth();
    		stageHeight = getHeight();
    		
    		newStone();
    		
    		new Thread(this).start();
    	}
    	public void newStone()
    	{
    		xSpeed = 5 + rand.nextInt(20);
    		ySpeed = 0;
    		
    		xPosition = 0;
    		yPosition = 0;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setColor(0xffffff);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		g.setColor(0x000000);
    		g.fillRect(xPosition - 2, yPosition - 2, 4, 4);
    	}
    	void moveStone()
    	{
    		ySpeed += gravity;
    		
    		xPosition += xSpeed;
    		yPosition += ySpeed;
    		
    		if(xPosition > stageWidth || yPosition > stageHeight)
    		{
    			newStone();
    		}
    	}
    	public void run()
    	{
    		while(true)
    		{
    			try
    			{
    				repaint();
    				
    				moveStone();
    				
    				synchronized(this)
    				{
    					wait(100L);
    				}
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    If you have any doubts about it, feel free to ask :)

    Pit

    hi jappit

    i checked your code its working fantastic...it will be very helpfulll for me....now i got some idea...

    in my gane know .the person is throwing the stone upwardly with a force..wen the stone reaches a certain height or the stone lose its force it should come down...so it will create a parabola..i want that kind of logic..if have any sample or any suggestion pls share with me..

    thanks in advance
    custom_made

  5. #5
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    Hi custom_made,

    you can get the desired behaviour just initializing ySpeed to a negative value. Modifying the newStone() method as follows should work:

    Code:
    public void newStone()
    {
    	xSpeed = 5 + rand.nextInt(20);
    	ySpeed = - (3 + rand.nextInt(5));
    		
    	xPosition = 0;
    	yPosition = 50;
    }
    Pit

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Re: parabolic movement ....

    Quote Originally Posted by jappit View Post
    Hi custom_made,

    you can get the desired behaviour just initializing ySpeed to a negative value. Modifying the newStone() method as follows should work:

    Code:
    public void newStone()
    {
    	xSpeed = 5 + rand.nextInt(20);
    	ySpeed = - (3 + rand.nextInt(5));
    		
    	xPosition = 0;
    	yPosition = 50;
    }
    Pit
    hi jappit

    it rocks man....its more than enough for me dear...thanks a lot... varying ySpeed i can get the projectile motion..i m expectng your help in future tooo...

    custom_made

  7. #7
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: parabolic movement ....

    Hey I just read this thread, and its been very helpful to me! i've been puzzling over how to do something like that for weeks! so if i wanted to incorporate that into making a sprite jump, how would i go about it? would i just have a method that decreases the sprites y position and maybe another method that would put gravity into effect? and call both when jumping? i dont think i've explained myself correctly but i hope you get the general idea..

  8. #8
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    Hi rooossone,

    your guess is right: each time your sprite jumps, you simply need to set an initial ySpeed different than zero, and then decrease it at each step as for the gravity force effect, until the sprite itself reaches the ground again. At that point, you'll newly set the ySpeed to zero.

    Hope it helps,
    Pit

  9. #9
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: parabolic movement ....

    Thanks Jappit,

    So i could leave the gravity pulling down method in the loop and call it the whole game right? which would mean falling from platforms would be taken car of too.. how would i stop the sprite from jumping in mid air though? i guess its just a y position check in the sprites jump method right? alot simpler than i thought really!

    btw Jappit i really like your site!

  10. #10
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    Hi rooossone,

    yes, you should increase ySpeed as for the gravity force effect each time the sprite is not on a platform/floor: so usually when it jumps, and when it's falling from some platforms.

    To do this, you could for example use a boolean flag, for example:
    Code:
    void moveSprite()
    {
    	if(yourSprite.isJumping || yourSprite.isFalling)
    	{
    		yourSprite.ySpeed += gravity;
    	}
    }
    In the same way (so, checking some boolean flags) you can also avoid the user to jump when it's not on some floor.

    Pit

    PS: thanks for your appreciation :)

  11. #11
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: parabolic movement ....

    wow! thats alot simpler than i thought! so in the jump method i could just set the isJumping flag to true yeah? how would i set the isFalling flag when the sprite is not on 'land' though? and i guess i could use an enumerator instead of the flags or is that not recommended?
    Made With Caffiene And Sleepless Nights

  12. #12
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    About falling: there is not an unique way to check if your sprite is on a land, since it depends a lot on the model you've choosen to represent lands/floors themselves. Generally speaking you should check if, corresponding to sprite (x, y) position, there is some floor or not.

    About flags usage: mine is a simple example, you can of course use any way you prefer to manage the sprite state. Using and accessing simple properties (so, without accessors methods) is usually faster (and so recommended) in Java ME, but there could be situations where you need to do some computations to retrieve the sprite state itself, and so a method should be used.

    Please tell me if something in my explanation is not clear enough :)

    Pit

  13. #13
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: parabolic movement ....

    so in essence i would need a method for jumping, a method to check for falling(or land) and another to affect gravitational pull? would i put the land/falling method inside the larger 'collision check' method? or is it substatial enough to warrant its own method alongside the collision checking? would i just keep that to other game objects and the such and leavve the platform land checks seperate? cheers for all the help! its starting to make me think differently!
    Made With Caffiene And Sleepless Nights

  14. #14
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: parabolic movement ....

    Hi rooossone,

    there's more than a single way to implement game logic, so the best thing you can do is to experiment by your own and see if/how it performs.

    Anyway, you're correct on the main points of moving a Sprite, that usually are collision checking and external forces/speed calculation (so, it's fine to keep these 2 parts a bit separated).

    I don't have any related sample code right now, but if I find something useful I'll post it immediatly ;)

    Pit

  15. #15
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: parabolic movement ....

    THanks man you've been a lot of help! ive managed to implement the gravity feature into my concept game and have also implemented the jump feature, ive come unstuck here though because when i jump i jump verticaly and lose all x axis movement. i think its down to my keystate polling... i will try to nest the jump key polling within both direction polls and see how i fair there, do you know if i can check for multiple keypresses in me?

    on a side note when i get this game up together i shall create a tutorial to go with it, nothing like sharing some knowledge! i know theres alot out there already but it doesnt hurt from another angle!
    Made With Caffiene And Sleepless Nights

Similar Threads

  1. Parabolic effect
    By sssmails in forum Mobile Java General
    Replies: 4
    Last Post: 2008-09-05, 09:31
  2. movement of object without using timer
    By amrish20 in forum Symbian Media (Closed)
    Replies: 4
    Last Post: 2008-06-11, 07:10
  3. pointer movement on the screen
    By juliaf in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2004-12-23, 08:07
  4. N6600 diagonal movement
    By DwightCheu in forum Mobile Java General
    Replies: 1
    Last Post: 2004-10-07, 03:44
  5. diagonal movement on n-gage
    By lucerne2 in forum Mobile Java General
    Replies: 2
    Last Post: 2004-03-26, 20:43

Posting Permissions

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