Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (780) Games in Android Showcase (233) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 player movement  (Read 4904 times) 0 Members and 1 Guest are viewing this topic.
frulica

Senior Newbie

 « Posted 2005-11-01 11:10:41 »

i'm working on my first project(street football) and, of course, i got a problem with player movement..
first i did only 8 directions (up, down, left, right and diagonal) and it was working great, but now i decided i wanna go with some more realistic player movement so what i wanna do is make player move relative to its current direction

so, forward should always move player in his current direction, backward should move in oposite direction( but player is still faced forward). left and right should turn the player a bit by bit from his current direction (eg. buttons up and right would just make player go in circles).
i remember seing that kind of player movement in "chrimsonland" if anyone knows what im talking about.
any help is appreciated!

frulica

Senior Newbie

 « Reply #1 - Posted 2005-11-01 11:30:30 »

omg... good thing i asked this, last night i lost like 1,5 hours on this and another half today, but i got it now :\
sorry for the spam!
JAW

Senior Devvie

Medals: 2

 « Reply #2 - Posted 2005-11-01 11:40:09 »

Well I would use:
float direction
which handles the facing of your char
float velocity
which handles the current speed

and to help you
float xvel
float yvel
which handle the per frame position change for the given direction and velocity

With a given dir and vel, you compute the change in the x and y dimension using sin and cos. Each time, either vel or dir changes, you
need to recompute the xvel and yvel.

So something like direction 0 and speed 10 would give you 10 pixels up and 0 pixels to the side, while direction 45 (degrees) would give you around 6 xvel and 6 yvel.

I dont know, if this helps or what your solution is like, I dont know if you can even understand what I mean, I just dont get the right english words

-JAW
 Games published by our own members! Check 'em out!
frulica

Senior Newbie

 « Reply #3 - Posted 2005-11-01 14:23:04 »

actually your solution  was better, thanks!
nardev

Senior Newbie

 « Reply #4 - Posted 2005-11-11 18:18:00 »

for the lazy cats...

public void moveForward(){
x_position = x_position - ship_speed*Timer.getTimer().getElapsedTime()*(float)Math.sin((double)ship_turn*(Math.PI/180));//move the ship in the correct direction (amount for x)
y_position = y_position + ship_speed*Timer.getTimer().getElapsedTime()*(float)Math.cos((double)ship_turn*(Math.PI/180));//move the ship in the correct direction (amount for y)
}

public void moveBackward(){
x_position = x_position + ship_speed*0.5f*Timer.getTimer().getElapsedTime()*(float)Math.sin((double)ship_turn*(Math.PI/180));//move the ship in the correct direction (amount for x)
y_position = y_position - ship_speed*0.5f*Timer.getTimer().getElapsedTime()*(float)Math.cos((double)ship_turn*(Math.PI/180));//move the ship in the correct direction (amount for y)
}

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Jeff

JGO Coder

Got any cats?

 « Reply #5 - Posted 2005-11-13 06:14:12 »

If you use the built in sin() and cos() calls make sure you profile your app and take a good lookat their cost.

Many modern desktop processords have intrinsic trig functions but when you get into embedded processors this is generally not true and they can be very expensive to calculate.  The standard game solution to this has traditionally been a trig look-up table with linear interpolation between the data points.  You can get away with as little as a single table that goes from 0 -45 degrees and get all of your sin and cos out of it for a full 360 degrees if your a teensy bit clever.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
nardev

Senior Newbie

 « Reply #6 - Posted 2005-11-13 10:05:35 »

If you use the built in sin() and cos() calls make sure you profile your app and take a good lookat their cost.

Many modern desktop processords have intrinsic trig functions but when you get into embedded processors this is generally not true and they can be very expensive to calculate.  The standard game solution to this has traditionally been a trig look-up table with linear interpolation between the data points.  You can get away with as little as a single table that goes from 0 -45 degrees and get all of your sin and cos out of it for a full 360 degrees if your a teensy bit clever.

even for todays computers?
What's an embedded processor?
How do you profile your application?
What is meant by linear "interpolation" in the context used here?
When you say a trig-lookup table do you mean some sort of an array of the values of sin, cos precalculated?

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

 « Reply #7 - Posted 2005-11-13 21:24:08 »

even for todays computers?
What's an embedded processor?
How do you profile your application?
What is meant by linear "interpolation" in the context used here?
When you say a trig-lookup table do you mean some sort of an array of the values of sin, cos precalculated?

embedded processor I assume is referring to those on devices such as phones, PDAs, and consoles.

You profile your application with a profiler.  Google for it.

Linear interpolation means what it always means, the context here isn't strange.  You need to find the sine of 3.5 degrees but your table only has values for 3 degrees and 4 degrees, so you look up both the sine of 3 degrees and the sine of 4 degrees and then pick a value that is half way between the two as your estimate for the sine of 3.5 degrees.

Yes the trig lookup table would be an array of floats or doubles.  A lookup table can be used for any expensive function and should be in your aresnal of optimization techniques.

Jeff

JGO Coder

Got any cats?

 « Reply #8 - Posted 2005-11-13 21:38:33 »

All great questions! Lets start at the top...

Quote
even for todays computers?

Most of today's general purpose computers (laptops and desktops) have intrinsic trigonometry in their processors.  Having said that, there are some issues with x86's trig and Java which I'll hold til the end 'cause it complciates the picture slightly.

However today's computers are in more then just desktops and laptops, they are everywhere.  In game consoles, in cell phones, in your pocket, in your set top box, your NAT gateway, and so on.  Which leads nciely to the next question...

Quote
What's an embedded processor?

An embedded processor is one that is intended for use in an embedded application.  That doesn't help much as a definition, I know, but ist a starting point.  Embedded applications are all those devices that aren't desktops and laptops.  They have very differnt needs.  A desktop, and to a lesser degree a laptop, is almost all about processing speed.  These other areas though have other over-riding needs such as low power consumption or low processor cost.  As a result, embedded processors are generally not as fast or as capable as the general purpose processors.

Most processors intended for embedded work do *not* have intrinsic trig and do it all in software.

Quote
How do you profile your application?

With a profiler!  A profiler is a program that watches your program run and gives you a detailed report on what parts are using what percentage of the processing time, what parts are generating what memory usage, etc.  A good profiler is an essential Java performance coding tool.  Both Eclipse and Netbeans now have free Profilers you can download that plug right into the IDE.  The Netbeans one is quite good, almost as good as some very expensive commercial tools I've used in the past.  I haven't tried the Eclipse one yet so I can't comment on it..

You can find a whole chapter on profilign in Steve and my performacne book.  The book is a bit old and some of the "tactical" stuff is a bit outdated but the "strategy" section is still quite appropriate to today's VMs.  (As are msot of the tctics.  The biggestchnage is that we no longer have to be as afraid of creating garbage collection runs as we used to be.)

It's all on line here:

http://java.sun.com/docs/books/performance

The profiler chapter is here:

http://java.sun.com/docs/books/performance/1st_edition/html/JPMeasurement.fm.html#30000

I  strongly recommend yo uread the entire measurmeent section though, not just the Profiler section.  People new to Java often get themselves in trouble trying to write "microbenchmarks" and the section talks about that, too.
Quote

Quote
What is meant by linear "interpolation" in the context used here?
When you say a trig-lookup table do you mean some sort of an array of the values of sin, cos precalculated?

Okay, Il lanswer these together and yes, yo uare on the right tarck here, you create an array of precaculated sin values (or cos, either will work.)
You only need one because Sin and Cos are the same with a phase shift (+/- 90 degrees).

On top of that, they repeat every 180 degrees, so you can do a mod(180) and only have 180 dgrees in your table.
You can actually get fancier then that by rcognizing that the table refelcts around 90 degrees so IF180>=N>=90, sin(n) == sin(180-n), tha reduces your table size to 90 degrees.  Finally, it reflects again around 45, so IF 90>=N>=45, sin(n) == sin (90-n)

These tables can be a useful aid in gorkking all this: http://www.math2.org/math/trig/tables.htm

The end result of all this, though, is all you need is a 45 degree table, some IFs and some addition or subtraction (and one mod) to get the Sin of any number.

SO lets say you build a table with 45 entries, one for each degree.  How do you get the values inj betweem, say sin (1.75).  The answer is interpolation. In general a simple linear interpolation will suffice for the accuracy required for most games.  A linear interpolation betwene two points is just a weighted average,

So lets say our SIn array is called "sinarray", sin(1.75) ==  ((2-1.75)*sinarray[2])+((1.75-1)*sinarray[1]))

or in the general case, assuming a 45 degree table with 45 entries and 45>=n>=0

long highval = Math.ceiling(n);
long lowval = Math.floor(n);
long result = (((highval-n)*sinarray[highval)+((n-lowval)*sinarray[lowval]))

Note that I am an old guy and think in "degrees".  Java's trig is actually in radians so if you want to mimic it you will have to do conversions
or arrange your table in radians rather then degrees.

NOW I said Java compilcates things.  The long explaination is here:

http://blogs.sun.com/roller/page/jag?entry=transcendental_meditation

But the short hand is that, for angles under -45 or over 45 degrees, at the accuracy that Java by definition must calculate flaoting point math, the intrinsic trig in the x86 processor is broken.  For that reason Java defaults to a software implementastion outside of that range BUT using the reflection tricks I just showed you, you should never have to leave that range

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
nardev

Senior Newbie

 « Reply #9 - Posted 2005-11-14 08:35:36 »

beautiful explanation

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
 Games published by our own members! Check 'em out!
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #10 - Posted 2005-11-14 18:15:15 »

Calling cos/sin 2-3 times for your single player's movement is no big deal.  It's when you have 100 characters all moving with 2-3 sin/cos calls that you really start to care about that.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
frulica

Senior Newbie

 « Reply #11 - Posted 2005-11-15 09:11:15 »

all help much appreciated... i do plan to have only 7 players in this game, but the next one... who knows
thanks!
Jeff

JGO Coder

Got any cats?

 « Reply #12 - Posted 2005-11-15 19:23:56 »

all help much appreciated... i do plan to have only 7 players in this game, but the next one... who knows
thanks!

Thats cool as long as the players are the only thinsg moving on screen.

But keep in mind that if you have other animated objects on the screen they too will need to do trig.  (Imagine Robotron for a minute.  One player, LOTS of moving objects.)

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
 ignore  |  Print

 hadezbladez (695 views) 2018-11-16 13:46:03 hadezbladez (344 views) 2018-11-16 13:41:33 hadezbladez (677 views) 2018-11-16 13:35:35 hadezbladez (168 views) 2018-11-16 13:32:03 EgonOlsen (2362 views) 2018-06-10 19:43:48 EgonOlsen (2474 views) 2018-06-10 19:43:44 EgonOlsen (1463 views) 2018-06-10 19:43:20 DesertCoockie (2128 views) 2018-05-13 18:23:11 nelsongames (1904 views) 2018-04-24 18:15:36 nelsongames (2571 views) 2018-04-24 18:14:32
 SkyAphid 19x orange451 13x VaTTeRGeR 10x gouessej 10x Zemlaynin 7x dime26 6x philfrei 6x ral0r2 5x KaiHH 4x CJC 4x cygnus 3x Pilvinen 3x torbuntu 3x WinterGuardian 2x Shatterhand 2x Brynn 1x
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org