Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: TheCoder1998 on October 17, 2013, 01:39:27 pm

Title: can someone help me with my physics code?
Post by: TheCoder1998 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
Title: Re: can someone help me with my physics code?
Post by: bored_student 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   :)
Title: Re: can someone help me with my physics code?
Post by: Matrefeytontias 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 :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.
Title: Re: can someone help me with my physics code?
Post by: TheCoder1998 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.