/dpt/ - Daily Programming Thread

Old thread: What are you working on, Sup Forums?

Other urls found in this thread:

youtube.com/watch?v=qdSLaFirs_s
youtube.com/watch?v=BZsE2PFWS4Q
twitter.com/SFWRedditImages

that can't be legal

So to learn C i've been making a 2D platform game. I've actually set up most of the back end stuff quite well, but now I am at the part I figured I would not do well at: collision.

What I am trying to create is something like: youtube.com/watch?v=qdSLaFirs_s and youtube.com/watch?v=BZsE2PFWS4Q

What I think my problem may be is order of operations. What order do I check collision in? Where do I check collision? Currently, I have four functions for collision checking, one to check the left side, the right side, the top side, and the bottom side (and they are called in that order, after the entity is done thinking); they are mostly identical but for the left/right functions it will scan for a column of tiles next to the direction its searching, whereas the other two scan for a row of tiles. I have sloped tiles but I have yet to program those in, so every tile is treated like a solid square at the moment.

If the hitbox is going to be past the coordinate it is scanning for, it will adjust so its touching instead. I think this is where the order of operations becomes a problem. For example, if I head down, then the entity slides all the way to the right (which tells me the left side collision checking is technically doing its thing.

I've been looking at various forum posts and guides and I really am just lost. I'm not trying to be the most optimized, but I don't want to make something that would be "expensive" (for an old dos machine playing a similar game in its time) either.

If you need some information about the code. The hitbox is a struct with an x, y, w, and h. The level tiles are stored in a one dimensional array, and the tile is grabbed with the following forumla:
tileN=(tileY*mapwidth)+tileX;
For now, the width and height of each tile are 16, and my entities hitbox is 16x32. I'll post some code in the reply as I have ran out of characters.

Does anyone have any idea how I may get this working?

>>For now, the width and height of each tile are 16, and my entities hitbox is 16x32.

As an example, if I search the tiles to the left, the code would be:
int begY = entbox.y

I see so many different and conflicting things, not to mention most search results I get are for unity or game maker, I have no idea where to go anymore lol. I will try that phrase and hope it gets me somewhere.

Not sure what theta is. That might work but only for hitboxes that are square, not rectangular. A problem with the code I have is if i'm up or down on the y just a little bit, the hitbox technically is in three rows, but it will still only search the first two.

What about what I said to you in the other thread doesn't make sense?

> there couldn't be any solid tiles next to it or else the entity would stop moving up because its colliding with the wall next to it.

what does this even mean? this sounds like expected behavior. can you record a gif/webm?

Theta is the symbol used for an angle in radians.

>That might work but only for hitboxes that are square, not rectangular.

Why? It wouldn't change your ability to calculate the y's for the upper bound of the hitbox.

I don't have that code anymore, but here is a photo that would describe the issue.

I may have misread your thing then, I thought you meant only checking the corners.

>What are you working on, Sup Forums?

I'm on Think Java : Chapter 12 Arrays of Objects. Can I get a job now? I can't go back to working in the supermarket counting 200 individual lemons.

For now, I was only talking about corner checking. Side checking is easy.

So in your example, lowerLeftY at centerX-halfwidth shouldn't be able to drop below y as calculated by mx+b at centerX-halfwidth.

What in your collision algorithm would allow that to happen? Probably an off-by-one error?

I'm not sure, perhaps it is the order of operations as I said. After the entity would think, I would check collisions by left, right, up, and down.

So if i moved down, the left/right checks would run first and notice that they are in a tile and offset to correct that.

I added an x_spd and y_spd variable so I am going to try putting those in where I think they might need to be.

Oh I misread you again, i'm sorry. I've spent all day trying to figure this out, i've tried taking a break to think, but nothing seems to be working. My brain is pretty rip today.

tech company HR depts don't take too kindly to self-taughts trying to change fields from unrelated fields like trades or minimum wage grocery store work

I'm learning C and I'm having trouble with this do loop. It seems to end before the while statement is true. Test starts at 0 and strlen was 3 in my test. When I went through the debugger test ended at 1 and the while loop didn't repeat.
do
{
if (isalpha(k[test]))
{
if (isupper(k[test]))
{
k[test] = k[test] - 65;
}
else
{
k[test] = k[test] - 97;
}
++test;
}
else
{
return 1;
}
}
while (test < strlen(k));

I mean before the while statement is false.

How does Ayano's hair smell like?

I have a (2.2) maths degree from 7 years back. That has to count for something, right?

We've all been there.

The order would have an effect.

For floor calculations at x, if I'm moving left, and floorY at x-1 is higher than floorY at x because of m, I need to update my hitbox's y before I should check left for a collision. At least in the way you're doing things.

Does k contain the letter 'a'?

probably like nothing because she doesn't exist

Yes, in my test I used the string ABC and abc, both behaved the same way. Would that cause problems?

bro, before I look at this, you should use character literals to make this clearer.

k[test] = k[test] - 'A';

What is the term for the following in C?
struct person myperson = {
.birth_year = 1954,
.name = "Angela Merkel"
};

why not -=

Because I wasn't thinking. Even better.

designated (i shit you not) initializer

'A' - 65 and 'a' - 97 give zero and put a null terminator '\0' in your string

Please make it more readable:
size_t len = strlen(k);
do {
if (isalpha(k[test])) {
k[test] -= isupper(k[test]) ? 'A' : 'a';
++test;
} else {
return 1;
}
} while (test < len);

It's to do with the way strings work in C.
They're a null terminated array of characters. That's to say, the last character has the value zero.
So here is the state of the array when you enter the loop:
{'A', 'B', 'C', '\0'}
strlen(k) == 3, test == 0

k[0] = k[0] - 'A'
so k[0] == '\0'

Right before the end of the first iteration, your array will look like
{'\0', 'B', 'C', '\0'}
But to a C string function, this has a length of 0, because it will stop when it sees the first null character.
strlen(k) == 0, test == 1, so the loop stops.

The way to fix this is to save the length of the string before the loop, and test using that.
size_t len = strlen(k);
do {
// ...
} while (test < len);

thanks for doing that.

now the question is what the fuck user is trying to do.

What is the goal (or name) of this function?

because even with the clearer code user provided, you're still going to have a null terminator problem with A/a

>put a null terminator '\0'
Thank you! I'll try making the sum +1 larger and fix it when converting it back into characters.

just run the strlen function once and store it in a variable like did, it will fix your problem

As long as you're not printing the intermediary character array, you shouldn't have to worry about the char array containing a null terminator if you're going to do further conversions.

So what exactly are you doing?

wow user, you're so smart and good at programming. thank god you were here to show that beginner who's learning the language some parts of the syntax he hasn't seen so that it's easier to read. I don't know what he would've done without your input

I like to keep the indentation low. Do you like this style?

size_t len = strlen(k);

do {
if (!isalpha(k[test]))
return 1;

k[test] -= isupper(k[test]) ? 'A' : 'a';
test++;
} while (test < len);

I had nothing to do today and created something that generated brazilian id numbers, but I got bored and added the fork() line and sent to friend for testing

whatever floats your boat. If you're not going to bracket the if, I'd probably inline it.

some user yelled at me for using the return value of an assignment in the conditional statement, because it was unclear.

If he didn't know, would have asked.

If you're trying to learn how to be a C programmer, you better get used to how C actually ends up looking.

L-Lewd

by the way, why is this a do-while? you still handle the zero-length case anyway but it'd feel a bit more normal to have a straight-while.

So I get using why character literals are clearer though I'm not sure what's going on in this line.

k[test] -= isupper(k[test]) ? 'A' : 'a';


I literally just started, so is this a format I just haven't been shown yet?

-= I assume this means subtract something that a follows from the variable and then set the variable to it.

The second part I assume means use 'A' if true and 'a' if false?

Thank you, I tried the thing I was thinking and it did work, but setting the length to a variable makes much more sense.

This is a problem for the noncredit programming lecture I'm following. The goal for it was to make sure the string provided as an argument was solely letters. This is then used as a cipher key to output a new string provided while the program was running.

Can you think of any examples where the register keyword makes any difference to the asm output?

I'd agree with just while. or even

size_t len = strlen(k);
int i;
char ki;

for(i = 0; i < len; ki = k[i++]) {
if (!isalpha(ki)) return 1;
k[i] -= isupper(ki) ? 'A' : 'a';
}

Compilers are typically smart enough for it to not matter.

var = condition ? lorem: ipsum;

/* Is equal to */

if (condition)
var = lorem;
else
var = ipsum

/* And */

var -= 3;

/* Is equal to */
var = var - 3;

You aren't initializing ki on the first pass. Also, why the aversion to C99/for-loop initializers?

compilers ignore it nowadays, it's still accepted in the code but does nothing

yes
a -= b;
is
a = a - b;

(b ? x : y)
evaluates to x if b is true
else it evaluates to y

Thank you, that makes sense.

fucking cheeky compilers

>but does nothing
It does actually do something, so it's not literally useless like auto.
It makes it so you can't take the address of it.

There is a proposal for the next C standard to extend it and make 'register const' a proper typed constant.

>You aren't initializing ki on the first pass.
Good call.

>Also, why the aversion to C99/for-loop initializers?
Simply force of habit when I try to write C

size_t len = strlen(k);

for (; test < len; test++) {
if (!isalpha(k[test]))
return 1;
k[test] -= isupper(k[test]) ? 'A' : 'a';
}

This picture is very unsettling. It's affecting my productivity as I keep thinking about this picture.

Leave Ayano alone. Please don't smell her hair.

What was the thought process of keywords expands and implements in Java? For an OOP language the syntex for classes seems pretty shitty

>For an OOP language the syntex for classes seems pretty shitty

Agreed. I have to write java in my CS course and it urks me to look at stuff like
private int a;
private int b;
private int c;
private int d;

forgot about C evaluating uninitialized vars as 0
thanks for fixing my code

>forgot about C evaluating uninitialized vars as 0
No it doesn't. It's unspecified what the value is.

this is only true for static variables

but it's evaluated as 0, no?

It doesn't. I didn't test or run it because I'm not the guy who posted the original and don't know what the variable "test "should be. I just assumed it was properly initialized somewhere else.

>all that bloat
this is actually assault. java is actively trying to harm my comprehension

No. It's generally undefined behaviour if you use it.
Basically, don't use initialised variables, ever.

got it

I know, just wasn't sure if it was some trick C people took advantage of.

maybe. if you don't want to initialize variables you should test its value with an if-statement after you declare it

By the way you can use calloc to allocate zero-initialized memory.

thanks for the tip

I like helping newfriends with their C questions, and it's very nostalgic for me. One day I hope I can get a job where I can actually use C. At least for a little while.

you're absolutely right and I actually agree with you, it just seemed like in his case he's new enough that what he posted really is, to him, the most readable format. I know at least if I were in his position, seeing
k[test] -= isupper(k[test]) ? 'A' : 'a';

would just confuse and distract me to the point that I wouldn't be able to actually figure out the issue at hand anymore, but that's just me I do that to myself a lot

anyway, I only responded to you the way I did as a bad mood shitpost, my apologies

You're point was valid. I assumed he would have known about compound assignment operators. Ternaries were a stretch.

I'm glad user asked and now knows. We definitely added another step which was unrelated to fixing the problem with his algorithm, but he's better for it.

wew lad
private:
int a;
int b;
int c;
int d;

That's not valid java user

It would be nice if it were, though, right?

fuckin a right it would

That's what I thought. Not that I would know

fucking 10 captchas man

I'm glad I'm not the only one getting 10 captchas wtf

Why do you have variables that do not self-comment via their identifiers?
Why do you have a global integer variable for a, b, c and d? What situation would demand this?

Are you literally autistic?

like 10 captchas and then it disconnected and made me do 2 more

8 more to post this hehehee

No but my brother is.

stay away user i hear it's contagious

Confession, I hate meaningful variable names. They are just so god damn ugly.

how about
private int a,b,c,d;

holy fuck you're right, I guess I've only ever written my own classes from scratch in cpp

>zero captchas

Not easy to read.

I bet you are also ugly.

private int a,
b,
c,
d;
is this legit?

The question was basically why Java doesn't have context blocks, and it was really about the perceived verbosity in OOP.

I remember when I first saw things like
SomeThing someThing = new SomeThing();

and thinking it was verbose or boiler-platey

So much better if you could just
record Thing where
constructor MkThing
a : Int
b : Int
c : Int
d : Int

doSomething : StateT Thing IO a

tenta comprar algo com isso lol

ew. too unlike c

Nah, it'd be so much better if you could just write in Sanskrit, the source code of the world.

That's English, Pajeet

Better. But you should self-comment by using descriptive variable names.

C is the Sanskrit of computing

In OpenGL is there a noticable performance difference between GL_NEAREST and GL_LINEAR?

You are an uncultured swine. Sanskrit is related to Indo-European languages.

>C is the Sanskrit of computing
Wouldn't that be perl? Just a bunch of symbols whose meaning becomes lost a month after you write them.

I know, that's why I called you Pajeet, Rasheed

Sanskrit is the indo-european language, everything else is just a hacky mess thrown together by barbarians barely capable of communicating more than their ids desires.

queria que funcionasse kk

say I have a colour image represented as a 3D matrix.
Is there any name for an operation that sums all the values in each channel? e.g returns a 3-tuple of (red_values_sum, green_values_sum, blue_values_sum)
or would I have to just slice out each layer (colour channel) and sum it individually and then put the 3 sums together in the tuple?

in matlab this is just sum(sum(M)) :)

Non-linguists/non-language enthusiasts get out