Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 137 users online: 4 members, 96 guests and 37 bots. Members: colophonicd, mouhni, Xeda112358. Bots: VoilaBot (3), Magpie Crawler (4), VoilaBot (12), Googlebot (17), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
|
| Author |
Message |
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 27 Apr 2012 07:30:03 pm Post subject: |
|
|
Yeah, I know. I was just complementing your skills.
How do you do Gravity in C? |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55881 Location: Earth, Sol, Milky Way
|
Posted: 28 Apr 2012 09:13:54 am Post subject: |
|
|
| Spenceboy98 wrote: | Yeah, I know. I was just complementing your skills.
How do you do Gravity in C? | First you should really understand how gravity works in physics; I assume you haven't taken a physics class yet or learned kinematics. Once you know bits of math or physics, and you know a language well, a mark of a good programmer is being able to translate that math or physics into code. So, assuming that you know physics and you know C, you would use the following equations for an object moving in 2D space, with gravity:
- Variables needed: x_pos, y_pos, x_vel, y_vel
- Constants assumed: x_accel = 0, y_accel = -9.8 [m/s^2]
On every physics tick:
- y_accel, x_vel remain unchanged
- y_vel += y_accel
- x_pos += x_vel
- y_pos += y_vel _________________
 |
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 28 Apr 2012 01:47:00 pm Post subject: |
|
|
Code: struct Coord {
int x;
int y;
};
int keydown(int basic_keycode)
{
const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
int row, col, word, bit;
row = basic_keycode%10;
col = basic_keycode/10-1;
word = row>>1;
bit = col + 8*(row&1);
return (0 != (keyboard_register[word] & 1<<bit));
}
void moveCursor(struct Coord* cursor) {
if( keydown(KEY_PRGM_UP) && cursor->y>0) cursor->y--;
if( keydown(KEY_PRGM_DOWN) && cursor->y<LCD_HEIGHT_PX) cursor->y++;
if( keydown(KEY_PRGM_LEFT) && cursor->x>0) cursor->x--;
if( keydown(KEY_PRGM_RIGHT) && cursor->x<LCD_WIDTH_PX) cursor->x++;
}
int main() {
struct Coord cur;
cur.x = 0;
cur.y = 0;
int y = 2;
int x = 0;
int g = 1;
while(1) {
int key = PRGM_GetKey();
if(key == KEY_PRGM_MENU) { GetKey(&key); }
moveCursor(&cur);
CopySprite(BigCursor, cur.x, cur.y, 12, 21);
y+=g;
cur.y+=y;
cur.x+=x;
Bdisp_PutDisp_DD();
Bdisp_AllCr_VRAM();
}
return 0;
}
That's my code. How do I make it stop at a certain limit? Like have a platform to stand on. Oh, and jump. |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55881 Location: Earth, Sol, Milky Way
|
Posted: 29 Apr 2012 02:03:45 pm Post subject: |
|
|
Hmm, your variable names are extraordinarily non-descriptive, which is not great and made me confused. y should be vel_y, and x should be vel_x. Or vy and vx. Or velocity_y and velocity_x. Longer variable names in C don't make your program larger, so there's no point in trying to be overly concise. When the y-position reaches some value, you'll probably want to set the y velocity to zero, and not let it apply the gravity until it's not on the "platform" anymore, right? You're asking me questions more about thinking and logic than programming, to be honest. _________________
 |
|
| Back to top |
|
|
Ashbad

I am governor Jerry Brown

Joined: 01 Dec 2010 Posts: 2423 Location: There lived a certain man in Russia long ago
|
Posted: 29 Apr 2012 02:06:10 pm Post subject: |
|
|
Most of the time you'll be having a map in some sort of 2D array form, and each array entry will correspond with an actual tile on the screen. What you'll essentially do is convert from the x/y coordinates of the player to the x/y position in the array, through subtraction of the screen viewing offset and a division of the pixel size, and test to see if you can walk on the block below you in the array or not. _________________ -Ashbad |
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 29 Apr 2012 02:07:14 pm Post subject: |
|
|
Well, I'll change the var. names. So I just put something like, Code:
if( cur.y > 216)
vel_y = 0
?
And Ashbad is saying like if I want different height platforms? |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55881 Location: Earth, Sol, Milky Way
|
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 29 Apr 2012 02:11:54 pm Post subject: |
|
|
| But how would I make it jump? |
|
| Back to top |
|
|
PierrotLL

Advanced Newbie

Joined: 29 Nov 2011 Posts: 72 Location: France
|
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 29 Apr 2012 03:12:28 pm Post subject: |
|
|
Now it won't jump when it's moving left or right.
Code: int main() {
struct Coord cur;
cur.x = 0;
cur.y = 0;
int vel_y = 2;
int vel_x = 0;
int g = 1;
while(1) {
int key = PRGM_GetKey();
if(key == KEY_PRGM_MENU) { GetKey(&key); }
CopySprite(BigCursor, cur.x, cur.y, 12, 21);
if( cur.y >= 194) {
vel_y = 0;
cur.y = 194;
}
if (cur.y < 194) {
vel_y+=g;
}
if(key == KEY_PRGM_SHIFT && cur.y == 194)
vel_y = -10;
cur.y+=vel_y;
cur.x+=vel_x;
if(keydown(KEY_PRGM_LEFT) && cur.x > 0)
cur.x-=16;
if(keydown(KEY_PRGM_RIGHT) && cur.x < 372)
cur.x+=16;
Bdisp_PutDisp_DD();
Bdisp_AllCr_VRAM();
}
return 0;
}
|
|
| Back to top |
|
|
LincolnB

Advanced Member

Joined: 07 Jul 2011 Posts: 199
|
Posted: 29 Apr 2012 03:22:28 pm Post subject: |
|
|
Why does your Coord struct not include values for vel_y and vel_x? Also, there's no point to having a vel_x variable if you never use it in the code. The point of the vel_x variable is to be able to replace:
Code: cur.x-=16;
with
Code: vel_x=-16
If you don't want to do that, just get rid of the vel_x variable.
Besides these suggestions, I don't know what's causing your character not to jump while you're holding Left or Right. I'm guessing it has something to do with the way you're mixing up the two ways of getting key input. Like, you're using this method:
Code: if(key == X)
early in the code, and
Code: if(keydown(X)
later on...try changing all of them to the same method (probably the latter, but that's just a guess) and see if that works. _________________ -LincolnB
 |
|
| Back to top |
|
|
Ashbad

I am governor Jerry Brown

Joined: 01 Dec 2010 Posts: 2423 Location: There lived a certain man in Russia long ago
|
Posted: 29 Apr 2012 03:30:15 pm Post subject: |
|
|
| LincolnB wrote: | Besides these suggestions, I don't know what's causing your character not to jump while you're holding Left or Right. I'm guessing it has something to do with the way you're mixing up the two ways of getting key input. Like, you're using this method:
Code: if(key == X)
early in the code, and
Code: if(keydown(X)
later on...try changing all of them to the same method (probably the latter, but that's just a guess) and see if that works. |
His input method is a bit unorthodox, and he should just use keydown(), but what he currently has should work just fine, concerning input methods. _________________ -Ashbad |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55881 Location: Earth, Sol, Milky Way
|
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 29 Apr 2012 08:41:53 pm Post subject: |
|
|
| Well, if I do that, it will just keep the cursor moving forever. It won't stop. How do I do what Ashbad was saying? |
|
| Back to top |
|
|
zeldaking
Power User

Joined: 31 Jul 2011 Posts: 474 Location: Utah
|
Posted: 30 Apr 2012 12:29:03 am Post subject: |
|
|
As soon as it hits your boundaries set your velocity variables back to zero, so they stop.
Code:
if (Xpos+Xvel>Xboundary) {
Xvel=0;
Xpos=Xboundary-Xspritesize;
//that is so the sprite is shown near the edge of the screen not stopped at whatever it was before
//where Xpos = X coord
//where Xvel = X velocity increment
//where Xspritesize = Sprite width
//where Xboundary = X boundary
}
//other if blocks
|
|
| Back to top |
|
|
LincolnB

Advanced Member

Joined: 07 Jul 2011 Posts: 199
|
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 01 May 2012 06:45:44 pm Post subject: |
|
|
Okay, so I tried to display a Nyan Cat animation on my Prizm, but it didn't work. It reboots the calculator. Can you fix?
Code: int frames = 1;
int main() {
while(1) {
int key = PRGM_GetKey();
if(key == KEY_PRGM_MENU) { GetKey(&key); }
Bdisp_EnableColor(1);
CopySprite(frames%12*160*160+(color_t*)NyanCat1, 0, 0, 160, 160);
frames++;
if(frames > 4)
frames = 1;
Bdisp_PutDisp_DD();
}
return 0;
}
|
|
| Back to top |
|
|
PierrotLL

Advanced Newbie

Joined: 29 Nov 2011 Posts: 72 Location: France
|
|
| Back to top |
|
|
Spenceboy98

Super-Expert

Joined: 06 Jan 2012 Posts: 855 Location: In the TARDIS
|
Posted: 01 May 2012 07:19:46 pm Post subject: |
|
|
| I'm not sure. When you add to the frames variable, it changes the frames to the one after. The 12 tells how many frames are used. I'm not sure what the "24*24" or the "color_t*" does. |
|
| Back to top |
|
|
LincolnB

Advanced Member

Joined: 07 Jul 2011 Posts: 199
|
Posted: 01 May 2012 09:10:50 pm Post subject: |
|
|
This sounds like you may be undertaking more than you can handle? idk. Maybe you should start really small, with less of an established goal in mind - however, that's a touchy thing to say because you need to have an idea of what the end product will look like. This is a little bit rude, but the only real way to learn how to program is to figure things out, by yourself - going through your code on a case-by-case basis, understanding every denotation of every line. _________________ -LincolnB
 |
|
| Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.045123 seconds.
|