Author Topic: can someone help me with my physics code?  (Read 3645 times)

0 Members and 1 Guest are viewing this topic.

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
can someone help me with my physics code?
« on: October 17, 2013, 01:39:27 pm »
Hi i'm new here and i am learning axe right now. i have written some code and it works, but i can't seem to stop my block from going offscreen...
can someone please help me?
here is the sourcecode:
Code: [Select]
.GRAVITY
Full
DiagnosticOff
#ExprOn
44*256→X
26*256→Y
0→A
0→B
Repeat getKey(15)
ClrDraw
rect(X/256,Y/256,8,8)
If Y/256=0
‾B→B
End
If Y/256=56
0→B
Else
B+4→B
End
If getKey(2)
A-4→A
End
If getKey(3)
A+4→A
End
If getKey(4)
B-8→B
End
If A<<0
A+1→A
End
If A>>0
A-1→A
End
Y+B→Y
If X/256=0
‾A→A
End
If X/256=86
‾A→A
End
X+A→X
DispGraph
End

edit: i've edited my program and now it tests pixels if there is a pixel next to him but sometimes it still goes offscreen what am i doing wrong ???

Code: [Select]
.PHYSICS

.full speed, no marching ants and optimized for size
Full
DiagnosticOff
ExprOff

.smiley sprite
[3C42A581A599423C]→Pic1

.initiate x/y coordinates
44*256→X
26*256→Y

.set initial acceleration to zero
0→A
0→B

.main loop until [clear] is pressed
Repeat getKey(15)

.pause
If getKey(54)
Pause 12000
End

.display sprite
Pt-On(X/256,Y/256,Pic1

.display border lines
HLine(0)
HLine(63)
VLine(0)
VLine(95)

.ceiling collision detection
If pxl-Test(X/256,Y/256)
 ‾B→B
End

.gravity and ground detection
If pxl-Test(X/256,Y/256+8
 0→B
Else
 B+4→B
End

.move left
If getKey(2)
 A-4→A
End

.move right
If getKey(3)
 A+4→A
End

.move up
If getKey(4)
 B-8→B
End

.friction right
If (A<<0)
 A+2→A
Else
 A+1→A
End

.friction left
If (A>>0)
 A-2→A
Else
 A-1→A
End

.wall collision
If pxl-Test(X/256,Y/256+1) or pxl-Test(X/256+7,Y/256+1)
 ‾A→A
End

.accelerate
X+A→X
Y+B→Y

.set vector length
A//8→C
B//8→D

.display force vectors
Line(X/256+4,Y/256+4,X/256+4+C,Y/256+4)
Line(X/256+4,Y/256+4,X/256+4,Y/256+4+D)

.update screen and end loop
DispGraph
ClrDraw
End
« Last Edit: October 18, 2013, 01:30:41 am by TheCoder1998 »
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline bored_student

  • LV3 Member (Next: 100)
  • ***
  • Posts: 44
  • Rating: +3/-0
    • View Profile
Re: can someone help me with my physics code?
« Reply #1 on: October 18, 2013, 03:57:49 pm »
Be careful with Pixeltests when you are using acceleration.
I think what happens here is the following:

imagine Y=55*256 + 255
your sprite is displayed with a one-pixel gap between the sprite and the ground line because  Y/256=55.996  is truncated to 55 in Axe. The pixeltest returns false.
Now your acceleration is at 257 or more.
In the next loop  Y=55*256 + 255 + 257 = 57*256
your sprite is displayed at Y/256 = 57  this is already on the ground line. The pixeltest which tests the row under your sprite returns false because the ball has already crossed the line. Now there is no way to stop your ball from falling further off the screen.

This can not only happen with the values I pointed out above but always if the acceleration is more than 256. The ball can skip certain pixel lines then and the same can be at the vertical walls.

There are three different solutions I can imagine:
A: Draw multiple lines on the ground (I wouldn't do that because the ball may kind of sink into the bottom lines and stay there
B: Keep the acceleration under 256 (this shouldn't be a problem  ;) )
C: Don't use pixeltest but check if the ball is lower then the last line (somthing like  Y/256>55  be careful then with the negative Y when the ball goes offscreen at the top  then you may use  Y/256>>55)

In my opinion version C is the best because I think it's the most clean and the fastest solution   :)
Sorry for my bad English, I'm German.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: can someone help me with my physics code?
« Reply #2 on: October 18, 2013, 04:37:51 pm »
Last solution, which will work in all cases with every speed and every wall, wherever it's on the screen, use a path-tracking routine. Here's how you do this :
  • Start at the bottom of your sprite
  • Test the pixel under it. If it's on, reset the gravity and exit the test loop, so your sprite won't move anymore since it landed on some ground. If the pixel is off, increment the Y coordinate of your sprite.
  • Repeat the previous step until you reach a pixel that is on, or until your sprite reaches the coordinate it would have if you just added the gravity to its initial coordinates.
It's not that easy to code and can be a bit slow with a high number of objects, but it can not fail if you write it correctly. Furthermore, you can super easily adapt it to work with up, left and right movement (I use this method for 4-ways collision testing in I'm Thirsty).
If you have any question, feel free to ask.

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: can someone help me with my physics code?
« Reply #3 on: October 20, 2013, 02:04:07 am »
thank you for the replies,  i really appreciate it.

the code finally works like i wanted it to.
i'm going to learn how slope physics work now.
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza