Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 2111 times)
0 Members and 1 Guest are viewing this topic.
Offline orogamo

Senior Member


Medals: 5
Projects: 1



« 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

JGO Ninja


Medals: 66



« 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

JGO Knight


Medals: 14
Projects: 5


iDream


« 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();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Varkas

JGO Knight


Medals: 14
Projects: 5


iDream


« 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();
Offline Roquen

JGO Ninja


Medals: 66



« 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: 437
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Roquen

JGO Ninja


Medals: 66



« Reply #36 - Posted 2013-01-03 16:19:37 »

Different issue.

SEE: http://openjdk.java.net/jeps/169
Offline sproingie
« 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

Junior Member


Medals: 5



« 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

JGO Ninja


Medals: 66



« 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!
Play the free demo of Revenge of the Titans!
Offline wreed12345
« 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 Knight


Medals: 23
Projects: 2


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
« 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>
Online HeroesGraveDev

JGO Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


« 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

JGO Knight


Medals: 14
Projects: 5


iDream


« 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();
Pages: 1 [2]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (34 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (116 views)
2013-05-14 15:56:26

kutucuk (140 views)
2013-05-12 17:10:36

kutucuk (140 views)
2013-05-12 15:36:09

UnluckyDevil (149 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.095 seconds with 21 queries.