Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 357 358 [359] 360 361 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 886598 times)

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5370 on: January 23, 2014, 11:37:45 pm »

Okay, my brain isn't working with simple math(as usual) so I figure I'll ask here.  :)  I've been playing around with writing a game, and I'm trying to get my game loop working right

My game logic updates 15 times per second.  Doesn't matter if it takes longer, but it won't update faster than that.  I've got that working great.  I'd like to have my graphics code run at full speed though, which it currently is.  So although my game updates 15 times per second, the graphics can be drawn 100 frames per second(for example).

During each game update, the object updates its position and records its last position, and I'd like to render frames between the old and new position.

Normally this can be achieved by looking at the time since last frame or something, and then multiplying your objects x/y location by that factor.  I've been doing it like this:

Code: [Select]
obj.x = obj.x + (obj.x - obj.lastx) * factor
obj.y = obj.y + (obj.y - obj.lasty) * factor

I'm having a heck of a time figuring out how to calculate the factor though!  I've got access to all sorts of clocks that I can place anywhere, but my brain refuses to figure out how to calculate it.  Any thoughts?
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5371 on: January 24, 2014, 12:04:36 am »

For smoothest results, factor := timeSinceLastTick / timeBetweenLastTwoTicks.
You shouldn't update the actual object coords in the rendering code though. Do the following:
Code: [Select]
obj.displayX = obj.prevX + (obj.x-obj.prevX) * factor
obj.displayY = obj.prevY + (obj.x-obj.prevY) * factor

If you want to use prediction to make your game hyper-responsive, then factor := 1 + timeSinceLastTick / timeBetweenLastTwoTicks, but note that the prediction may be off if your game objects are under high accelerations (e.g. impacts).
« Last Edit: January 24, 2014, 12:11:40 am by MagmaMcFry »
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5372 on: January 24, 2014, 12:31:32 am »

For smoothest results, factor := timeSinceLastTick / timeBetweenLastTwoTicks.

Blarg, of course.  It makes perfect sense once I see it.  Thanks, I'll make that change tomorrow when I'm working some more on it.

If you want to use prediction to make your game hyper-responsive, then factor := 1 + timeSinceLastTick / timeBetweenLastTwoTicks, but note that the prediction may be off if your game objects are under high accelerations (e.g. impacts).

I'm not sure I get that, wouldn't that adding 1 make it run super fast?

Thankfully nothing in my game moves particularly fast though, so I shouldn't need it to be that responsive. 
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5373 on: January 24, 2014, 12:45:01 am »

Assuming obj.x is the object's position since the last logic tick and obj.prevX was the position it had before that, then obj.prevX + (obj.x-obj.prevX)*(1 + timeSinceLastTick / timeBetweenLastTwoTicks) would be the object's position right at the time of rendering, assuming a linear motion. Without the 1+, the rendering would lag one tick behind the logic, but the motion would be perfectly smooth (at least if your ticks aren't irregular).
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5374 on: January 24, 2014, 03:02:52 am »

☆python☆

So awesome! I can put dictionaries In dictionaries and stick them into an array then dump it all as formatted json. The wonders o_O it sure beats hand-assembled Perl.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5375 on: January 24, 2014, 07:34:15 am »

☆python☆

So awesome! I can put dictionaries In dictionaries and stick them into an array then dump it all as formatted json. The wonders o_O it sure beats hand-assembled Perl.
As a Perl fanboy, it very likely doesn't. Give me some Python code and I'll give you less Perl code that does the same. :P
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5376 on: January 24, 2014, 08:34:45 am »

☆python☆

So awesome! I can put dictionaries In dictionaries and stick them into an array then dump it all as formatted json. The wonders o_O it sure beats hand-assembled Perl.
As a Perl fanboy, it very likely doesn't. Give me some Python code and I'll give you less Perl code that does the same. :P

Very likely.

Difference between the two being that the resulting Python will be more readable than the Obfuscated Perl Contest submission.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5377 on: January 24, 2014, 08:39:50 am »

☆python☆

So awesome! I can put dictionaries In dictionaries and stick them into an array then dump it all as formatted json. The wonders o_O it sure beats hand-assembled Perl.
As a Perl fanboy, it very likely doesn't. Give me some Python code and I'll give you less Perl code that does the same. :P

Very likely.

Difference between the two being that the resulting Python will be more readable than the Obfuscated Perl Contest submission.
Nah, I can make it legible no problem.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5378 on: January 24, 2014, 09:17:04 am »

☆python☆

So awesome! I can put dictionaries In dictionaries and stick them into an array then dump it all as formatted json. The wonders o_O it sure beats hand-assembled Perl.
As a Perl fanboy, it very likely doesn't. Give me some Python code and I'll give you less Perl code that does the same. :P

Very likely.

Difference between the two being that the resulting Python will be more readable than the Obfuscated Perl Contest submission.
Nah, I can make it legible no problem.

Here you go!

(btw this is what I meant by "hand-assembled Perl". I didn't use any JSON modules and just output every single character by hand ;D)
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5379 on: January 24, 2014, 12:39:04 pm »

And here's my version:
Code: [Select]
use autodie;
use strict;

my $rawout = "protojson/plantproperties.json";
my $rawin = "protojson/plantproperties.protojson";

open (my $RAWIN, "<", $rawin);
open (my $RAWOUT, ">", $rawout);

print $RAWOUT "[\n";
for my $line (<$RAWIN>) {
chomp (my $myline = $line);
my @entries = split /\t/, $myline;
my $jstr = jsontemplate();
my $i = 0;
$jstr =~ s/%/$entries[$i++]/eg;
print $RAWOUT $jstr;
}
print $RAWOUT "]\n";

sub jsontemplate {return <<"ENDJSON"}
{
"name" : "%",
"annual" : "%",
"isTree" : "%",
"maxLAI" : %,
"stage 6" : %,
"stage 7" : %,
"stage 9" : %,
"stage 10" : %,
"base temp" : %,
"water tolerence" : %,
"max height" : %,
"max root depth" : %,
"starting nitrogen" : %,
"final nitrogen" : %,
"day neutral" : "%",
"flowering min temp" : %,
"flowering optimal temp" : %,
"floral induction units" : %,
"long day" : "%",
"minimum induction" : %,
"critical night length" : %,
"min growth temp" : %,
"optimal growth temp" : %,
"base" : {
"stem" : %,
"root" : %,
"storage" : %,
"fruit" : %
},
"flowering" : {
"stem" : %,
"root" : %,
"storage" : %,
"fruit" : %
},
"final" : {
"stem" : %,
"root" : %,
"storage" : %,
"fruit" : %
},
"germination min temp" : %,
"germination optimal temp" : %,
"germination thermal units" : %,
"average fruit weight" : %,
"seed ratio" : %,
"dormancy" : %,
"default dormancy" : "%",
"dormant height decrease" : %,
"dormant root decrease" : %,
"dormant biomass" : {
"stem" : %,
"root" : %,
"storage" : %,
"fruit" : %
}
},

ENDJSON
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5380 on: January 24, 2014, 01:52:08 pm »

That may be less code than Skyrunner's Perl, but it doesn't duplicate its functionality exactly.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5381 on: January 24, 2014, 01:53:55 pm »

Here's an exact duplicate (up to JSON equivalence):
Code: [Select]
use autodie;
use strict;
use JSON;

my $rawout = "protojson/plantproperties.json";
my $rawin = "protojson/plantproperties.protojson";

open (my $RAWIN, "<", $rawin);
open (my $RAWOUT, ">", $rawout);

my @attrs;
for my $line(<DATA>) {
chomp($line);
my ($key, %depends) = split(":",$line);
push @attrs, [$key, \%depends];
}

my @structs;

for my $line (<$RAWIN>) {
chomp(my $line2 = $line);
my @matches = map {$_ eq $_+0 ? $_+0 : $_} split /\t/, $line2;
my %flat;
ATTR: for my $i (0..$#attrs) {
for my $dep (keys %{$attrs[$i][1]}) {
next ATTR if lc($attrs[$i][1]{$dep}) ne lc($flat{$dep});
}
$flat{$attrs[$i][0]} = $matches[$i];
}
my %final;
for my $flatkey (keys %flat) {
my @subdirs = split m"/", $flatkey;
my $finalkey = pop @subdirs;
my $cdir = \%final;
for my $dir (@subdirs) {
$cdir->{$dir} = {} unless $cdir->{$dir};
$cdir = $cdir->{$dir};
}
$cdir->{$finalkey} = $flat{$flatkey};
}
push @structs, \%final;
}

print $RAWOUT to_json(\@structs, {pretty => 1});
__DATA__
name
annual
isTree
maxLAI
stage 6
stage 7
stage 9
stage 10
base temp
water tolerance
max height
max root depth
starting nitrogen
final nitrogen
day neutral
flowering min temp:day neutral:false
flowering optimal temp:day neutral:false
floral induction units:day neutral:false
long day:day neutral:false
minimum induction:day neutral:false
critical night length:day neutral:false
min growth temp
optimal growth temp
base/stem
base/root
base/storage
base/fruit
flowering/stem
flowering/root
flowering/storage
flowering/fruit
final/stem
final/root
final/storage
final/fruit
germination min temp
germination optimal temp
germination thermal units
average fruit weight
seed ratio
dormancy
default dormancy
dormant height decrease:default dormancy:false
dormant root decrease:default dormancy:false
dormant biomass/stem:default dormancy:false
dormant biomass/root:default dormancy:false
dormant biomass/storage:default dormancy:false
dormant biomass/fruit:default dormancy:false
Logged

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: if self.isCoder(): post() #Programming Thread
« Reply #5382 on: January 24, 2014, 03:20:15 pm »

I'll just go ahead and give the legibility award to Python :P

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5383 on: January 24, 2014, 04:04:44 pm »

Perl was never supposed to be legible in the first place! *grumbles away incoherently*
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5384 on: January 24, 2014, 08:29:52 pm »

Interesting way to fill out the json code, by apparently making a large template file and just filling in the appropriate strings o.O
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward
Pages: 1 ... 357 358 [359] 360 361 ... 796