Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Need help moving projectiles with math...  (Read 4518 times)
0 Members and 1 Guest are viewing this topic.
Offline orogamo
« Reply #30 - Posted 2013-01-03 14:57:16 »

If the object has a rotation and speed, do this:

1  
2  
3  
4  
5  
6  
7  
double x = 0, y = 0;
x += speed * (float) Math.sin(RAD(rotation));
y -= speed * (float) Math.cos(RAD(rotation));

public static double RAD(double VAL) {
   return ((VAL * 3.14) / 180);
}


EDIT:
if the object moves the wrong way, switch the + & - signs.
Offline Roquen
« Reply #31 - Posted 2013-01-03 15:10:13 »

@Varkas - yes I'm sure.  As I'm fond of say, you can't make generalizations about computer science techniques.  And as I said, java doesn't have real immutables, so the only reason to go that route is for some design purpose.  A pure functional language can leverage immutability, java never can (at least until it adds them).

@orogamo - see: complex multiply Smiley
Offline Varkas
« Reply #32 - Posted 2013-01-03 15:25:07 »

And as I said, java doesn't have real immutables, so the only reason to go that route is for some design purpose.

I don't understand your sentance, I'm afraid. You say, Java doesn't have immutables. The Java String class is considered to be immutable though, and the page that I've linked talks about immutable objects in Java.

How does that go together?

I understand that for games performance considerations may lead to the decision not to use immutable objects - but I still don't understand why you say Java does not have immutable objects?

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Varkas
« Reply #33 - Posted 2013-01-03 15:33:08 »

Ok so i felt like i should know how to do this using the formula y-y1 = m(x-x1)

Movement formulas:

p = projectile position
v0 = initial projectile speed
g = gravitational force (downwards acceleration)
t = the time which the projectile has travelled already

1) p = v*t

2) v = v0 + g*t

3) p = t*v0 + g*t*t

if you set gravity to 0, you get

4) p = t * v0

which was suggested before.

If you want to consider air resistance, you need to reduce v over time.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Roquen
« Reply #34 - Posted 2013-01-03 15:50:15 »

Java the high level language has 'final' which disallows direct modification once initialized.  It also has (for instance) reflection which allows these final values to be modified at any time during runtime.  Since the JVM is a dynamic language that cannot know all code that will run, it cannot do treat final values as immutable.  (The decompilation framework does deal with some guesswork of this nature, but not for fields).  Off and on there's been talk of adding real immutables, but it hasn't happened yet.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #35 - Posted 2013-01-03 16:08:37 »

Java the high level language has 'final' which disallows direct modification once initialized.  It also has (for instance) reflection which allows these final values to be modified at any time during runtime.  Since the JVM is a dynamic language that cannot know all code that will run, it cannot do treat final values as immutable.  (The decompilation framework does deal with some guesswork of this nature, but not for fields).  Off and on there's been talk of adding real immutables, but it hasn't happened yet.
HotSpot already treats final variables differently, and changing their value through reflection results in 'undefined behavior', in some cases, where the actual memory location is updated, but the old value may be 'cached' in certain expressions.

1  
2  
3  
4  
5  
final int one = 1;

public int addOne(int val) {
   return val + one; // might be compiled to value+1 (compiled by HotSpot, not javac)
}


IIRC erikd ran into this years ago.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #36 - Posted 2013-01-03 16:19:37 »

Different issue.

SEE: http://openjdk.java.net/jeps/169
Offline sproingie

JGO Kernel


Medals: 201



« Reply #37 - Posted 2013-01-03 18:00:03 »

That's utter nonsense.  Stop listening to whomever told you that.  Beside java doesn't even have immutable object (no...it really doesn't).  The widespread usefulness of immutables took a huge blow in the 80s with the introduction of SSA form, but even before that this statement would still be nonsense.

SSA, Single Static Assignment, would seem to imply that it makes everything immutable.  Seems compilers have moved in the direction of immutable data even for modeling state.

Look, in terms of theory, referential transparency makes everything so much simpler.  Pure functions can be memoized without fear.  Evaluation can be done in any order and parallelized trivially.  Testing is a matter of plugging in inputs and checking outputs, no setup needed.  The list goes on and on. 

The fact is, we still use mutable vectors and matrices because the garbage collector does not run at zero cost.  State might be useful for modeling other problems, but if not for the gc problem, most of us would rather be using actual math to describe math problems, not coaxing an automaton into simulating it.
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #38 - Posted 2013-01-03 18:22:31 »

Just thought I'd mention that from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects ...
That's utter nonsense.  Stop listening to whomever told you that.  Beside java doesn't even have immutable object (no...it really doesn't).  The widespread usefulness of immutables took a huge blow in the 80s with the introduction of SSA form, but even before that this statement would still be nonsense.
SSA is just a technique used by compilers to optimize code. It has nothing to do with what I was saying. And it doesn't matter if Java has a keyword built in for defining immutable classes. You can still make objects de facto immutable.
I'm even gonna go a step further with my claims and say that making vectors mutable is a form of premature optimization and nothing else. Of course in game/graphic engines it may make sense to do that but unless performance issues force me to optimize my code, I'm gonna stick with immutablity.
Offline Roquen
« Reply #39 - Posted 2013-01-03 21:54:26 »

The point of SSA (indeed a backend compiler technique) is that transforms and analysis that were previously difficult to impossible for mutable values became possible and therefore made the argument for favoring languages and/or constructions that truly made usage of immutability less (or even much less) strong.  I have a reasonably firm grasp of issues related to mutable vs. immutable values (notice I'm talking about values rather than the more abstract notion of types).   Furthermore temporal coherency of memory accesses sticks yet another dagger into the heart of the importance of immutability.  But there's about zero reason to push this discussion into language design, type theory, lambda calculus and modern-esqe memory architectures, because it's moot. Java doesn't currently support immutable types in the sense that we are talking about. Back to the root of this,  DrZoidberg stated that "from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects" to which I say is nonsense as you cannot make generalization of this kind, or at least worthwhile ones.  Besides computer science is a form of applied mathematics so really all types are mathematical and by this reasoning should be immutable.

Quote
I'm even gonna go a step further with my claims and say that making vectors mutable is a form of premature optimization and nothing else.
That's just silly.  This whole "premature optimization" garbage is really out of hand.  The important issue is opportunity cost.

Quote
Of course in game/graphic engines it may make sense to do that but unless performance issues force me to optimize my code, I'm gonna stick with immutablity
And, as I stated (or at least implied) there's not wrong with making the design choice of immutability (as long as it doesn't bite you in the butt), but that's a far cry from claiming that it's the "ordained true path" of computer science.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #40 - Posted 2013-01-03 22:10:42 »

@varkas
If i were to use one of them, since p is the position would I set p to an x or y value? or something different?

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #41 - Posted 2013-01-03 22:51:42 »

If the object has a rotation and speed, do this:

1  
2  
3  
4  
5  
6  
7  
double x = 0, y = 0;
x += speed * (float) Math.sin(RAD(rotation));
y -= speed * (float) Math.cos(RAD(rotation));
...
public static double RAD(double val) {
   return (val * 3.14) / 180;
}


EDIT:
if the object moves the wrong way, switch the + & - signs.

I think the cos and sin are backwards. I thought x always went with cos, and y with sin

"Experience is what you get when you did not get what you wanted"
Offline sproingie

JGO Kernel


Medals: 201



« Reply #42 - Posted 2013-01-03 23:12:50 »

The fact that compilers are able to optimize mutability better is still very orthogonal to the fact that referential transparency is simply easier to reason about, both for the computer and the user.  Application is just textual substitution anywhere you want it to be.

But on the other side of the coin, the other unfortunate fact is that for a lot of games, we're still up against the limits of how well the machine can perform with immutable data. Since java has no concept of immutable values beyond primitives and some really basic optimizations on String, and therefore isn't going to bypass garbage generation very often, we're stuck with a lot of in-place modifications for matrix and vector ops done in bulk.

</derail>
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #43 - Posted 2013-01-04 00:07:27 »

If the object has a rotation and speed, do this:

1  
2  
3  
4  
5  
6  
7  
double x = 0, y = 0;
x += speed * (float) Math.sin(RAD(rotation));
y -= speed * (float) Math.cos(RAD(rotation));
...
public static double RAD(double val) {
   return (val * 3.14) / 180;
}


EDIT:
if the object moves the wrong way, switch the + & - signs.

I think the cos and sin are backwards. I thought x always went with cos, and y with sin

If 0 degrees is pointing along the y-axis, then x is sin and y is cos. If 0 degrees is pointing along the x-axis, you are right. It all depends on what system you use.

Offline Varkas
« Reply #44 - Posted 2013-01-12 13:40:25 »

@varkas
If i were to use one of them, since p is the position would I set p to an x or y value? or something different?

The variables are vector types, in 2D space, they will be (x,y) pairs.

Untested code, using java.awt.Point:

1  
2  
3  
4  
5  
6  
7  
8  
Point p = new Point(startx, starty);
Point v = new Point(speedx, speedy);
double time = (currentTime - timeWhenFired) * timeLapseFactor;

// no gravity

double actualx = p.x + v.x * time;
double actualy = p.y + v.y * time;




if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!