Bouncing ball: Try making a circle bounce around the screen at random angles. Sp
ID: 3574279 • Letter: B
Question
Bouncing ball: Try making a circle bounce around the screen at random angles. Specifically you'll want to draw a circle on the screen that will move in one dircetion until it hits the edge of the screen. Then it will 'bounce' off the edge at a random angle and change its velocity.
I need to create this code using matlab
this is what I have so far :
[w,rect]=Screen('OpenWindow',0,[128 128 128],[0,0,500,500]);
%Stepping feet illusion
KbName('UnifyKeyNames')
buttonEscape=KbName('ESCAPE');
%Open the screen
[w,rect] = Screen('OpenWindow',0,[0 0 0],[100,100,500,500]);
%Define the size of the circle
circSize = 100;
%Determine the starting location of the center of our square
circCenterX = circSize/2;
circCenterY = circSize/2;
%Starting color of the bars
colorValue = [255 255 255];
%Color of the rectangles
circ1Color = [0 255 255];
%How fast do you want your rects to move across the screen
acc = 10;
%Move the 'feet' until you hit escape
[keyIsDown, secs, keycode] = KbCheck;
while ~keycode(buttonEscape)
[keyIsDown, secs, keycode] = KbCheck;
%Draw the background bars
locationHolder=0;
for i=1:circCenterX+circSize/2 < rect(3)
Screen('FillOval',w,[255 255 255],[circCenterX-circSize/2,circCenterY-circSize/2,circCenterX+circSize/2,circCenterY+circSize/2]);
Screen('Flip',w);
circCenterX=circCenterX+acc;
circCenterY=circCenterY+acc;
end;
if circCenterX+circSize/2 >rect(3);
acc=acc*(-1);
elseif circCenterX+circSize/2 < rect(3);
acc=acc*(1);
end;
end;
Screen('Flip',w);
However It only 'bounces onces and I am unsure how to change the angle
Thanks for you help
Bouncing ball. trymaking a circle bounce around our screen at random angles.Specifically you’ll want to draw a circle on the screen that will move in one direction until it hits theedge of the screen. Then it will ‘bounce’ off the edge at a random angle and change its velocity.
Explanation / Answer
Hii there run this code :
<html>
<head>
</head>
<body>
<div align="center" id="img">
<img src="http://www.htmlbestcodes.com/images/ball.gif">
</div>
<SCRIPT LANGUAGE="JavaScript">
var step = 5;
var delay = 30;
var height = 0;
var Hoffset = 0;
var Woffset = 0;
var yon = 0;
var xon = 0;
var pause = true;
var interval;
var name = navigator.appName;
if(name == "Microsoft Internet Explorer") name = true;
else name = false;
var xPos = 20;
if(name) var yPos = document.body.clientHeight;
else var yPos = window.innerHeight;
function changePos() {
if(name) {
width = document.body.clientWidth;
height = document.body.clientHeight;
Hoffset = img.offsetHeight;
Woffset = img.offsetWidth;
img.style.left = xPos + document.body.scrollLeft;
img.style.top = yPos + document.body.scrollTop;
}
else {
height = window.innerHeight;
width = window.innerWidth;
Hoffset =33;
Woffset =30;
document.getElementById('img').style.top = yPos + window.pageYOffset;
document.getElementById('img').style.left = xPos + window.pageXOffset;
}
if (yon) {
yPos = yPos + step;
}
else {
yPos = yPos - step;
}
if (yPos < 0) {
yon = 1;
yPos = 0;
}
if (yPos >= (height - Hoffset)) {
yon = 0;
yPos = (height - Hoffset);
}
if (xon) {
xPos = xPos + step;
}
else {
xPos = xPos - step;
}
if (xPos < 0) {
xon = 1;
xPos = 0;
}
if (xPos >= (width - Woffset)) {
xon = 0;
xPos = (width - Woffset);
}
}
function start() {
document.getElementById('img').style.visibility = "visible";
interval = setInterval('changePos()',delay);
}
function pauseResume() {
if(pause) {
clearInterval(interval);
pause = false;
}
else {
interval = setInterval('changePos()',delay);
pause = true;
}
}
start();
// End -->
</script>
<br/><div></div><div><a target="_blank" href="http://www.htmlbestcodes.com/"><span>HTML Best Codes</span></a></div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.