Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 2D Inertia

Author
Message
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 23rd Jun 2008 21:18 Edited at: 6th Jul 2008 03:19
I'm quite sure I'm going the wrong way about this... but here's what I came up with. It only thrusts in one direction...



SOLVED


This now works. Thanks to everyone that helped, it was a great learning experience.

Timer works.



As you can see, I cleaned it up a little bit...

Any ideas on how to implement a max speed?

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 23rd Jun 2008 22:34 Edited at: 23rd Jun 2008 22:35
I don't know what's wrong with the code itself. I'll look at it when I have a bit less going on around me. I just wanted to point out a shortcut you can take in some of your code.

trueAngle = trueAngle - turnSpeed;

can be replaced by

trueAngel -= turnSpeed;

The same can be done with +, *, /, %, |, & and maybe others that don't come to mind immediately. Other statements you could have applied this to would be

velocityAngle += 0.25f;

velocity = velocity - (acceleration/2);

could have become

velocity -=(acceleration/2);

Note also that the parenz around acceleration/2 aren't absolutely necessary in this instance because the division operation binds more closely than the assignment operation. But putting them there helps to make the code and intent more readable. There's no runtime penalty for putting extra parenz where they aren't needed as long as it doesn't change the purpose of the code.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 09:15
Quote: "It only thrusts in one direction..."


which direction would this be?
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 24th Jun 2008 21:00
How else are you going to program a game of Lunar Lander (from orbit) and not crash every time?

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 02:04
Anyone have any ideas yet?

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Jul 2008 05:02
I'd use a 2 component vector to store the velocity of my ship, when I hold a directional key I'd increment the vector by 'sin(angle) * acceleration, cos(angle) * acceleration'(if it's in 3D(y is up) space that is, else flip sin/cos), the acceleration value would be timer-based of course. Then you just increment the ship position by this vector every frame(timer-based too).

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 08:22
dark coder, your method KIND of works, but only on the Y axis. It won't go anywhere left to right... and goes backwards.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Jul 2008 08:50
How so? when I say sin(blah),(<-- comma)cos(blah) I mean you use the sin to calculate the X axis' offset and cos for the Z axis. Thus getting 2D movement.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 09:38
This is 2D. No Z axis.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 09:39 Edited at: 4th Jul 2008 09:39
Another problem. With this code, the minimum acceleration is 1. Anything less doesn't work, doesn't even move.



Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 4th Jul 2008 17:54
Quote: "Another problem. With this code, the minimum acceleration is 1. Anything less doesn't work, doesn't even move."


You need to stop relying on the sprite telling you where it is. Since it needs to reside on a point, both the x and y values are always going to be integers. If you add something less than 1.0 to that position and tell the sprite to move to that (same) position, the next time you call dbSpriteX(int) it's going to give you the same answer. Even if xvelocity is greater than 1.0f, you're always going to be truncating the result of the addition to an integer.


Even if xvelocity or yvelocity are too small to make a change in the integer position on any given cycle, eventually xPos and yPos will grow into the next integer value but it will take more than one loop for it to show as movement.

Side note: I'd also use a bit of rounding by adding .5 to the value I pass to dbSprite() although I wouldn't use it in the assignment of xPos or yPos.

I also notice that you're calling dbSpriteAngle() through every iteration of the loop. Why not just maintain angle on your own and change

dbRotateSprite(1, angle-turnSpeed);

to

dbRotateSprite (1, angle-=turnSpeed);

and, of course, the same for when it turns the other way

dbRotateSprite (1, angle+=turnSpeed);


and don't call dbSpriteAngle(). This has the advantage of not needing to set angle with a call to the db functionality, which uses a few CPU cycles.

Here's a bit of C++ (and simply C) usefulness. You have expressions and you have side effects. a + b is an expression. c = a + b is a side effect, the side effect being the assignment of the value of the expression a + b
to the integer c.

c = a = b is rather like an expression with two side effects. The assignment operation is right to left. So the expression is like saying c = (a = b) a is assigned the value of b. But consider that the expression/side effect (a = b) also has the value of the assignment, in this case the value of b, which is subsequently assigned to c. Many times in my code I need to set a number of variables to zero. So, assuming I have the integers declared, I can do something like:

a = b = c = d = e = f = 0;

Which has the effect of setting all the variables noted to zero.

In your code, with the modifications I suggest,

dbSprite (1, xPos+=xvelocity, yPos+=yvelocity, ship);

takes the value of xPos and adds xvelocity to it. The assignment is the side effect. At the same time the value of the assignment (essentially the new value of xPos) is passed to the dbSprite() function because the value of the expression is the result of the assignment. The same holds true for the new value of yPos and is also applied in my example of manipulating angle.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 22:12
Thanks, Lilith. That all worked. But it still won't work on the X axis. It goes up and down, but not side to side.



Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 22:33 Edited at: 4th Jul 2008 22:34


Okay, now it goes in both axes.

But now there's the problem of it thrusting SIDEWAYS.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Jul 2008 22:56
When thrusting sideways just add 90 degrees to the angle when calculating the sin/cos, i.e. dbCos(angle + 90.0f) * acceleration; etc

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 4th Jul 2008 22:57
I feel so stupid. I need to go over my algebra book again.
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 4th Jul 2008 22:59


Still does the same thing, no matter what. I tried adding 90.0f, subtracting, adding 270.0f, but it still goes backwards or sideways.

Jaheco
16
Years of Service
User Offline
Joined: 30th Jun 2008
Location:
Posted: 4th Jul 2008 23:16
Well, the fastest way I can think to fix the problem is to do this:



Perhaps not ideal, but it'll work.

Which direction does your sprite point when you start off, by the way?
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 5th Jul 2008 04:10 Edited at: 5th Jul 2008 04:10
I'm not sure how you're determining the the speed but you can change the following



into



There's no need for the use of the intermediate storage in the result variable. It will just go away when the function exits. It might be viable to declare such a function as an inline, especially if you only call it once or twice.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 5th Jul 2008 11:32
Lilith, I'm getting the net speed out of the velocity, then I multiply it by 10 (may change?). This is to establish an easy to understand speed throughout the game.

This Light Fighter will go a top speed of 120 units, and a transport might go 40.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 5th Jul 2008 15:55
Do you mean the actual velocity based on the independent components? If so, then wouldn't that be

dbSQRT (xvel * xvel + yvel * yvel);

The diagonal (hypotenus) equals the square root of A squared plus B squared.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 6th Jul 2008 03:27
Yeah, I got that fixed.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 6th Jul 2008 17:32
Does anyone know how to do a maximum speed? When I try the easy way, it can't decelerate.

Jaheco
16
Years of Service
User Offline
Joined: 30th Jun 2008
Location:
Posted: 6th Jul 2008 19:01
By easy way do you mean something along the lines of:

if(speed > MAXSPEED) speed = MAXSPEED;

I don't think that would prevent deceleration.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 7th Jul 2008 03:21


Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 7th Jul 2008 04:21
Thanks, dark coder, once again. Works good.

Login to post a reply

Server time is: 2024-09-30 01:22:48
Your offset time is: 2024-09-30 01:22:48